C# Cheat Sheet
if
Conditional branching:
bool x = true;
bool y = false;
if (x)
{
Console.WriteLine("X be true... boom shakalaka");
}
else if (y)
{
Console.WriteLine("Y be true... boom shakalaka");
}
else
{
Console.WriteLine("Y be false? Best be more truthful.");
}
bool y = false;
if (x)
{
Console.WriteLine("X be true... boom shakalaka");
}
else if (y)
{
Console.WriteLine("Y be true... boom shakalaka");
}
else
{
Console.WriteLine("Y be false? Best be more truthful.");
}
foreach
int[] foreach_array = new int[] { 1, 2, 3, 4, 5};
foreach (int i in foreach_array) {
Console.WriteLine(i.ToString());
}
foreach (int i in foreach_array) {
Console.WriteLine(i.ToString());
}
Arrays
Useful Methods
- Array.Resize(array, size);
- Array.Reverse(array);
Size of Array
int[] int_array = new int[] { 1, 2, 3, 4, 5 };
Console.WriteLine(int_array.Length.ToString()); // Call Array.Length to get size.
Console.WriteLine(int_array.Length.ToString()); // Call Array.Length to get size.
Two-dimensional Arrays
Declaring and initializing:
string[,] string_array = new string[, ] {} // initialize blank
string[,] string_array2 = new string[5, 10] // initialize with dimensions 5x10
string[,] string_array2 = new string[5, 10] // initialize with dimensions 5x10
Getting the dimensions:
// 1st dimension
string_array2.GetLength(0);
// 2nd dimension
string_array2.GetLength(1);
string_array2.GetLength(0);
// 2nd dimension
string_array2.GetLength(1);
Console
Useful Methods
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.Green;
- Console.ResetColor();
- Console.SetCursorPosition(x, y);
Flush the console:
Console.Out.Flush();
Get the dimensions of the console:
// Get max X and Y co-ordinates for the Console.
int console_max_x = Console.WindowWidth;
int console_max_y = Console.WindowHeight;
int console_max_x = Console.WindowWidth;
int console_max_y = Console.WindowHeight;
Threads
All threaded code will require the proper inclusion:
using System.Threading;
Simple Thread example:
Thread some_thread = new Thread(ThreadMethod);
some_thread.Start();
private static void ThreadMethod() {
// execute this in the thread.
}
some_thread.Start();
private static void ThreadMethod() {
// execute this in the thread.
}

There are no comments on this page. [Add comment]