common C# and .NET interview questions with their answers ( AI generated)

Here are common C# and .NET interview questions with their answers to help you prepare:


1. What is the difference between ref and out in C#?

  • ref: Requires the variable to be initialized before being passed.
  • out: Does not require the variable to be initialized before passing; it must be assigned a value inside the method.

Example:

void RefExample(ref int x) { x += 10; }
void OutExample(out int x) { x = 10; }

int a = 5;
RefExample(ref a);  // a = 15

int b;
OutExample(out b);  // b = 10

2. What is the purpose of async and await in C#?

  • async: Marks a method as asynchronous, allowing it to run without blocking the main thread.
  • await: Pauses the execution of the method until the awaited task is complete.

Example:

async Task<string> GetDataAsync()
{
    await Task.Delay(1000); // Simulates delay
    return "Data Loaded";
}

3. What is the difference between IEnumerable and IQueryable?

  • IEnumerable:
    • Executes the query in memory.
    • Best for in-memory collection iteration.
  • IQueryable:
    • Executes the query on the database (deferred execution).
    • Useful for querying large datasets.

Example:

IEnumerable<int> enumerable = myList.Where(x => x > 10);
IQueryable<int> queryable = myDbContext.Entities.Where(x => x > 10);

4. Explain Garbage Collection in .NET.

  • Garbage Collection: Automates memory management by reclaiming unused memory objects to free up resources.
  • Generations:
    • Gen 0: Short-lived objects (e.g., temporary variables).
    • Gen 1: Medium-lived objects.
    • Gen 2: Long-lived objects (e.g., static fields).

5. What is the difference between struct and class?


6. Explain Dependency Injection (DI).

  • Definition: A design pattern where objects are provided with their dependencies at runtime rather than hardcoding them.
  • Benefits:
    • Improves code modularity.
    • Enhances testability.
  • Example:
public interface IService { void Serve(); }
public class MyService : IService { public void Serve() => Console.WriteLine("Service Called"); }

public class Consumer
{
    private readonly IService _service;
    public Consumer(IService service) { _service = service; }
    public void Start() { _service.Serve(); }
}

// Usage (with DI Container, e.g., ASP.NET Core)
services.AddTransient<IService, MyService>();

7. What is the difference between abstract class and interface?


8. What is LINQ, and how does it work?

  • LINQ (Language-Integrated Query): Enables querying data in a type-safe and concise way using query syntax or method syntax.
  • Example:
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();

9. What are delegates in C#?

  • Definition: Delegates are type-safe pointers to methods.
  • Usage: Useful for implementing callbacks and event handling.

Example:

public delegate void PrintDelegate(string message);

void PrintMessage(string message) => Console.WriteLine(message);

PrintDelegate del = PrintMessage;
del("Hello, Delegates!");

10. What are the different types of assemblies in .NET?

  • Private Assembly: Used by a single application.
  • Shared Assembly: Shared across multiple applications (stored in the Global Assembly Cache - GAC).
  • Satellite Assembly: Used for localization/globalization.

If you need additional questions or in-depth explanations, let me know!

Comments

Popular posts from this blog

Spring boot versions : Detailed explanation of the different versions and releases of Spring Boot (AI Generated)

download youtube videos java program ( AI generated)

Java Spring Framework versions and their major releases ( AI Generated )