4 questions found
What's the difference between IEnumerable<T> and List<T>?
Intermediate
IEnumerable<T> is an interface representing a sequence of items that can be enumerated one at a time using deferred execution, meaning the actual data is only produced when you iterate over it, which makes it memory efficient for large or streaming data sources. List<T> is a concrete class that implements IEnumerable<T> but stores all of its items in memory at once, and it also provides additional capabilities like adding, removing, sorting, and accessing items directly by index, which IEnumerable<T> alone does not support.
IEnumerable<int> numbers = GetNumbersLazily();
List<int> list = numbers.ToList();
list.Add(10);
var first = list[0];
Real-world example
A reporting feature that processes millions of database records uses IEnumerable<T> to stream results one at a time and avoid loading everything into memory, only converting the relevant filtered subset into a List<T> for further manipulation.
Common follow-ups: What does deferred execution actually mean in practice?;When should you convert an IEnumerable to a List?
What are the 2 broad classifications of data types available in C#?;What is the difference between value types and reference types?
Why is class an abstract data type?
Intermediate
A class is considered an abstract data type because it defines a blueprint that combines both data, in the form of fields and properties, and behavior, in the form of methods, while hiding the internal implementation details from the code that uses it. Users of a class only need to know what operations are available through its public methods and properties, not how those operations are implemented internally, which is the core idea behind abstraction and encapsulation in object oriented programming.
public class BankAccount {
private decimal _balance;
public void Deposit(decimal amount) {
_balance += amount; // internal detail hidden from callers
}
public decimal GetBalance() { return _balance; }
}
Real-world example
A banking application exposes a BankAccount class with simple Deposit and Withdraw methods, while the actual internal logic for validating transactions and updating balances remains completely hidden from the code that uses the account.
Common follow-ups: What is the difference between abstraction and encapsulation?;How does an abstract class differ from a regular class?
What is an Abstract Property in C#? Give an example.;What are the advantages of using properties in C#?
What are the new features introduced in C# 7?
Intermediate
C# 7 introduced several useful features including tuples with named elements for returning multiple values from a method more cleanly, pattern matching with the is and switch keywords for more expressive conditional logic, local functions that let you define a helper method inside another method, out variables that let you declare an out parameter inline, and expanded expression bodied members for properties and constructors, all aimed at making code more concise and readable.
(string Name, int Age) GetPerson() {
return ("Ali", 30);
}
var person = GetPerson();
Console.WriteLine(person.Name);
if (obj is string text) {
Console.WriteLine(text.Length);
}
Real-world example
A development team refactors a method that previously used an out parameter class just to return two values, switching to a named tuple in C# 7 instead, making the method signature far cleaner and easier to read.
Common follow-ups: How do named tuples differ from using a custom class to return multiple values?;What further pattern matching improvements were added in later C# versions?
What is C#?;What's the difference between IEnumerable<T> and List<T>?
Why should you override the ToString() method?
Beginner
Every class in C# inherits a ToString method from the base Object class, but by default it simply returns the full name of the type, which is rarely useful for debugging or displaying information to users. Overriding ToString lets you return a meaningful, readable string representation of an object's actual data, which is especially helpful when logging objects, displaying them in a user interface, or debugging, since it shows relevant details instead of a generic type name.
public class Product {
public string Name { get; set; }
public decimal Price { get; set; }
public override string ToString() {
return $"{Name}: ${Price}";
}
}
Console.WriteLine(new Product { Name = "Pen", Price = 2.50m });
Real-world example
A logging system that writes exception details to a file calls ToString on custom exception related objects, and because those classes override ToString with meaningful details, the resulting logs are immediately useful for debugging instead of showing generic type names.
Common follow-ups: What is the default behavior of ToString if it is not overridden?;Should ToString include sensitive information like passwords?
Why is class an abstract data type?;What is the difference between string keyword and System.String class?