How do you arrange source code elements in C# 3.0?2009-04-15 c# .net oop c#-3.0
Does following look good?
Edit: Options are general in nature, may not be exhaustive in terms of C# elements.
Single Source file can contain following:
Files can come in pair - Editable + Generated Single file can have only one name-space.
- One partial or full class per file
- Zero or more enum per file
- Zero or more structures per file
- Zero or more delegate type per file
- One or more interfaces per file
- One static class per file
Within class: Option-1
There will be following sections in given Order.
Enums - Fields - Properties - Events - Delegates - Methods
Within each section, elements will be ordered by accessibility i.e. public methods will appear before private methods. Inner types can have their own section between any two sections. Optionally, related fields and properties can be grouped together.
Within class: Option-2
Group closely related elements without looking at accessibility level. Use regions without fail.
Within class: Option-3
Just do not care. Let VS help you.
What do you guys think and do?
I use File: Option-3 and a mix of "Within class: Option-1" + "Within class: Option-2" depends on the class type. If there is clear relationship then I'll go for option-2 but most of the time I stick with Option-1.
I only have a single
element per file. If you need to group things together to tidy them up, then that is what namespaces are for.
I also tend to stick fields and properties at the top of classes, followed by the constructors, then methods. I usually keep private methods next to the public ones that use them.
Edit: And under no circumstances should you use regions! ever. at all. If youre class is so big you need to collapse huge portions of it youve got far worse problems to worry about.
I usually use Option-3 for files and Option-1 within classes. Classes are structured by this regions:
- Nested Classes
- Events / Delegates
- Construction / Destruction / Finalization
I generally put types in their seperate files. (Enums, structs, classes and delegates) Nested types go in the same file as their parenting type. Partial files are only used with generated files.
Within a file, the main structure is:
- Nested classes
- Consts, fields, event and delegate fields
- Methods (related ones are close to eachother, not necessarily grouped by accessibillity.)
I'm not too strict on these rules. They're guidelines...
I also would put only one element per file. It's easier to find the elements if they are in their own file, especially in large projects.
Robert C Martin's book Clean Code provides some useful guidance on this- although the contents are for Java, I found the guidance still very applicable to .NET.
The most important thing is to pick a style and stick with. StyleCop is very useful for enforcing these rules.
- Reference - What does this error mean in PHP?
- How do you convert a byte array to a hexadecimal string, and vice versa?
- How do I calculate someone's age in C#?
- Interface vs Abstract Class (general OO)
- How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
- Order of items in classes: Fields, Properties, Constructors, Methods
- How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
- How to get the type of T from a member of a generic class or method?