Can't Inspect Variables When Debugging .NET As

2020-08-23 17:25发布

问题:

I have a .NET 4.5 proj that is using async/await functionality.

When i try to inspect/quickwatch a variable reference after an await statement, i get the following:

The name 'id' does not exist in the current context

Know how to fix this so i can debug it?

Edit-- here's the code

    [Fact]
    public async Task works_as_expected()
    {
        var repo = Config.Ioc.GetInstance<IAsyncRepository<Customer>>();

        var work = Config.Ioc.GetInstance<IUnitOfWork>();
        Customer c= new Customer()
        {
            FirstName = "__Micah",
            LastName = "__Smith_test",
            DateCreated = DateTime.Now,
            DateModified = DateTime.Now,
            Email = "m@m.com",
            Phone = "7245551212"
        }; 

        var id=await repo.Insert(c);
        // i can't inspect the value of id
        Assert.True(id > 0);
    }

回答1:

That variable doesn't exist yet, so there is no way to inspect it's value. It has no value (yet).

It won't be defined, technically, until you reach the line of code that defines it, and it won't have a value until you initialize it. While you can technically inspect the value of a variable before it is ever assigned in some cases, that value can never be accessed in code (outside of unsafe code) so there's little reason to look at it.

As for when the memory for that location will be allocated (which is when you first have the option of looking at it in the debugger) that will be after you have reached the first line after the last await before the variable is used.