# C# Cheatsheet - [PDF Download Cheatsheet](https://1drv.ms/b/s!Ai0GNI50Q5GAgdQzF5Ynuo_6GZmofg)
<div align="center">
<img src="https://i2.wp.com/programmingwithmosh.com/wp-content/uploads/2015/10/Cover.jpg" width=100%/>
</div>
<br>
<br>
<br>
# Index<br>
[Comments](#Comments)<br>
[Scopes](#Scopes)<br>
[Functions](#Functions)<br>
[Params Keyword](#Params-Keyword)<br>
[DataTypes ](#DataTypes )<br>
[Operators](#Operators)<br>
[Convert Data Types](#Convert-Data-Types)<br>
[Define Variables](#Define-Variables)<br>
[Initialise Varaiable](#Initialise-Varaiable)<br>
[Constants](#Constants)<br>
[Casting User input](#Casting-User-input)<br>
[Classes](#Classes)<br>
[Properties](#Properties)<br>
[Constructor](#Constructor)<br>
[Method Overloading](#Method-Overloading)<br>
[Class Visability](#Class-Visability)<br>
[Inheritance](#Inheritance)<br>
[Virtual and Override](#Virtual-and-Override)<br>
[Base](#Base)<br>
[Abstract Class](#Abstract-Class)<br>
[Abstract methods](#Abstract-methods)<br>
[Interfaces](#Interfaces)<br>
[Partial Classes](#Partial-Classes)<br>
[Collections](#Collections)<br>
[Arrays](#Arrays)<br>
[ForLoop](#ForLoop)<br>
[ForEach](#ForEach)<br>
[Lists](#Lists)<br>
[Dictionaries](#Dictionaries)<br>
[Strings](#Strings)<br>
[Common String Methods](#Common-String-Methods)<br>
[Structs or Structures](#Structs-or-Structures)<br>
[Enums](#Enums)<br>
[Read File](#Read-File)<br>
[Write File](#Write-File)<br>
[Create a File](#Create-File)<br>
## Comments
```c#
// My comments about the class name could go here...
// My comments about the class name could go here...
// Add as many lines as you would like
// ...Seriously!
/*
My comments about the class name could go here...
Add as many lines of comments as you want
...and use indentation, if you want to!
*/
```
## Commented Tasks highlighter
```c#
//TODO: Change "world" to "universe"
//HACK: Don't try this at home....
//NOTE: Don't try this at home....
//UNDONE: Don't try this at home....
```
<br>
[back to top](#Index)<br>
<br>
# Scopes
Block/Function Scoped
Class Scoped
```c#
using System;
namespace VariableScope
{
class Program
{
private static string helloClass = "Hello, class!";
static void Main(string[] args)
{
string helloLocal = "Hello, local!";
Console.WriteLine(helloLocal); // Hello local
Console.WriteLine(Program.helloClass); // Hello Class
DoStuff();
}
static void DoStuff()
{
Console.WriteLine("A message from DoStuff: " + Program.helloClass); // Hello Class
}
}
}
```
<br>
[back to top](#Index)<br>
<br>
# Functions
## Syntax
```c#
<visibility> <return type> <name>(<parameters>)
{
<function code>
}
```
```If you don't define any, then the function will be private```
```void``` means it returns nothing.
### Example
```c#
public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
```
## Params Keyword
We can create a function and pass parameters like this
```c#
static void GreetPersons(string[] names) { }
```
However, calling it would be a bit clumsy. In the shortest form, it would look like this:
```c#
GreetPersons(new string[] { "John", "Jane", "Tarzan" });
```
By Adding the keyword params we can call it like this
```c#
static void GreetPersons(params string[] names) { }
```
And call it like this,
```c#
GreetPersons("John", "Jane", "Tarzan");
```
Another advantage of using the params approach, is that you are allowed to pass ```zero parameters``` to it as well.
<br>
[back to top](#Index)<br>
<br>
## DataTypes
| Type | Description |Examples |
| ------------- |:-------------:| -----:|
| `int ` | Integer (whole numbers) | 103, 12, 5168|
| `double` | 64 bit floating-point number | 3.14, 3.4e38|
| `Float` | Floating-point number | 3.14, 3.4e38 |
| `Decimal` | Decimal number (higher precision) | 1037.196543 |
| `Bool` | Boolean | true, False|
| `String` | String | "Hello World" |
| `byte` | 8-bit unsigned integer | 0 to 255 |
| `char` | 16-bit Unicode character | "A" |
| `long` | 64-bit signed integer type | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
<br>
[back to top](#Index)<br>
<br>
## Operators
| Operator | Description |Examples |
| ------------- |:-------------:| -----:|
| `=` | Assigns a value to a variable.| i=6
| `+` | Adds a value or variable. | i=5+5 |
| `-` | Subtracts a value or variable. | i=5-5 |
| `*` | Multiplies a value or variable. | i=5*5
| `/` | Divides a value or variable.| i=5/5
| `+=` | Increments a variable. | i += 1|
| `-=` | Decrements a variable. | i -= 1 |
| `==` | Equality. Returns true if values are equal. | if (i==10) |
| `!=` |Inequality. Returns true if values are not equal. | if (i!=10)
| `<` | Less Than | if (i<10)
| `<=` | Greater Than | if (i>10)|
| `>=` | Less Than or Equal to | if (i<=10) |
| `+` | Adding strings (concatenation). | "Hello " + "World" |
| `.` | Dot. Separate objects and methods. | DateTime.Hour
| `()` |Parenthesis. Groups values. | (i+5)
| `()` | Parenthesis. Passes parameters. | x=Add(i,5)
| `[]` | Brackets. Accesses values in arrays or collections. | name[3]
| `!` | Not. Reverses true or false. | if (!ready)|
| `&&` | Logical AND. | if (ready && clear) |
| `||` | Logical OR. | if (ready || clear) |
<br>
[back to top](#Index)<br>
<br>
## Convert Data Types
| Method | Description |Examples |
| ------------- |:-------------:| -----:|
|AsInt(),<br> IsInt() | Converts a string to an integer. | ``` if (myString.IsInt())```<br> ```{myInt=myString.AsInt();```|
|AsFloat(), IsFloat()| Converts a string to a floating-point number.|```if (myString.IsFloat())```<br>```{myFloat=myString.AsFloat();}```|
|AsDecimal(), IsDecimal()| Converts a string to a decimal number..|```if (myString.IsDecimal())```<br>```{myDec=myString.AsDecimal();}```|
|AsDateTime(), IsDateTime()|Converts a string to an ASP.NET DateTime type.| ```myString="10/10/2012";```<br>``` myDate=myString.AsDateTime();```|
|AsBool(),<br> IsBool()|Converts a string to a Boolean..| ```myString="True";```<br> ```myBool=myString.AsBool();```|
|ToString()|Converts any data type to a string.| ```myInt=1234;```<br> ```myString=myInt.ToString();```|
<br>
[back to top](#Index)<br>
<br>
## Define Variables
int i, j, k;
char c, ch;
float f, salary;
double d;
| Type | Name |
| ------------- |:-------------:|
|```int```|i, j, k;|
|```char```|c, ch;|
|```float```|f, salary;|
|```double```|d;|
## Initialise Varaiable
```c#
variable_name = value;
```
You can also initialize a varaible at the same time you define it.
```c#
int d = 3, f = 5; /* initializing d and f. */
byte z = 22; /* initializes z. */
double pi = 3.14159; /* declares an approximation of pi. */
char x = 'x'; /* the variable x has the value 'x'. */
```
## Constants
```c#
// const <data_type> <constant_name> = value;
const double pi = 3.14159;
```
<br>
[back to top](#Index)<br>
<br>
## Casting User input
```c#
int num;
num = Convert.ToInt32(Console.ReadLine());
```
## ? == Nullable Types
C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.
```c#
// < data_type> ? <variable_name> = null;
int? num1 = null;
```
<br>
[back to top](#Index)<br>
<br>
# Classes
* A Class is a group of related methods and variables.
* On this object, you use the defined methods and variables.
* You can create as many instances of your class as you want
```c#
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Declare a variable of type Car
Car car;
// Creates an new instance
car =