Coding Toolkit for C#
Table of contents
Array
int[] nums = new int[n];
int[] nums = { 4, 3, 1, 2 };
nums.Length // Length of the array
for(int i = 0; i < nums.Length; i++) { nums[i] } // Iterate
Array.Sort(nums) // 1 2 3 4
Array.Reverse(nums) // 4 3 2 1
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
numbers[0, 1] // Accessing element output: 4
numbers.GetLength(0)
numbers.GetLength(1)
String
string str = "Hello Developers"
char[] arr = str.ToCharArray();
string str = new string(arr);
// Iterate and get length is same as array
if(str.IsNullOrEmpty(str))
if(str.IsNullOrWhiteSpace(str))
if(str.Equals("Hello")) // false
int firstIndex = str.IndexOf('e'); // 1
int lastIndex = str.LastIndexOf('e') // 13
str.Substring(startIndex);
str.Substring(startIndex, length)
str.Trim() // Trim leading and trailing whitespaces
str.ToLower() // "hello developers"
str.ToUpper() // "HELLO DEVELOPERS"
str.Replace('e', 'a') // "Hallo Davalopars"
int number = 42;
string str = "42"
Convert integer -> string: num.ToString() // "42"
Parse string -> integer : int.tryParse(str, out int result) // boolean
String Builder
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("Hello");
stringBuilder.Insert(2, "She"); // "HeShello"
stringBuilder.Replace("She", "") // "Hello"
string newString = stringBuilder.ToString();
List
// Create a list
List<int> integerList = new List<int>();
List<int> integerList = new List<int> { 10, 20, 30, 40, 50 };
List<int> integerList = integerArray.ToList(); // array -> list
// Iterate the list
foreach (int number in integerList) {
}
integerList.Add(42);
integerList.Add(10);
// Check whether an element exists in the list
if(integerList.Contains(30)) {
integerList.Remove(30);
}
// Get an element at particular index
int element = integerList[index];
// Convert list into array
int[] integerArray = integerList.ToArray();
Stack
System.Collections.Generic
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
Console.WriteLine("Top item:" + stack.Peek());
while(stack.Count > 0) {
int poppedItem = stack.Pop();
Console.WriteLine("Popped: " + poppedItem);
}
Queue
Queue<int> queue = new Queue<int>;
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enquque(3);
Console.WriteLine("Front item:" + stack.Peek());
int dequeuedItem = queue.Dequeue(); // 1
bool contains = queue.Contains(1) // true
int[] queueArray = queue.ToArray();
while(queue.Count > 0) {
int dequeuedItem = queue.Dequeue();
Console.WriteLine("Dequeued: " + dequeuedItem);
}
// Removes all items from queue
queue.Clear();
Dictionary
// Create a map
Dictionary<string, int> map = new Dictionary<string, int>();
// Add entries in the map
map.Add("one", 1);
map.Add("two", 2);
// Check if key exists in the map
if(map.ContainsKey("two")) {
Console.WriteLine("Value for key: " + map["tw0"]);
map["two"] = -1; // Replace value
}
int value;
if (map.TryGetValue("two", out value)){
Console.WriteLine("Value for key: " + value);
}
// Access both keys and values
foreach(var pair in map) {
Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}");
}
// Access keys
foreach(string key in map) {
Console.WriteLine(key);
}
map.Remove("one"); // Remove entry from the map
Math
Namespace: System.Math
Int32.MaxValue - Maximum value of an int data type.
Math.Abs - Absolute value of a number
Math.Round - Rounds a floating point number to the nearest integer
Math.Pow - Number raised to the specified power
Math.Min(num1, num2) - Minimum of two number