0

In various articles I see 2 different notations which look very similar and appear to produce the exact same results.

var doc = XDocument.Load("Settings.xml");
XDocument doc = XDocument.Load("Settings.xml");

What is the difference between them, if any? and which is preferred?

Joey Joystick
  • 145
  • 1
  • 11
  • 1
    There's no difference, with `var` the compiler automatically deduces the type from the expression on the right (type inference). That saves you from writing out `XDocument` as the variable type. See https://msdn.microsoft.com/en-us/library/bb384061.aspx. – Maximilian Gerhardt Oct 29 '16 at 20:06
  • @Maximilian. Clear. Thanks a lot. – Joey Joystick Oct 29 '16 at 20:13
  • 1
    @Gilad. Thanks for pointing that out. Must have missed it. Shall I remove this post? – Joey Joystick Oct 29 '16 at 21:13
  • @JoeyJoystick - No :) I think having duplicate questions that refer back to the "original" are good - make search easier. I do think that editing the title of this question to specify that you are asking `XDocument` compared to `var` is important though – Gilad Green Oct 29 '16 at 21:16

0 Answers0