.NET Core Interview Questions and Answers
1. What is C#?
C# is a modern, object-oriented, type-safe programming language developed by Microsoft as part of the .NET platform. It is used for building Windows applications, web applications, and enterprise software.
2. What are the different types of comments in C#?
Single-line: // comment
Multi-line: /* comment */
XML documentation: /// summary
3. What is the difference between int and Int32?
There is no difference. int is an alias for System.Int32.
4. What are value types and reference types?
Value types (e.g., int, float, bool) store data directly.
Reference types (e.g., class, string, object) store a reference to the data's memory address.
5. What is the difference between var, dynamic, and object?
var: type decided at compile time.
dynamic: type resolved at runtime.
object: base class of all types, but needs casting.
6. What are nullable types?
Nullable types (e.g., int?) allow value types to be assigned null.
7. What is the difference between == and .Equals()?
==: checks value for primitive types, reference for objects.
.Equals(): checks value and can be overridden for custom comparison.
8. What is the purpose of the using statement?
It defines a scope at the end of which an object will be disposed (used with IDisposable).
9. What is the Main() method in C#?
It is the entry point of a C# console application where execution starts.
10. What is a namespace?
A namespace is a container for classes and other namespaces to organize code.
11. What is a class and an object?
A class is a blueprint; an object is an instance of a class.
12. What is inheritance?
Inheritance allows one class to acquire the properties and methods of another.
13. What is polymorphism?
Polymorphism allows methods to behave differently based on the object.
14. What is method overloading and overriding?
Overloading: same method name, different parameters.
Overriding: redefining a base class method in a derived class.
15. What is encapsulation?
Encapsulation hides object data through access modifiers and exposes behavior via methods.
16. What is abstraction?
Abstraction shows only essential features and hides details using abstract classes or interfaces.
17. Difference between abstract class and interface?
Abstract class: can have implementations.
Interface: only method definitions (until C# 8).
18. Can a class inherit multiple classes in C#?
No, C# does not support multiple inheritance for classes.
19. Access modifiers in C#:
public, private, protected, internal, protected internal, private protected
20. What is a sealed class?
A sealed class cannot be inherited.
21. What is boxing and unboxing?
Boxing: converting a value type to object.
Unboxing: extracting value type from object.
22. What is a struct?
A lightweight value type used to group related variables.
23. What is a readonly variable?
Can only be assigned during declaration or in a constructor.
24. Difference between const and readonly?
const: compile-time constant.
readonly: runtime constant.
25. What is garbage collection?
Automatic memory management that frees unused objects.
26. What are the generations in GC?
Gen 0: short-lived objects
Gen 1: medium-lived
Gen 2: long-lived
27. Stack vs Heap?
Stack: value types, fast, managed by scope.
Heap: reference types, garbage-collected.
28. What is a static class?
A class that cannot be instantiated and contains only static members.
29. ref, out, and in keywords?
ref: requires initialization before passing.
out: no need to initialize.
in: readonly reference.
30. What is a memory leak?
Unreleased memory due to references still being held.
31. What is LINQ?
Language Integrated Query; enables querying collections with SQL-like syntax.
32. What are extension methods?
Static methods that act as if they belong to existing types.
33. What is a delegate?
A type-safe function pointer that can hold reference to methods.
34. What is an event?
A way for a class to provide notifications to clients.
35. What is an anonymous method?
A method without a name, often used in delegates.
36. What is a lambda expression?
Short syntax for anonymous methods: (x) => x * x
37. What is a generic?
Allows class or method to operate on any data type without boxing/unboxing.
38. Common collections in C#?
List<T>: dynamic array
Dictionary<K,V>: key-value pairs
ArrayList: non-generic
39. IEnumerable vs ICollection vs IQueryable?
IEnumerable: forward-only read
ICollection: add/remove
IQueryable: query from data source
40. What is async/await?
Keywords used for asynchronous programming with tasks.
41. What is exception handling?
Mechanism to handle runtime errors using try-catch-finally blocks.
42. try-catch vs try-finally?
try-catch: catches exceptions.
try-finally: always executes finally block.
43. throw vs throw ex?
throw: preserves original stack trace.
throw ex: resets stack trace (not recommended).'
44. How to read/write a file in C#?
Use System.IO.File.ReadAllText() and File.WriteAllText().
45. What are custom exceptions?
User-defined exceptions derived from System.Exception.
46. What is dependency injection?
A design pattern to inject dependencies into a class rather than hardcoding them.
47. IDisposable vs Finalize()?
IDisposable.Dispose() is called manually.
Finalize() is called by GC.
48. async void vs async Task?
async Task: preferred for async methods.
async void: use for event handlers only.
49. What are tuples in C#?
A lightweight way to group multiple values in a single return type.
50. How to improve C# application performance?
Use caching
Avoid unnecessary object creation
Use async calls
Minimize boxing/unboxing
Use efficient algorithms