Roslyn: c# 6 null-conditional operator doesn't work for await statement

Created on 17 Dec 2015  路  4Comments  路  Source: dotnet/roslyn

var body = await request.Content?.ReadAsStringAsync();

throw an nullreferenceexception when when request.Content is null instead of just returning null.

Is this an intended behaviour?

Area-Language Design Question Resolution-Answered

Most helpful comment

Proposal on a language change to address htis is here: https://github.com/dotnet/csharplang/issues/35

All 4 comments

Yes, it is intentional behavior that awaiting null results in a null reference exception.

Yes, it is intentional behavior that awaiting null results in a null reference exception.

That sucks.

I wanted to do something like this using the MS Graph SDK:

var graphClient = new GraphServiceClient(authProvider);
var users = await graphClient.Users.Request().GetAsync();

do
{
    foreach (var user in users)
    {
        Console.WriteLine($"{user.Id} : {user.GivenName} {user.Surname}");
    }
}
while ((users = await users.NextPageRequest?.GetAsync()) != null);

I guess

while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);

isn't that bad...

Proposal on a language change to address htis is here: https://github.com/dotnet/csharplang/issues/35

Was this page helpful?
0 / 5 - 0 ratings