0

There are may ways to initialize a variable for eg string varName; and so on. But what is the different between var appWindow = new AppWindow(); and AppWindow appWindow = new AppWindow();

Are they the same?

Can anyone please explain me the reason as I am foreign for c#.

Thanks

Ghasem
  • 14,455
  • 21
  • 138
  • 171
usrNotFound
  • 2,680
  • 3
  • 25
  • 41

2 Answers2

0

Both the codes are equivalent and the IL code for both will be same. Here is a sample to demonstrate that the IL code for both will be similar:

private static void mycompareMethod()
{
    var str1 = new String(new char[10]);
    string str2 = new String(new char[10]);
}

The IL output:

{
  .method private hidebysig static void  mycompareMethod() cil managed
  .maxstack  2
  .locals init ([0] string str1,
           [1] string str2)
  IL_0000:  nop
  IL_0001:  ldc.i4.s   9
  IL_0003:  newarr     [mscorlib]System.Char
  IL_0008:  newobj     instance void [mscorlib]System.String::.ctor(char[])
  IL_000d:  stloc.0
  IL_000e:  ldc.i4.s   9
  IL_0010:  newarr     [mscorlib]System.Char
  IL_0015:  newobj     instance void [mscorlib]System.String::.ctor(char[])
  IL_001a:  stloc.1
  IL_001b:  ret
} // end of method Program::mycompareMethod
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • This doesn't answer the op's question. It just says: " They're the same". – Jan Köhler Jan 11 '16 at 05:19
  • @khlr:- The first line of my answer says that "Both the codes are equivalent" And then I have added the code to prove that they are equivalent! Also OP has clearly mentioned `Can anyone please explain me the reason as I am foreign for c#.` – Rahul Tripathi Jan 11 '16 at 05:21
  • And he also asks "what is the different between" which isn't answered. – Jan Köhler Jan 11 '16 at 05:33
  • @khlr:- I am not sure if you are getting the question and the answer. If there is no difference then how will you make it. Please read the question and the answer carefully and then comment. Thanks! – Rahul Tripathi Jan 11 '16 at 05:57
  • Well, it's not only about the generated IL-code. Important may be the question when you should use one or the other. Meaning: Are there differences in the usage. _Why_ should/could you use var? And so on... – Jan Köhler Jan 11 '16 at 10:28
  • @khlr:- I am not gonna discuss this anymore. As I think you are simply not getting the question. The question is about "What" not "When or why or how". The generated IL code is just to add to the point as why they are equivalent when Op is asking about `What is the difference`.Thanks – Rahul Tripathi Jan 11 '16 at 11:32
  • I'm getting the question _and_ your point of view very well. What I wanna point out is that there _is_ a difference. Not in the output but in the usage. And an answer like "they are the same" comes a bit short, as the OP asks for the reason, why there are multiple ways. But anyway, have a nice day ;) – Jan Köhler Jan 11 '16 at 16:03
0

it's same, but you cannot use both at a same way. the first one var appWindow = new AppWindow(); used when the type is obvious and initialize your variable right away.

AppWindow appWindow; //using var you will get compile error
if(true)
 appWindow = _GetFromAnotherMethod();
else
 appWindow = new AppWindow();
vantian
  • 848
  • 3
  • 10
  • 25