Written by
Maximilian Leodolter
Prepare yourself with these intermediate C# Questions.
Table of Contents
Preparing for a technical interview can be daunting, especially when you’re aiming to demonstrate your expertise as a C# developer. To help you get ready, we’ve compiled a list of five intermediate-level questions that are commonly asked in technical interviews. By familiarizing yourself with these topics, you’ll be better equipped to tackle challenging questions and impress your interviewers.
Understanding the difference between abstract classes and interfaces is fundamental for C# developers, as both are used to define contracts and provide polymorphic behavior.
Can have both abstract methods (without implementation) and non-abstract methods (with implementation). Can contain fields, constructors, and destructors. Can provide default behavior. A class can inherit only one abstract class (single inheritance).
Can only have method signatures (methods without implementation), properties, events, and indexers. Cannot contain fields, constructors, or destructors. Cannot provide any default behavior. A class or struct can implement multiple interfaces (multiple inheritance).
public abstract class Animal
{
public abstract void MakeSound();
public void Sleep()
{
Console.WriteLine("Sleeping");
}
}
public interface IFlyable
{
void Fly();
}
public class Bird : Animal, IFlyable
{
public override void MakeSound()
{
Console.WriteLine("Chirp");
}
public void Fly()
{
Console.WriteLine("Flying");
}
}
LINQ (Language Integrated Query) is a powerful feature in C# that allows you to query collections in a declarative manner, similar to SQL. LINQ can be used with various data sources such as arrays, collections, XML, and databases.
Provides a consistent query syntax across different data sources. Supports filtering, ordering, and grouping operations. Enables strong typing and IntelliSense support in Visual Studio.
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
foreach (var num in evenNumbers)
{
Console.WriteLine(num);
}
Asynchronous programming is crucial for improving the responsiveness of applications, especially in scenarios involving I/O-bound operations. The async and await keywords in C# simplify writing asynchronous code.
async keyword marks a method as asynchronous. await keyword is used to suspend the execution of an async method until the awaited task completes. The method marked with async must return Task, Task
public async Task<string> GetDataAsync()
{
using (var client = new HttpClient())
{
var response = await client.GetStringAsync("https://example.com/data");
return response;
}
}
Dependency Injection (DI) is a design pattern used to achieve Inversion of Control (IoC) between classes and their dependencies. It enhances code reusability, testability, and maintainability.
C# provides built-in support for DI through the Microsoft.Extensions.DependencyInjection namespace.
public interface IMessageService
{
void SendMessage(string message);
}
public class EmailService : IMessageService
{
public void SendMessage(string message)
{
Console.WriteLine($"Email sent: {message}");
}
}
public class Notification
{
private readonly IMessageService _messageService;
public Notification(IMessageService messageService)
{
_messageService = messageService;
}
public void Notify(string message)
{
_messageService.SendMessage(message);
}
}
// Setup DI
var services = new ServiceCollection();
services.AddTransient<IMessageService, EmailService>();
services.AddTransient<Notification>();
var serviceProvider = services.BuildServiceProvider();
// Resolve and use the service
var notification = serviceProvider.GetService<Notification>();
notification.Notify("Hello, Dependency Injection!");
Garbage collection (GC) in C# is an automatic memory management feature that reclaims memory occupied by objects that are no longer in use, preventing memory leaks and optimizing the use of system resources.
class Program
{
static void Main()
{
for (int i = 0; i < 100; i++)
{
CreateObject();
}
GC.Collect(); // Forces garbage collection
GC.WaitForPendingFinalizers(); // Waits for finalizers to complete
}
static void CreateObject()
{
var obj = new object();
// obj goes out of scope here and becomes eligible for GC
}
}
Being well-prepared for a technical interview involves more than just knowing the right answers. It requires a deep understanding of core concepts and the ability to apply them effectively. These five questions cover important areas in C# development and will help you showcase your skills and knowledge confidently. Good luck with your interview!