Programming‎ > ‎

C#

Basic Info:

C# is case sensitive. 
Variable <> variable

Main is a static method that designates a program's entry point. 
static void Main(string[] args)
{

}

A method's content should be enclosed by curly braces { }.
private myMethod() 
    //Do Stuff 
}

Each statement should be followed by a semicolon.
string myString; 
myString = "Hello World!";
Console.WriteLine(myString);

Use double forward slash for commenting.
//This is my comment

Comment out a block of code.
/* This is a block of
commented code. It 
will not execute */

Create XML comments using triple forward slash and the <summary> and <remarks> tags
///<summary>
///This is my summary
///</summary>
///<remarks>
///I have a remark to make about that
///</remarks>

Use namespaces as a named container for your code. It's good practice to define all classes in namespaces.
namespace MyNameSpace
{
    class MyClass
    {
        public DoStuff()
        {
            \\Do Stuff
        }
    }

    class AnotherClass
    {

    }
}

MyNameSpace.MyClass.DoStuff()

Bring namespaces into scope(current file) with using 
using System;
using System.Text;

Console.WriteLine()

is less typing than

System.Console.WriteLine()

You can only use letters, numbers, and underscores for identifiers (The name of namespaces, classes, methods, and variables) and they cannot begin with a number.
Var is ok
_var is ok
var1 is ok
1var is NOT ok

A proper C# naming convention for identifiers is to use camelCase for all non-public identifiers and PascalCase or TitleCase  for public identifiers. Don't forget C# is case sensitive.
myPrivateCamelCaseVariable

MyPublicPascalCaseVariable

private myClass

public MyClass

Declare your variables with the type and name, then assign it a value.
int myInt;
string myString;

myInt = 42;
myString = "Forty Two";


You can also declare and initialize your variable in one statement.
int myInt = 42;

You can let the compiler infer the type of variable from an expression using the var keyword.
var myVariable = 42;

Initialize multiple variables at once.
int x, y, y;
string myString1, myString2;

Operators.
Normal: + - * / < > %
Equality: ==
And : &&
Or: ||

Simple True/False evaluation. If input variables equals "1" then assign the first string after the question mark to result, otherwise assign the second string to result.
string result = (input == "1") ? "You entered the number 1." : "You entered something else."

FOR loop.
for (int i = 0; i < 10 ; i++)
{
    //Do something until i is no longer less than ten. i is initially assigned the value of 0 and through each iteration of the loop 1 is added to i using i++
    // for (assignment ; evaluate and execute block of code; increment){}
}

Arrays are immutable once they are defined.
int[] i = new int[10];

int[0] = 2;
int[1] = 4;
and so on...

or

int[] i = new int[] {2,4,6,8,10,12};

Console.WriteLine(i[2].ToString());

foreach loop to use with arrays.
foreach(int number in i)
{
    //Do something with each "number"
}

Create and array of letters using char data type.
string message = "These aren't the droids you are looking for!";
char[] charArray = message.ToCharArray();
Console.WriteLine(charArray[3]);

Result
s

While loop
StreamReader myReader = new StreamReader("values.txt");
string line = "";

while (line != null)
{
    line = myReader.ReadLine();
    if (line != null)
        Console.WriteLine(line);
}
myReader.Close();
Console.ReadLine();

String manipulation.

escape character \
string myString = "c:\\folder";

Format
string myString = String.Format("{0}, {1}!", "Hello", "World");


.....