C# Array and String Methods Cheat Sheet
Array Methods
- Array.IndexOf(): Finds index of exact match (case-sensitive, only first match)
- Array.Copy(): Copies values (shallow copy for objects; size must be known)
- Array.Sort(): Sorts array A-Z (modifies original, nulls come first)
- Array.Reverse(): Reverses order (entire array only, copy before use)
- Array.Clear(): Resets values to default (null/0), doesn't shrink array
- Array.Find(): Finds first matching item with predicate
- Array.Exists(): Checks if any item matches condition
- Array.Resize(): Changes size with 'ref'; retains values
- Clone(): Shallow copy (like Array.Copy)
String + Array Methods
- ToCharArray(): Converts string to char[] (for reversing/encrypting)
- Split(delimiter): Splits string into string[] (CSV/word parsing)
- String.Join(): Joins array to string with delimiter
- new string(char[]): Rebuilds string from char[]
Shallow vs Deep Copy
- Shallow Copy: Only references are copied (changes reflect in original)
- Deep Copy: Clone each object manually
Example:
copy[i] = new TaskItem {
Title = original[i].Title,
IsCompleted = original[i].IsCompleted
};
Common Beginner Mistakes
- Sorting original: Always copy before sorting
- Split() for characters: Use ToCharArray instead
- Clear() shrinks array: It does not; use Resize
- Shared memory issues: Use deep copy for objects
Real Use Cases
- Exact task search: Array.IndexOf()
- Keyword search: Array.Find() or Exists()
- Sort tasks: Copy + Sort
- Reverse list: Copy + Sort + Reverse
- Wipe tasks: Array.Clear()
- Encrypt: ToCharArray + Reverse + Join
C# Array and String Methods Cheat Sheet
- Decrypt: Split + Reverse + Join