What is the difference between using is with a specific type and var?

145 Views Asked by At

What is the difference between these 2 code snippets? One with the var works but the one with the type string gives an error.(use of unassigned variable error) Aren't they both unassigned until runtime? I am quite confused about how and where these variables get assigned. Struggling with making a distinction between runtime and compile time calculations. 1st code:

 object x = "Message";
 bool result = x is string o;
 Console.WriteLine(o);

2nd code:

 object x = "Message";
 bool result = x is var o;
 Console.WriteLine(o);

2nd one works, 1st one gives the mistake I have mentioned before.

0

There are 0 best solutions below