Tuples & Extensions
C# / .NET Interview Notes
1. Big Picture
Tuples let you return multiple values from a method without creating a dedicated class. Extension methods let you add methods to existing types without modifying them or creating a derived class.
⭐ Quick Memory Trick:
Tuple → "Multiple values in one bag"
(int, string)Extension → "Add methods to existing types"
this string s2. Tuples
Basic Syntax
(int, string) pair = (42, "hello");
Console.WriteLine(pair.Item1); // 42
Console.WriteLine(pair.Item2); // "hello"
Named Elements
(int Id, string Name) person = (1, "Alice");
Console.WriteLine(person.Id); // 1
Console.WriteLine(person.Name); // "Alice"
Returning Multiple Values
(int min, int max) FindBounds(int[] numbers)
{
return (numbers.Min(), numbers.Max());
}
var (lo, hi) = FindBounds(new[] { 3, 1, 4, 1, 5 });
Console.WriteLine($"Range: {lo} to {hi}");
Deconstruction
var (id, name) = (1, "Alice");
// id = 1, name = "Alice"
// Discard with _
var (x, _) = (10, 20); // only x
3. Tuple vs ValueTuple
| Aspect | Tuple (System.Tuple) | ValueTuple (System.ValueTuple) |
|---|---|---|
| Type | Reference type (class) | Value type (struct) |
| Syntax | Tuple<int, string> | (int, string) |
| Mutability | Immutable | Mutable |
| Performance | Heap allocated | Stack allocated |
| Named elements | No | Yes |
| Usage | Legacy | Modern C# |
4. Extension Methods
Add methods to existing types without modifying the original type:
public static class StringExtensions
{
// 'this' makes it an extension method
public static int WordCount(this string str)
{
return str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
public static string Truncate(this string str, int maxLength)
{
return str.Length <= maxLength ? str : str[..maxLength] + "...";
}
}
// Usage — looks like a built-in method
string text = "hello world foo bar";
Console.WriteLine(text.WordCount()); // 4
Console.WriteLine("hello world".Truncate(5)); // "hello..."
5. Extension Method Rules
- Must be in a static class
- Method must be static
- First parameter uses
thiskeyword - Cannot override existing methods (instance methods win)
- Cannot extend properties or fields
6. LINQ = Extension Methods
LINQ is the most famous use of extension methods:
var numbers = new[] { 1, 2, 3, 4, 5 };
// These are all extension methods on IEnumerable<T>
var evens = numbers.Where(n => n % 2 == 0);
var doubled = numbers.Select(n => n * 2);
var sum = numbers.Sum();
var max = numbers.Max();
7. Common Interview Questions
Q: Can extension methods access private members?
No — they only have access to public members, just like any other external code.
Q: What happens if an extension method conflicts with an instance method?
The instance method always wins. Extension methods are only used when no matching instance method exists.
Q: Are tuples value types or reference types?
Modern C# tuples (int, string) are ValueTuple — value types on the stack. The older Tuple<T> class is a reference type.
8. One-Line Interview Answer
Tuples(int, string)are value types that bundle multiple values without a dedicated class, with named elements and deconstruction. Extension methods use thethiskeyword on the first parameter to add methods to existing types — LINQ is the most famous example.