Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 88ffa62

Browse files
committed
Merge pull request ktaranov#1 from Darja1989/patch-1
Create C# Coding Standards and Naming Conventions
2 parents aed14f7 + 67c0940 commit 88ffa62

File tree

1 file changed

+374
-0
lines changed

1 file changed

+374
-0
lines changed
Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
## C# Coding Standards and Naming Conventions ##
2+
3+
4+
| SQL Server Object Name | Notation | Length | Plural | Prefix | Suffix | Abbreviation | Char Mask |Underscores |
5+
| ------------------------- | ---------- | ------ | ------ | ------ | ------ | ------------ | -------------------|-------------|
6+
| Class name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No |
7+
| Constructor name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No |
8+
| Method name | PascalCase | 128 | Yes | No | No | No | [A-z][0-9] | No |
9+
| Method arguments | CamelCase | 128 | Yes | No | No | Yes | [A-z][0-9] | No |
10+
| Local variables | CamelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
11+
| Constants name | PascalCase | 50 | No | No | No | No | [A-z][0-9] | No |
12+
| Field name | CamelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | Yes |
13+
| Properties name | PascalCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
14+
| Delegate name | PascalCase | 128 | No | No | Yes | Yes | [A-z] | No |
15+
| Enum type name | PascalCase | 128 | Yes | No | No | No | [A-z] | No |
16+
17+
#### 1. Do use PascalCasing for class names and method names:
18+
19+
```csharp
20+
public class ClientActivity
21+
{
22+
public void ClearStatistics()
23+
{
24+
//...
25+
}
26+
public void CalculateStatistics()
27+
{
28+
//...
29+
}
30+
}
31+
```
32+
33+
***Why: consistent with the Microsoft's .NET Framework and easy to read.***
34+
35+
#### 2. Do use camelCasing for method arguments and local variables:
36+
37+
```csharp
38+
public class UserLog
39+
{
40+
public void Add(LogEvent logEvent)
41+
{
42+
int itemCount = logEvent.Items.Count;
43+
// ...
44+
}
45+
}
46+
}
47+
```
48+
49+
***Why: consistent with the Microsoft's .NET Framework and easy to read.***
50+
51+
#### 3. Do not use Hungarian notation or any other type identification in identifiers
52+
```csharp
53+
// Correct
54+
int counter;
55+
string name;
56+
// Avoid
57+
int iCounter;
58+
string strName;
59+
```
60+
61+
***Why: consistent with the Microsoft's .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general you want to avoid type indicators in any identifier.***
62+
63+
#### 4. Do notuse Screaming Caps for constants or readonly variables
64+
```csharp
65+
// Correct
66+
public static const string ShippingType = "DropShip";
67+
// Avoid
68+
public static const string SHIPPINGTYPE = "DropShip";
69+
```
70+
71+
***Why: consistent with the Microsoft's .NET Framework. Caps grap too much attention.***
72+
73+
#### 5. Use meaningful names for variables. The following example uses seattleCustomers for customers who are located in Seattle:
74+
```csharp
75+
var seattleCustomers = from cust in customers
76+
where cust.City == "Seattle"
77+
select cust.Name;
78+
```
79+
80+
***Why: consistent with the Microsoft's .NET Framework and easy to read.***
81+
82+
#### 6. Avoid using Abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri.
83+
```csharp
84+
// Correct
85+
UserGroup userGroup;
86+
Assignment employeeAssignment;
87+
// Avoid
88+
UserGroup usrGrp;
89+
Assignment empAssignment;
90+
// Exceptions
91+
CustomerId customerId;
92+
XmlDocument xmlDocument;
93+
FtpHelper ftpHelper;
94+
UriPart uriPart;
95+
```
96+
97+
***Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations.***
98+
99+
#### 7. Do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)
100+
```csharp
101+
HtmlHelper htmlHelper;
102+
FtpTransfer ftpTransfer;
103+
UIControl uiControl;
104+
```
105+
106+
***Why: consistent with the Microsoft's .NET Framework. Caps would grap visually too much attention.***
107+
108+
#### 8. Do not use Underscores in identifiers. Exception: you can prefix private static variables with an underscore:
109+
```csharp
110+
// Correct
111+
public DateTime clientAppointment;
112+
public TimeSpan timeLeft;
113+
// Avoid
114+
public DateTime client_Appointment;
115+
public TimeSpan time_Left;
116+
// Exception (Class field)
117+
private DateTime _registrationDate;
118+
```
119+
120+
***Why: consistent with the Microsoft's .NET Framework and makes code more natural to read (without 'slur'). Also avoids underline stress (inability to see underline).***
121+
122+
#### 9. Do use predefined type names instead of system type names like Int16, Single, UInt64, etc
123+
```csharp
124+
// Correct
125+
string firstName;
126+
int lastIndex;
127+
bool isSaved;
128+
// Avoid
129+
String firstName;
130+
Int32 lastIndex;
131+
Boolean isSaved;
132+
```
133+
134+
***Why: consistent with the Microsoft's .NET Framework and makes code more natural to read.***
135+
136+
#### 10. Do use implicit type var for local variable declarations. Exception: primitive types (int, string, double, etc) use predefined names.
137+
138+
```csharp
139+
var stream = File.Create(path);
140+
var customers = new Dictionary();
141+
// Exceptions
142+
int index = 100;
143+
string timeSheet;
144+
bool isCompleted;
145+
```
146+
147+
***Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.***
148+
149+
#### 11. Do use noun or noun phrases to name a class.
150+
151+
```csharp
152+
public class Employee
153+
{
154+
}
155+
public class BusinessLocation
156+
{
157+
}
158+
public class DocumentCollection
159+
{
160+
}
161+
```
162+
163+
***Why: consistent with the Microsoft's .NET Framework and easy to remember.***
164+
165+
#### 12. Do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
166+
```csharp
167+
public interface IShape
168+
{
169+
}
170+
public interface IShapeCollection
171+
{
172+
}
173+
public interface IGroupable
174+
{
175+
}
176+
```
177+
178+
***Why: consistent with the Microsoft's .NET Framework.***
179+
180+
#### 13. Do name source files according to their main classes. Exception: file names with partial classes reflect their source or purpose, e.g. designer, generated, etc.
181+
182+
```csharp
183+
// Located in Task.cs
184+
public partial class Task
185+
{
186+
//...
187+
}
188+
// Located in Task.generated.cs
189+
public partial class Task
190+
{
191+
//...
192+
}
193+
```
194+
195+
***Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.***
196+
197+
#### 14. Do organize namespaces with a clearly defined structure:
198+
199+
```csharp
200+
// Examples
201+
namespace Company.Product.Module.SubModule
202+
namespace Product.Module.Component
203+
namespace Product.Layer.Module.Group
204+
```
205+
206+
***Why: consistent with the Microsoft's .NET Framework. Maintains good organization of your code base.***
207+
208+
209+
#### 15. Do vertically align curly brackets:
210+
```csharp
211+
// Correct
212+
class Program
213+
{
214+
static void Main(string[] args)
215+
{
216+
}
217+
}
218+
```
219+
220+
***Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.***
221+
222+
#### 16. Do declare all member variables at the top of a class, with static variables at the very top.
223+
```csharp
224+
// Correct
225+
public class Account
226+
{
227+
public static string BankName;
228+
public static decimal Reserves;
229+
public string Number {get; set;}
230+
public DateTime DateOpened {get; set;}
231+
public DateTime DateClosed {get; set;}
232+
public decimal Balance {get; set;}
233+
// Constructor
234+
public Account()
235+
{
236+
// ...
237+
}
238+
}
239+
```
240+
***Why: generally accepted practice that prevents the need to hunt for variable declarations.***
241+
242+
#### 17. Do use singular names for enums. Exception: bit field enums.
243+
```csharp
244+
// Correct
245+
public enum Color
246+
{
247+
Red,
248+
Green,
249+
Blue,
250+
Yellow,
251+
Magenta,
252+
Cyan
253+
}
254+
// Exception
255+
[Flags]
256+
public enum Dockings
257+
{
258+
None = 0,
259+
Top = 1,
260+
Right = 2,
261+
Bottom = 4,
262+
Left = 8
263+
}
264+
```
265+
***Why: consistent with the Microsoft's .NET Framework and makes the code more natural to read. Plural flags because enum can hold multiple values (using bitwise 'OR').***
266+
267+
#### 18. Do not explicitly specify a type of an enum or values of enums (except bit fields)
268+
```csharp
269+
// Don't
270+
public enum Direction : long
271+
{
272+
North = 1,
273+
East = 2,
274+
South = 3,
275+
West = 4
276+
}
277+
// Correct
278+
public enum Direction
279+
{
280+
North,
281+
East,
282+
South,
283+
West
284+
}
285+
```
286+
***Why: can create confusion when relying on actual types and values.***
287+
288+
#### 19. Do not use an "Enum" suffix in enum type names.
289+
```csharp
290+
// Don't
291+
public enum CoinEnum
292+
{
293+
Penny,
294+
Nickel,
295+
Dime,
296+
Quarter,
297+
Dollar
298+
}
299+
// Correct
300+
public enum Coin
301+
{
302+
Penny,
303+
Nickel,
304+
Dime,
305+
Quarter,
306+
Dollar
307+
}
308+
```
309+
***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.***
310+
311+
#### 20. Do not use "Flag" or "Flags" suffixes in enum type names.
312+
```csharp
313+
//Don't
314+
[Flags]
315+
public enum DockingsFlags
316+
{
317+
None = 0,
318+
Top = 1,
319+
Right = 2,
320+
Bottom = 4,
321+
Left = 8
322+
}
323+
//Correct
324+
[Flags]
325+
public enum Dockings
326+
{
327+
None = 0,
328+
Top = 1,
329+
Right = 2,
330+
Bottom = 4,
331+
Left = 8
332+
}
333+
```
334+
***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.***
335+
336+
#### 21. Do use suffix EventArgs at creation of the new classes comprising the information on event:
337+
```csharp
338+
// Correct
339+
public void BarcodeReadEventArgs : System.EventArgs
340+
```
341+
***Why: consistent with the Microsoft's .NET Framework and easy to read.***
342+
343+
#### 22. Do name event handlers (delegates used as types of events) with the "EventHandler" suffix, as shown in the following example:
344+
```csharp
345+
public delegate void ReadBarcodeEventHandler(object sender, ReadBarcodeEventArgs e);
346+
```
347+
***Why: consistent with the Microsoft's .NET Framework and easy to read.***
348+
349+
#### 23. Do not create names of parametres in methods (or constructors) which differ only the register:
350+
```csharp
351+
// Avoid
352+
private void MyFunction(string name, string Name)
353+
```
354+
***Why: consistent with the Microsoft's .NET Framework and easy to read, and also excludes possibility of occurrence of conflict situations.***
355+
356+
#### 24. DO use two parameters named sender and e in event handlers. The sender parameter represents the object that raised the event. The sender parameter is typically of type object, even if it is possible to employ a more specific type.
357+
358+
***Why: consistent with the Microsoft's .NET Framework***
359+
360+
***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.***
361+
362+
#### 25. Do use suffix Exception at creation of the new classes comprising the information on exception:
363+
```csharp
364+
// Correct
365+
public void BarcodeReadException : System.Exception
366+
```
367+
***Why: consistent with the Microsoft's .NET Framework and easy to read.***
368+
369+
## Offical Reference
370+
371+
1. [MSDN General Naming Conventions](http://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx)
372+
2. [DoFactory C# Coding Standards and Naming Conventions](http://www.dofactory.com/reference/csharp-coding-standards)
373+
3. [MSDN Naming Guidelines](http://msdn.microsoft.com/en-us/library/xzf533w0%28v=vs.71%29.aspx)
374+
4. [MSDN Framework Design Guidelines](http://msdn.microsoft.com/en-us/library/ms229042.aspx)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /