Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

code cleanup: should fields, variables and properties be declared at the top or bottom of a class? [closed]

Is it pretty common to declare it near the bottom in C# code?

I seen few example where its done that way.

There are tools like Resharper which help you organize your source code, does it have an option to specify where it should create regions for me?

like image 333
VoodooChild Avatar asked Jul 13 '11 22:07

VoodooChild


People also ask

What is a property in c# as it relates to classes and objects?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they're public data members, but they're special methods called accessors.

What is field in C# with example?

A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type. A class or struct may have instance fields, static fields, or both. Instance fields are specific to an instance of a type.

What is private set in C#?

An immutable class is a class that doesn't change once it's created, so private setters (or no setters at all) is needed to protect the properties. Private setters came into more frequent use with the property shorthand that was instroduced in C# 3.

What is public class in C#?

Public, in C#, is a keyword used to declare the accessibility of a type and type member such that the access is not limited. It is one of the access modifiers that provides complete visibility to all types and type members.


1 Answers

This is a stylistic discussion because with the majority of compilers the placement at the top or bottom (of a class file) doesn't affect the results. Historically a lot of people do it at the top, but I like to leave them at the bottom.

Why?

Because once I've declared that member variable, I don't need to see it anymore - it shows up in my intellisense, so I don't need it in my face. When you open a file, you see the top - I want to see my code straight away, not a bunch of variable declarations. This is especially relevant because variable definitions are not a natively collapsible region in the VS editor, so if they're at the top you are forced to scroll past them. If I need to jump to it then Visual Studio's F12 or just a Ctrl+End will take me there.

This is a style that some may find hard to deal with initially, but it does grow on you quite quickly. This is a particularly good approach on files that are more mature. You will also find that if you are using a plugin like ReSharper it is smart enough to put generated declarations with all the others - which means if you have them at the bottom that's where ReSharper will put it. Of course this can get messy if you have multiple classes in a file, but if you do then variable definition placement is the least of your stylistic issues.

Edit:

at the risk of drifting off topic, a comment on using a #region block instead: I use regions all the time, I love them because they help me collapse code down out of the way. However using them requires discipline as it is easy for non-related code to make its way inside the region. How many times have you looked for code only to find it buried in a #region where it didn't belong?

like image 61
slugster Avatar answered Sep 17 '22 21:09

slugster