## C# Best practices, Design Patterns & Test Driven Development (TDD)
## Setup
<ahref='http://www.linqpad.net/'target='_blank'rel='nofollow'>LinqPad</a> is an .NET scratchpad to quickly test your C# code snippets.The standard edition is free and a perfect tool for beginners to execute language statements, expressions and programs.
Alternatively, you could also download <ahref='https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx'target='_blank'rel='nofollow'>Visual Studio Community 2015</a> which is an extensible <ahref='https://en.wikipedia.org/wiki/Integrated_development_environment'target='_blank'rel='nofollow'>IDE</a> used by most professionals for creating enterprise applications.
Every C# console application must have a <ahref='https://msdn.microsoft.com/en-gb/library/acy3edy3.aspx'target='_blank'rel='nofollow'>Main method</a> which is the entry point of the program.
Edit <ahref='https://dotnetfiddle.net/kY7QRm'target='_blank'rel='nofollow'>HelloWorld</a> in .NET Fiddle, a tool inspired by <ahref='http://jsfiddle.net'target='_blank'rel='nofollow'>JSFiddle</a> where you can alter the code snippets and check the output for yourself. Note, this is just to share and test the code snippets, not to be used for developing applications.
If you are using visual Studio, follow this <ahref='https://msdn.microsoft.com/en-us/library/k1sx6ed2.aspx'target='_blank'rel='nofollow'>tutorial</a> to create console application and understand your first C# program.
## Types and Variables
C# is a strongly typed language. Every variable has a type. Every expression or statement evaluates to a value. There are two kinds of types in C#
* Value types
* Reference types.
**Value Types** : Variables that are value types directly contain values. Assigning one value type variable to another copies the contained value.
<ahref='https://dotnetfiddle.net/JCkTxb'target='_blank'rel='nofollow'>Edit in .NET Fiddle</a>
Note that in other dynamic languages this could be different, but in C# this is always a value copy. When value type is created, a single space most likely in <ahref='http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html#orgheadline2'target='_blank'rel='nofollow'>stack</a> is created, which is a "LIFO" (last in, first out) data structure. The stack has size limits and memory operations are efficient. Few examples of built-in data types are `int, float, double, decimal, char and string`.
_Float_ | `float fooFloat = 314.5f;` | Precision: **7 digits**.**F is used to specify that this variable value is of type float**
_Decimal_ | `decimal fooDecimal = 23.3m;` | Precision: **28-29 digits**.Its more precision and smaller range makes it appropriate for **financial and monetary calculations**
_Char_ | `char fooChar = 'Z';` | A single **16-bit Unicode character**
_String_ | `string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs);` | **A string of Unicode characters.**
For complete list of all built-in data types see <ahref='https://msdn.microsoft.com/en-us/library/ms228360'target='_blank'rel='nofollow'>here</a>
<ahref='https://msdn.microsoft.com/en-us/library/490f96s2.aspx'target='_blank'rel='nofollow'>**Reference types**</a> : Variables of reference types store references to their objects, which means they store the address to the location of data on the <ahref='http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html#orgheadline2'target='_blank'rel='nofollow'>stack</a>, also known as pointers. Actual data is stored on the <ahref='http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html#orgheadline3'target='_blank'rel='nofollow'>heap</a> memory. Assigning reference type to another doesn't copy the data, instead it creates the second copy of reference which points to the same location on the heap.
In heap, objects are allocated and deallocated in random order that is why this requires the overhead of memory management and <ahref='https://msdn.microsoft.com/en-us/library/hh156531(v=vs.110'target='_blank'rel='nofollow'>garbage collection</a>.aspx).
Unless you are writing <ahref='https://msdn.microsoft.com/en-us/library/t2yzs44b.aspx'target='_blank'rel='nofollow'>unsafe code</a> or dealing with <ahref='https://msdn.microsoft.com/en-us/library/sd10k43k(v=vs.100'target='_blank'rel='nofollow'>unmanaged code</a>.aspx), you don't need to worry about the lifetime of your memory locations. .NET compiler and CLR will take care of this, but it's still good to keep this mind in order to optimize performance of your applications.
More information <ahref='http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91'target='_blank'rel='nofollow'>here</a>
## Flow Control Statements
*<ahref='https://msdn.microsoft.com/en-us/library/5011f09h.aspx'target='_blank'rel='nofollow'>If else</a> statement : <ahref='https://dotnetfiddle.net/IFVB33'target='_blank'rel='nofollow'>Edit in .NET Fiddle</a>