tags:

views:

64

answers:

3

I saw multiple examples in MSDN that uses to declare the internal fields at the end of the class. What is the point?

I find this a little embarrassing, because each time Visual Studio adds a method it adds it to the end of the class, so there is need every time to move it...

class A
{
  public A(){}
  // Methods, Properties, etc ...

  private string name;
}

class A
{
  private string name;

  public A(){}  
  // Methods, Properties, etc ...
}
+2  A: 

If it's obvious the variable has been declared, and the code is by way of an example, then arguably this gets you to the bit being demonstrated quicker - that's all I can think of.

Add-ins like ReSharper will allow you to standardise and automatically apply this layout at the touch of a key combination, by the way, if it is what you want.

David M
so, you think in MSDN it was used for demonstration purpose only, without any "hidden crafty" logic bellow.
serhio
That's my guess
David M
+3  A: 

In C++, it makes sense to put the public interface of the class at the top, so that any user of the class can open up your header file and quickly see what's available. By implication, protected and private members are put at the bottom.

In C# and Java, where interface and implementation are completely intertwined, people would probably not open your class's source code to see what's available. Instead they would rely on code completion or generated documentation. In that case, the ordering of the class members is irrelevant.

Thomas
A: 

I don't think there is any valid reason for this. If you run Code Analysis on a class declared like this you'll get an error as private fields should be declared on top of classes (and below constants).

Anero