Solution:1

No - it's generally considered best practice to declare a variable as late as you can, preferably setting it at the point of declaration. The only time when I don't do that is when I have to set a variable conditionally, or it's set in a more restrictive scope:

It keeps the variable's type close to its use - no need to scroll back up in the method to find it.

It keeps the scope as narrow as possible, which makes the use of the variable more obvious.

Solution:2

I would use

XmlDocument doc = xmlDocuments[3];

Declare variables where they are used.

Solution:3

They are different styles, and neither of them is objectivily better than the other. It's just a matter of taste. You can either declare the variable first and then assign a value to it:

XmlDocument doc; doc = xmlDocuments[3];

Or you can do both in the same statement:

XmlDocument doc = xmlDocuments[3];

However, this form:

XmlDocument doc = null; doc = xmlDocuments[3];

To assign a null reference to the variable and then immediately replace it with a different reference, is totally pointless.

Solution:4

The declaration and assignment should ideally be paired for code legibility. In fact ReSharper will pick up any exceptions to this and suggest that they be joined.

Solution:5

As the others has pointed out the first style can be useful if you need to declare the variable outside the scope of a loop, if or something else. But in most cases I think this style is a remnant from the old days of Visual Basic 6 (and earlier) where you always had to declare a variable before using in.

Old VB didn't support your second style and therefore the first style is still popular...

Solution:6

If the variable 'doc' is assigned right after it is declared then no, I don't see any reason why you would want to do that. In any case, it certainly is not my style.

Solution:7

I like to think of variables as 'shared' or 'not shared' in that some variables need to be used in multiple locations in the class or method, and some you only need to use once. For the former, I declare them all at the top of the relevant block, the latter I declare just before I use them.

It is best to keep declaration, definition and initialization as "together" as possible. Option 1 is example of declaration, definition and initialization done together in one line. You could compress it further:

Solution:10

As doc may not be being used till later on in the method. It makes more sense to create doc just before it is used. Easier to read (less scrolling).

Solution:11

I'm always trying to declare and assign variables on the same row if possible, so in this case I would try to use the second option. Fewer lines of code, less declared variables to keep in mind when reading the code also. In the end I think it's all about the coding rules you and your team uses though. We've had coding rules which says to declare all local variables in the beginning of the method, so I've seen both programming styles.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com