Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

?. operator and await question (potential bug) #36310

Closed
yahorsi opened this issue Jun 11, 2019 · 5 comments
Closed

?. operator and await question (potential bug) #36310

yahorsi opened this issue Jun 11, 2019 · 5 comments

Comments

@yahorsi
Copy link

yahorsi commented Jun 11, 2019

Hi Guys!

Have a look at following sample:

using System;
using System.Threading.Tasks;

namespace ConsoleApp15
{
    public class Foo
    {
        public void Bar() { }
        public async Task Bar2() { await Task.Delay(0);  return; }
    }
    class Program
    {
        static async Task Main(string[] args)
        {
            Foo foo = null;
            foo?.Bar(); // Not throws
            await foo?.Bar2(); // Throws

            Console.WriteLine("Hello World!");
        }
    }
}

Here when I call Bar I use ?. operator and expect method to not be called and it is not called and no exception thrown. Right on the next line I expect exactly the same behaviour but I do get NullReferenceException. Is that something expected? (If it is then I would say it looks very inconsistent)

VS 2019 16.1.1
C# Latest Minor

Thank you

@Andrew-Hanlon
Copy link
Contributor

Think of the offending line as being interpreted as something like Task.Await(foo?.Bar2());. Then you can see that you are indeed awaiting null. There are proposals, I believe, to add something like await? to cover this annoyance.

@yahorsi
Copy link
Author

yahorsi commented Jun 11, 2019

Think of the offending line as being interpreted as something like Task.Await(foo?.Bar2());. Then you can see that you are indeed awaiting null. There are proposals, I believe, to add something like await? to cover this annoyance.

That is correct and clear but still very inconsistent with code without await. I would personally prefer to not have await? and change current behaviour, otherwise we add one more inconsistenly on top of existing inconsistency. Like imagine you had code await? foo.Bar() and then for some reason function become sync and await has gone. Then we can't simply delete await? and have to change code from await? foo.Bar() to foo?.Bar().

Does not look good IMHO

@Andrew-Hanlon
Copy link
Contributor

imagine you had code await? foo.Bar() and then for some reason function become sync and await has gone. Then we can't simply delete await? and have to change code from await? foo.Bar() to foo?.Bar().

You would still need to write await? foo?.Bar() which would be self-consistent with or without the await.

@yahorsi
Copy link
Author

yahorsi commented Jun 11, 2019

imagine you had code await? foo.Bar() and then for some reason function become sync and await has gone. Then we can't simply delete await? and have to change code from await? foo.Bar() to foo?.Bar().

You would still need to write await? foo?.Bar() which would be self-consistent with or without the await.

Ah, now Is clear. is that something already discussed and agreed? Just interesting if this might go to any version soon?

@Andrew-Hanlon
Copy link
Contributor

Champion "Null-conditional await"
Proposal: await? (Null-aware await)

Cheers.

@yahorsi yahorsi closed this as completed Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants