19

I am trying to use the following method in a WPF application .NET Framework 4 Client Profile but I receive this error:

The type or namespace name 'async' could not be found

I am using

using System.Threading.Tasks;

Any idea what could be wrong? Thanks in advance

private async Task SumPageSizesAsync()
{
    HttpClient client = new HttpClient();
    Task<byte[]> getContentsTask = client.GetByteArrayAsync(url);
    byte[] urlContents = await getContentsTask;   
}

I am using VS 2010

GibboK
  • 71,848
  • 143
  • 435
  • 658

3 Answers3

26

Well, there are two things:

  • You need to be using a C# 5 compiler, e.g. VS2012. If you're using VS2010, you can't use async. Given the error message, I suspect that you're using the wrong compiler version.
  • You need to use the Microsoft.Bcl.Async NuGet package to bring in the appropriate library support for .NET 4.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    In case I use VS 2010 is this link appropriate http://www.microsoft.com/en-us/download/details.aspx?id=9983 – GibboK Jan 14 '14 at 08:26
  • 3
    @GibboK: Well it would be, but that's just a CTP, including known bugs. You should upgrade to VS2012, or accept that you won't be able to use async/await. I would *really* caution against using the CTP. – Jon Skeet Jan 14 '14 at 08:30
  • Information about Visual Studio and .NET versions which support async/await: http://openstacknetsdk.org/docs/html/32cc6156-3b31-4450-b209-b55fcfc0a210.htm – Sam Harwell Aug 04 '14 at 22:10
  • I'm not sure if this is an issue with the VS 2013 Syntax highlighting/checking, or if it is just correct. But if you create an async method in VS2013 without a return type (by mistake ofc!) it will also come up with this error when compiling and when highlighting. When what is actually wrong, is your method has no return type. – KidCode Jan 24 '15 at 12:03
  • @KidCode: It could be either - because `async` is a valid type name, so you could be trying to write a method returning that type. – Jon Skeet Jan 24 '15 at 12:22
23

In my case the return type was missing, message was cause by code:

private async button1_Click(object sender, EventArgs e)

and should be

private async void button1_Click(object sender, EventArgs e)
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
14

If you are using Visual Studio 2012+ and you stop typing the moment you get a red squiggley line, you may see this error message on the async keyword until you finish writing the method signature.

Finish writing the method signature, make sure there aren't any other compiler issues, and wait a second or so for Visual Studio to catch up.

Jason
  • 8,400
  • 10
  • 56
  • 69