Home
MyClass.cs
1    /******************************************************************************
2        Style Guidelines:
3            * Readability - code is easy to read and to understand
4            * Standardisation - all team members are aligned with same code style
5            * Self-descriptive code - code is easy to understand without comments
6            * Debuggable - code is easy to debug (IDE friendly)
7     ******************************************************************************/
8    
9    // using (imports) of other namespaces will be wrapped with region "Using" 
10   #region Using
11   using System;
12   using MyNameSpace.Delegates;
13   using MyNameSpace.Interfaces;
14   #endregion
15   
16   // types are defined within the scope of namespace
17   // space name starts with Upper case letter
18   // each following word starts with upper Case letter
19   // sub-spaces are divided by '.' (dot)
20   // here "MyNameSpace" is main space and "Classes" is sub-space
21   namespace MyNameSpace.Classes
22   { // <- opening parenthesis, new line after namespace name
23       /// <summary>
24       /// type name starts with Upper case letter
25       /// each following word starts with Upper case letter
26       /// <remarks>
27       /// type = class, enum, struct, delegate. interface, etc
28       /// </remarks>
29       /// </summary>
30       public class MyCodeStyle : IMyInterface
31       { // <- opening parenthesis, new line after type name
32           /// <summary>
33           /// private variable name starts with "_" (underscore) character
34           /// and with lower case letter afterwards
35           /// each following word starts with Upper case letter
36           /// </summary>
37           private int _myInt;
38   
39           /// <summary>
40           /// protected variable name starts with Upper case letter
41           /// each following word starts with Upper case letter
42           /// </summary>
43           protected float MyFloat;
44   
45           /// <summary>
46           /// type scope const name starts with Upper case letter
47           /// each following word starts with Upper case letter
48           /// </summary>
49           public const string MyConstString = "Hello My Style!";
50   
51           /// <summary>
52           /// property name starts with Upper case letter
53           /// each following word starts with Upper case letter
54           /// </summary>
55           public int MyIntProperty
56           { // <- opening parenthesis, new line after property name
57               // if simple return, can save line space
58               // alternative syntax: get => _myInt;
59               get { return _myInt; }
60               // if public setter is not necessary, private or protected will be used here
61               private set
62               { // <- opening parenthesis, new line after set/get instruction
63   
64                   // conditional operator '?' usage 
65                   _myInt = value >= 0 ? value : 0;
66   
67                   // similar code for easier debugging and future extension
68                   // with parenthesis
69                   if (value >= 0)
70                   {
71                       _myInt = value;
72                   }
73                   else
74                   {
75                       _myInt = 0;
76                   }
77   
78                   // similar code for easier debugging and future extension
79                   // without parenthesis
80                   if (value >= 0)
81                       _myInt = value;
82                   else
83                       _myInt = 0;
84               } // <- closing parenthesis, new line after last set/get code line
85           } // <- closing parenthesis, new line after last property code line
86   
87           /// <summary>
88           /// public event name starts with Upper case letter
89           /// each following word starts with Upper case letter
90           /// <remarks>
91           /// this is simplified style of declaration,
92           /// in case no logic in adder/remover is required
93           /// </remarks> 
94           /// </summary>
95           public event MyDelegate MyEvent;
96   
97           /// <summary>
98           /// private event/delegate name starts with "_" (underscore) character
99           /// and with lower case letter afterwards
100          /// each following word starts with Upper case letter
101          /// </summary>
102          private event MyDelegate _myEvent;
103  
104          // alternative syntax to prevent null ref. exc.
105          // private event MyDelegate _myEvent = delegate(int value) {  };
106  
107          /// <summary>
108          /// public event name starts with Upper case letter
109          /// each following word starts with Upper case letter
110          /// <remarks>
111          /// this style uses expression body style with '=>' operator
112          /// code block 'wraps' access for '_myEvent'
113          /// </remarks> 
114          /// </summary>
115          public event MyDelegate MyEventWrapper1
116          {
117              add => _myEvent += value;
118              remove => _myEvent -= value;
119          }
120  
121          /// <summary>
122          /// public event name starts with Upper case letter
123          /// each following word starts with Upper case letter
124          /// <remarks>
125          /// this style uses parenthesis
126          /// code block 'wraps' access for '_myEvent' and add logic code
127          /// </remarks> 
128          /// </summary>
129          public event MyDelegate MyEventWrapper2
130          {
131              add
132              {
133                  // exception throwing code blocked wrapped with parenthesis to allow better debugging
134                  if (value == null)
135                  {
136                      throw new NullReferenceException("Null value cannot be accepted");
137                  }
138                  _myEvent += value;
139              }
140              remove
141              {
142                  // short code block, but not debugging friendly
143                  _myEvent -= 
144                      value ?? 
145                      // throwing exception here is not a good practice
146                      // see next block with better example
147                      throw new NullReferenceException("Null value cannot be accepted");
148  
149                  // alternative: exception throwing code blocked wrapped with parenthesis to allow better debugging
150                  if (value == null)
151                  {
152                      throw new NullReferenceException("Null value cannot be accepted");
153                  }
154                  _myEvent -= value;
155              }
156          }
157  
158          /// <summary>
159          /// method name starts with Upper case letter
160          /// each following word starts with Upper case letter
161          /// </summary>
162          /// <param name="numericValue">
163          /// parameter name starts with lower case letter
164          /// each following word starts with Upper case letter
165          /// </param>
166          public void MyMethod(int numericValue)
167          { // <- opening parenthesis, new line after method name
168              // TODO comment
169              // BUG comment
170              // NOTE comment
171          } // <- closing parenthesis, new line after last method code line
172  
173          /// <summary>
174          /// function name starts with Upper case letter
175          /// each following word starts with Upper case letter
176          /// </summary>
177          /// <param name="worldName">
178          /// parameter name starts with lower case letter
179          /// each following word starts with Upper case letter
180          /// </param>
181          /// <returns></returns>
182          protected string MyFunction(string worldName)
183          {
184              // new way: string interpolation with '$' operator (predicate)
185              var str1 = $"Hello {worldName}!";
186  
187              // old way: use string.Format(...) function to interpolate strings
188              var str2 = string.Format("Hello {0}!", worldName);
189  
190              return str1 + str2;
191          }
192      } // <- closing parenthesis, new line after last type code line
193  } // <- closing parenthesis, new line after last space code line