none
Error when using ValueTask RRS feed

  • Question

  • I have VS 15.6.0 Preview 1. For this code:

    private async ValueTask<int> FFF()
    {
        return await Task.Run(() => 5);
    }

    VS gives me error: The return type of an async method must be void, Task or Task<T>

    I wonder why?


    There is no knowledge that is not power.

    Saturday, December 16, 2017 5:09 PM

Answers

  • @Kareninstructor, @Wizend

    So, why this error occurs?


    There is no knowledge that is not power.

    If you are using VS2017, targeting C# 7 you can use ValueTask, otherwise it's not supported in earlier versions of Visual Studio e.g. C# 6, only C#7.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by JohnyL Tuesday, December 19, 2017 5:54 AM
    Sunday, December 17, 2017 9:49 PM
    Moderator
  • You told us you're working with VS 15.6. Maybe you need to make the new C# 7.0 ValueTask feature available by adding the NuGet package 'System.Threading.Tasks.Extensions' to your project, esp., when your project is compiled in an older .NET version.

    wizend

    • Marked as answer by JohnyL Tuesday, December 19, 2017 5:54 AM
    Monday, December 18, 2017 11:36 AM

All replies

  • Your method needs a return type to be a valid C# method, as the error message says. In your case it would be:

    private async Task<int> FFF()
    {
        return await Task.Run(() => 5);
    }

    There are only three possible return types for async methods: Task, Task<T> and void. For more details, see Async/Await - Best Practices.

    wizend



    • Edited by Wizend Saturday, December 16, 2017 9:40 PM
    Saturday, December 16, 2017 9:36 PM
  • In C# 7.0, the compiler no longer limits async method returns to void, Task or Task<T>. You can now define custom types, such as the .NET Core Framework-provided System.Threading.Tasks.ValueTask<T> struct, which are compatible with an async method return. See itl.tc/GeneralizedAsyncReturnTypes for more information.

    Here are a few simple examples.

    public async ValueTask<int> Tiny() { await (Task)null; return 1; }
    
    public async ValueTask<bool> ImVersatile()
    {
        if (File.Exists("karen.txt"))
        {
            return true;
        }
        else
        {
            await Task.Delay(0);
            return false;
        }
    }
    public async ValueTask<int> ImVersatile1()
    {
        await Task.Delay(100);
        return 5;
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, December 16, 2017 10:45 PM
    Moderator
  • @Kareninstructor, @Wizend

    So, why this error occurs?


    There is no knowledge that is not power.

    Sunday, December 17, 2017 7:50 PM
  • @Kareninstructor, @Wizend

    So, why this error occurs?


    There is no knowledge that is not power.

    If you are using VS2017, targeting C# 7 you can use ValueTask, otherwise it's not supported in earlier versions of Visual Studio e.g. C# 6, only C#7.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by JohnyL Tuesday, December 19, 2017 5:54 AM
    Sunday, December 17, 2017 9:49 PM
    Moderator
  • You told us you're working with VS 15.6. Maybe you need to make the new C# 7.0 ValueTask feature available by adding the NuGet package 'System.Threading.Tasks.Extensions' to your project, esp., when your project is compiled in an older .NET version.

    wizend

    • Marked as answer by JohnyL Tuesday, December 19, 2017 5:54 AM
    Monday, December 18, 2017 11:36 AM
  • @Karen, @wizend

    This is really strange... Now everything works! Thanks :)


    There is no knowledge that is not power.

    Tuesday, December 19, 2017 5:54 AM