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?
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
#regionblock 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#regionwhere it didn’t belong?