Expert · Interviews, portfolio and careers · topic 40 of 43 · 40 min
The C# and .NET interview questions asked most often
Explanation
How to answer
Interviewers want to see understanding, not a memorised paragraph. A good answer has three parts: a one-line definition, a tiny example, and why it matters in real code. If you do not know something, say what you do know and how you would find out; guessing confidently is worse.
Core language questions
| Question | A good short answer |
|---|---|
| Value type vs reference type? | Value types (int, struct, DateTime) hold the data and are copied on assignment. Reference types (class, string, array) hold a reference, so assignment shares one object. Value types cannot be null unless nullable (int?). |
| What is boxing? | Converting a value type to object or an interface: .NET copies it into a heap object. Unboxing is the cast back. It costs memory and time, which is why generic collections like List<int> are better than ArrayList. |
| Abstract class vs interface? | A class can inherit one abstract class but implement many interfaces. Abstract classes can have fields, constructors and shared code ("is a kind of"); interfaces describe a capability ("can do"). Use interfaces for contracts and DI; abstract classes for shared base behaviour. |
| == vs Equals? | For classes, == compares references by default; Equals can be overridden for value comparison. string and records override both to compare content. |
| const vs readonly? | const is fixed at compile time and must be a literal. readonly fields are set once, in the declaration or constructor, and can hold values computed at run time. |
| ref vs out? | Both pass the variable itself. ref needs the caller to initialise it; out requires the method to assign it (the TryParse pattern). |
| virtual/override vs new? | override replaces a virtual method and is chosen by the object's real type at run time. new hides the method, and the variable's declared type decides which one runs. |
| Why are strings immutable? | A string never changes; methods return new strings. This makes strings safe to share and use as dictionary keys. Build big text in loops with StringBuilder. |
| Struct vs class vs record? | struct: small value type, copied. class: reference type with identity. record: concise type for data with value equality, ToString and with-expressions; record struct is the value-type version. |
Extension methods (often asked, quick to learn)
Console.WriteLine("411001".IsValidPin()); // True
public static class StringExtensions
{
public static bool IsValidPin(this string s) =>
s.Length == 6 && s[0] != '0' && s.All(char.IsDigit);
}.NET and ASP.NET Core questions
| Question | A good short answer |
|---|---|
| IEnumerable vs IQueryable? | IEnumerable runs LINQ in memory with delegates. IQueryable builds an expression tree that a provider like EF Core translates to SQL, so filtering happens in the database. Calling ToList too early loads everything into memory. |
| What does async/await do? | It lets a method wait for I/O without blocking a thread: at await the method pauses and the thread is freed; it resumes when the task completes. Return Task, avoid async void and .Result. |
| DI lifetimes? | Singleton: one per app. Scoped: one per HTTP request (DbContext). Transient: new each time. A service must not depend on a shorter-lived one (captive dependency). |
| How does garbage collection work? | The GC frees heap objects nothing references any more. It is generational (gen 0, 1, 2): most objects die young, so young generations are collected often and cheaply. You do not free memory manually. |
| Why IDisposable if there is a GC? | The GC handles memory, not other resources such as file handles, sockets and database connections. Dispose releases them promptly; using ensures it is called. |
| What is middleware? | Components in the request pipeline that each handle the request and call the next one; order matters (authentication before authorization). |
| What is the N+1 problem? | One query for a list, then one more per item for related data. Fix with Include or a Select projection. |
| How do you keep secrets out of code? | Configuration: user-secrets in development; environment variables or a vault in production; never commit keys. |
Classic coding questions
// 1. Reverse a string
static string Reverse(string s)
{
char[] chars = s.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
// 2. Is it a palindrome? (ignore case)
static bool IsPalindrome(string s)
{
string t = s.ToLower();
for (int i = 0, j = t.Length - 1; i < j; i++, j--)
if (t[i] != t[j]) return false;
return true;
}
// 3. Second largest distinct number
static int? SecondLargest(int[] nums)
{
var distinct = nums.Distinct().OrderByDescending(n => n).ToList();
return distinct.Count >= 2 ? distinct[1] : null;
}
// 4. Count each character
static Dictionary<char, int> CountChars(string s)
{
var counts = new Dictionary<char, int>();
foreach (char c in s) counts[c] = counts.GetValueOrDefault(c) + 1;
return counts;
}
Console.WriteLine(Reverse("chennai")); // iannehc
Console.WriteLine(IsPalindrome("Malayalam")); // True
Console.WriteLine(SecondLargest(new[] { 4, 9, 9, 7 })); // 7Common mistakes in interviews
- Saying "value types are on the stack" as the full answer: add that it depends on where they are stored, and that copy-vs-share is the real difference.
- Saying async makes code "run in parallel" or "faster": it frees threads during waits; it does not speed up CPU work.
- Not knowing your own project: be ready to explain every line of anything on your CV.
- Only knowing theory: practise writing small programs by hand without an IDE.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
The C# and .NET interview questions asked most often · 1/7
In this lesson you will practise the C# and .NET questions interviewers ask most often.
Prefer a video by a teacher? Find free videos on “C# interview questions and answers for freshers .NET” on YouTube ↗
In real life: A campus placement technical round
At a campus drive in Coimbatore, an IT services company's .NET interview had three parts: 10 minutes of OOP questions (abstract class vs interface, overloading vs overriding), a short coding task (count the vowels in a string), and questions about the student's final-year project. Students who could explain their own code calmly did better than those who had memorised definitions.
static int CountVowels(string s) =>
s.ToLower().Count(c => "aeiou".Contains(c));
Console.WriteLine(CountVowels("Coimbatore")); // 5Practice (0/4 done)
Which is the best one-line answer to "What is boxing?"
An interviewer asks why IDisposable exists when .NET has a garbage collector. Best answer?
What does SecondLargest(new[] { 5, 5, 5 }) return in the lesson code?
Record yourself (or explain to a friend) answering these five questions, each in under one minute: value vs reference types, abstract class vs interface, IEnumerable vs IQueryable, async/await, and the three DI lifetimes. Then write FizzBuzz (1 to 30) and a method that checks if two strings are anagrams, on paper first, then run them.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Your portfolio project: a tested Web API with EF CoreInterviews, portfolio and careers · same module
- Career paths with C# and .NET in IndiaInterviews, portfolio and careers · same module
- A 30-day revision plan for the whole courseInterviews, portfolio and careers · same module
- Java interview questionsJava Programming (Core to Spring Boot) · Design patterns, JVM internals and interviews
- What C# and .NET areGetting started with C# and .NET
- ASP.NET Core: your first Web APIWeb APIs with ASP.NET Core and EF Core
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is an extension method?
Answer: A static method whose first parameter uses this, callable like an instance method
LINQ methods are extension methods on IEnumerable<T>.
Which GC generation is collected most often?
Answer: Generation 0
Most objects die young, so gen 0 is collected often and cheaply.
const vs readonly: which can be set from a value calculated at run time in the constructor?
Answer: readonly
const must be a compile-time literal.
Which statement about async/await is correct?
Answer: It frees the thread while awaiting I/O and resumes later
async is about not blocking during waits.
For records, what does == compare?
Answer: Property values
Records generate value-based equality.
A class implements an interface but misses one member. What happens?Related
Answer: Compile error CS0535
Every interface member must be implemented.
Which lifetime is usual for an EF Core DbContext in ASP.NET Core?Related
Answer: Scoped
AddDbContext registers it as scoped: one per request.
System.out.println(10 / 4 + " " + 10 / 4.0) prints…Related
Which statement about .NET Framework 4.8 is true?Related
Answer: It is the older, Windows-only version that gets no new features
.NET Framework is the legacy Windows-only line. Modern .NET (5 and later) is cross-platform.
From What C# and .NET are
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Interviews, portfolio and careers” before its exam: Your portfolio project: a tested Web API with EF Core, Career paths with C# and .NET in India, A 30-day revision plan for the whole course.
- Module 10 exam: Interviews, portfolio and careers · 10 questions · 15 min, without help. 4 topics of this module still to finish first.
- 43 topics left in the course before the final exam.