DEV Community

Caique Santos
Caique Santos

Posted on

.NET Interview questions: Key concepts explained

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:
int n1 = 10;
int n2 = a;
n2 = 20; // change does not reflects in "n1"
Console.WriteLine(n1); // 10
Console.WriteLine(n2); // 20
Enter fullscreen mode Exit fullscreen mode
  • 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:
class Person{
    public string Name;
}
var p1 = new Person { Name = "Caique" };
var p2 = p1;
p2.Name = "John"; // change reflects in variable "p1"
Console.WriteLine(p1.Name); // "John"
Enter fullscreen mode Exit fullscreen mode

When to use Record, Struct or Class

  • Class: Use it when your object has behavior, meaningful functions, and has identity like an entity.
public class User {
    public Guid Id { get; set; }
    public string Name { get; set; }
    public void ChangeName(string name) {
        Name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Struct: Use it when you want immutability or you have few properties, like a coordinate object.
public struct Point {
    public int X { get; set; }
    public int Y { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
  • Record: Use it when you want to compare values, or when you are modeling just data, not behavior, like a DTO.
public record UserDto{
    public required string FirstName { get; init; }
    public required string LastName { get; init; }
};  // or
public record UserDto(string FirstName , string LastName );
Enter fullscreen mode Exit fullscreen mode

Top comments (0)