C# Cheatsheet
49 essential methods for coding interviews
Showing 49 of 49 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
List.Add | list.Add(item) | Adds an element to the end of the List<T> | O(1) amortized | essential |
List.Remove | list.Remove(item) | Removes the first occurrence of a specific object from the List<T> | O(n) | essential |
List.RemoveAt | list.RemoveAt(index) | Removes the element at the specified index | O(n) | essential |
List.Contains | list.Contains(item) | Determines whether an element is in the List<T> | O(n) | essential |
List.IndexOf | list.IndexOf(item) | Returns the zero-based index of the first occurrence of a value | O(n) | essential |
List.Sort | list.Sort() or list.Sort(comparison) | Sorts the elements in the entire List<T> using the default comparer or a custom comparison | O(n log n) | essential |
Dictionary.Add | dict.Add(key, value) or dict[key] = value | Adds the specified key and value to the dictionary | O(1) average | essential |
Dictionary.TryGetValue | dict.TryGetValue(key, out value) | Gets the value associated with the specified key without throwing if key not found | O(1) average | essential |
Dictionary.ContainsKey | dict.ContainsKey(key) | Determines whether the dictionary contains the specified key | O(1) average | essential |
Dictionary.Remove | dict.Remove(key) | Removes the value with the specified key from the dictionary | O(1) average | essential |
HashSet.Add | set.Add(item) | Adds the specified element to a HashSet<T> | O(1) average | essential |
HashSet.Contains | set.Contains(item) | Determines whether a HashSet<T> contains the specified element | O(1) average | essential |
HashSet.Remove | set.Remove(item) | Removes the specified element from a HashSet<T> | O(1) average | essential |
String.Substring | str.Substring(startIndex, length) | Retrieves a substring from this instance | O(n) | essential |
String.Split | str.Split(separator) | Splits a string into substrings based on specified delimiter | O(n) | essential |
String.Join | string.Join(separator, values) | Concatenates elements of a collection using the specified separator | O(n) | essential |
String.ToCharArray | str.ToCharArray() | Copies characters in this string to a character array | O(n) | essential |
String.Contains | str.Contains(value) | Returns whether the specified substring occurs within this string | O(n*m) | essential |
String.IndexOf | str.IndexOf(value) | Returns the zero-based index of the first occurrence of specified value | O(n*m) | essential |
String.Replace | str.Replace(oldValue, newValue) | Returns a new string with all occurrences of a specified value replaced | O(n) | essential |
StringBuilder | new StringBuilder().Append(str) | Mutable string builder for efficient string concatenation | O(1) amortized per Append | essential |
Array.Sort | Array.Sort(array) | Sorts the elements in an entire array using the default comparer | O(n log n) | essential |
Where | collection.Where(predicate) | Filters a sequence of values based on a predicate | O(n) | essential |
Select | collection.Select(selector) | Projects each element of a sequence into a new form | O(n) | essential |
Aggregate | collection.Aggregate(seed, func) | Applies an accumulator function over a sequence | O(n) | essential |
OrderBy/OrderByDescending | collection.OrderBy(keySelector) | Sorts the elements of a sequence in ascending or descending order | O(n log n) | essential |
GroupBy | collection.GroupBy(keySelector) | Groups the elements of a sequence according to a specified key selector | O(n) | essential |
First/FirstOrDefault | collection.First() or collection.FirstOrDefault() | Returns the first element of a sequence, or a default value if no element is found | O(1) | essential |
Any/All | collection.Any(predicate) or collection.All(predicate) | Determines whether any or all elements satisfy a condition | O(n) | essential |
Count | collection.Count() or collection.Count(predicate) | Returns the number of elements in a sequence, optionally filtered by predicate | O(1) for ICollection, O(n) otherwise | essential |
Sum/Max/Min/Average | collection.Sum() or collection.Max() or collection.Min() | Computes the sum, maximum, minimum, or average of a sequence of numeric values | O(n) | essential |
Math.Max/Min | Math.Max(a, b) or Math.Min(a, b) | Returns the larger or smaller of two numbers | O(1) | essential |
List.Insert | list.Insert(index, item) | Inserts an element into the List<T> at the specified index | O(n) | common |
List.BinarySearch | list.BinarySearch(item) | Searches a sorted List<T> for an element using binary search | O(log n) | common |
Dictionary.Keys/Values | dict.Keys or dict.Values | Gets a collection containing the keys or values in the dictionary | O(1) to get collection, O(n) to iterate | common |
HashSet.UnionWith | set.UnionWith(other) | Modifies the current set to contain all elements present in itself and the specified collection | O(n) | common |
HashSet.IntersectWith | set.IntersectWith(other) | Modifies the current set to contain only elements present in both itself and the specified collection | O(n) | common |
String.ToLower/ToUpper | str.ToLower() or str.ToUpper() | Returns a copy of this string converted to lowercase or uppercase | O(n) | common |
Array.Reverse | Array.Reverse(array) | Reverses the sequence of elements in the entire array | O(n) | common |
Array.BinarySearch | Array.BinarySearch(array, value) | Searches a sorted array for a value using binary search | O(log n) | common |
Array.Fill | Array.Fill(array, value) | Fills an array with the specified value | O(n) | common |
Distinct | collection.Distinct() | Returns distinct elements from a sequence | O(n) | common |
Take/Skip | collection.Take(count) or collection.Skip(count) | Returns a specified number of elements from the start or skips them | O(n) | common |
SelectMany | collection.SelectMany(selector) | Projects each element to a sequence and flattens the resulting sequences into one | O(n*m) | common |
ToDictionary | collection.ToDictionary(keySelector, valueSelector) | Creates a Dictionary from a sequence according to specified key and value selectors | O(n) | common |
ToHashSet | collection.ToHashSet() | Creates a HashSet from a sequence | O(n) | common |
Math.Abs | Math.Abs(value) | Returns the absolute value of a number | O(1) | common |
int.Parse/TryParse | int.Parse(str) or int.TryParse(str, out result) | Converts string representation of a number to an integer | O(n) | common |
char.IsLetter/IsDigit | char.IsLetter(c) or char.IsDigit(c) | Indicates whether a character is a letter or digit | O(1) | common |