What's the difference between IQueryable, IEnumerable and List?
IQueryable represents a query that is not executed yet. It allows building the query step by step and is typically executed when iterated.
IEnumerable represents a sequence of data that is already in memory (or will be pulled into memory). It supports iteration and can use lazy execution
List is a concrete collection in memory that implements IEnumerable. It is fully materialized and allows modification (add, remove, etc).
How does the lifecycle work for services in Dependency Injection (Singleton, Scoped, and Transient)?
Singleton creates a single instance for the entire lifetime of the application. The same instance is shared across all requests (e.g., cache service).
Scoped creates one instance per request. The same instance is shared across all components within that request (e.g., DbContext).
Transient creates a new instance every time it is requested. It is commonly used for lightweight, stateless services (e.g., mappers).
What is the difference between FirstOrDefault() and SingleOrDefault()?
FirstOrDefault returns the first element that match the condition, or the default value for that type.
Every type has a default value. Here are some examples:
classes returns null
Int returns 0
bool returns false
SingleOrDefault is very similar to FirstOrDefault, but it throws an exception if the result of the condition returns more than one element.
What is the difference between Value Types and Reference Types?
Value Types
Store the value directly
Typically allocated on the stack (but can be stored on the heap in some cases)
struct, int, bool are examples
When you copy it, you copy the entire value, like below:
intn1=10;intn2=a;n2=20;// change does not reflects in "n1"Console.WriteLine(n1);// 10Console.WriteLine(n2);// 20
Reference Types
Store a reference (memory address) to the object
Typically allocated on the heap
class, string, array, object are examples
When you copy it, you copy the reference, not the actual object, like below:
classPerson{publicstringName;}varp1=newPerson{Name="Caique"};varp2=p1;p2.Name="John";// change reflects in variable "p1"Console.WriteLine(p1.Name);// "John"
When to use Record, Struct or Class
Class: Use it when your object has behavior, meaningful functions, and has identity like an entity.
Top comments (0)