Interview Preparation

C# Interview Questions

Master the most commonly asked interview questions with comprehensive, expert-crafted answers designed to help you succeed.

26
Questions
100%
Expert Answers
Q1
What is C#?
C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by Microsoft as part of its .NET initiative. It is designed to be simple, powerful, and versatile, making it suitable for building a wide range of applications, including desktop software, web applications, mobile apps, and cloud-based services.
  • Object-Oriented: C# is fully object-oriented, supporting concepts like classes, inheritance, interfaces, and polymorphism.
  • Modern Syntax: The language syntax is similar to Java and C++, making it easy for developers familiar with those languages to transition.
  • .NET Framework: C# runs on the .NET platform, which provides a rich set of libraries, tools, and runtime support.
  • Component-Oriented: C# is designed to create reusable components and libraries that can be integrated into larger applications.
  • Versatile Use Cases: It is used for developing desktop apps with Windows Forms or WPF, web applications with ASP.NET, and even games using Unity.
Overall, C# balances performance, safety, and developer productivity, making it a widely used language in modern software development.
Q2
What is Common Language Runtime (CLR)?

The Common Language Runtime (CLR) is the core runtime engine of the .NET Framework that provides a managed execution environment for .NET programs.

  • It handles the execution of code written in different .NET languages (like C#, VB.NET, F#).
  • Provides essential services like:
    • Memory management
    • Type safety
    • Exception handling
    • Garbage collection
    • Security
    • Thread management
  • CLR converts Intermediate Language (IL) code into native machine code using a Just-In-Time (JIT) compiler.
  • Code executed under CLR is called Managed Code, while code outside it is called Unmanaged Code.
// Example: A simple C# program that runs under CLR

using System;

class HelloWorld {
  static void Main() {
    Console.WriteLine("Hello from CLR-managed code!");
  }
}

In essence, CLR is what makes the .NET ecosystem powerful, by abstracting low-level details and offering rich runtime services.

Q3
What is inheritance? Does C# support multiple inheritance?

Inheritance is a fundamental concept of Object-Oriented Programming (OOP) that allows one class to acquire the properties and methods of another class.

  • Base Class (Super Class): The class whose members (methods, fields) are inherited.
  • Derived Class (Sub Class): The class that inherits from the base class and can also define its own members.
  • Reusability: Inheritance promotes code reuse by enabling new classes to reuse code from existing classes.

C# supports single inheritance only, which means a class can inherit from just one base class. However, C# supports multiple inheritance of interfaces.

// Example of inheritance in C#

class Animal {
  public void Eat() {
    Console.WriteLine("This animal eats food.");
  }
}

class Dog : Animal {
  public void Bark() {
    Console.WriteLine("The dog barks.");
  }
}

class Program {
  static void Main() {
    Dog dog = new Dog();
    dog.Eat(); // Inherited method
    dog.Bark(); // Own method
  }
}

Note: To achieve multiple inheritance in C#, use interfaces instead of multiple base classes.

Q4
What is enum in C#?

Enum: An enumeration (enum) is a value data type in C# used to assign human-readable names to integral constants, making code easier to read and maintain. It is declared using the enum keyword and can be defined inside a namespace, class, or structure.

Example: Card suits such as Club, Diamond, Heart, and Spade can be represented as an enum named Suit.

Q5
What is the difference between ref and out keywords?

ref: Passes arguments by reference, meaning changes inside the method affect the original variable. The variable must be initialized before being passed.

out: Passes arguments as a reference type intended for output. The variable does not need to be initialized before being passed, but must be assigned before the method ends.

Q6
What are Properties in C#?

Properties are special members of a class that provide controlled access to private fields through get and set accessors. They help with encapsulation and improve maintainability.

  • Read-Write Properties: Have both get and set accessors.
  • Read-Only Properties: Have only a get accessor.
  • Write-Only Properties: Have only a set accessor.
  • Auto-Implemented Properties: No additional logic, introduced in C# 3.0.
Q7
What is the difference between constant and read-only in C#?

const: Declares a constant whose value must be assigned at compile time and cannot change thereafter.

readonly: Declares a variable whose value can be assigned either at declaration or in the constructor, and remains constant thereafter.

Q8
What is the difference between late binding and early binding in C#?

Early Binding: Binding where the compiler knows the object type, methods, and properties at compile time. This results in faster performance and fewer runtime errors. Typically used with static objects and provides better IntelliSense support.

Late Binding: Binding where the compiler determines the object type and members at runtime, often achieved through dynamic keyword or reflection. This provides flexibility but is slower and more prone to runtime errors.

Q9
What are the different ways in which a method can be Overloaded in C#?

Method overloading is a form of polymorphism where multiple methods share the same name but differ in parameter number, type, or order. It allows different implementations for different input scenarios within the same class.

  • Different number of parameters
  • Different types of parameters
  • Different order of parameters

Note: Return type alone cannot differentiate overloaded methods.

Q10
What is Reflection in C#?

Reflection is the process of inspecting metadata about assemblies, types, and their members at runtime. The System.Reflection namespace provides classes to obtain information about loaded assemblies, classes, methods, and value types.

Uses include:

  • Loading assemblies dynamically
  • Inspecting types and members
  • Invoking methods at runtime
Q11
What is Managed or Unmanaged Code?

Managed Code: Runs under the CLR (.NET's Common Language Runtime) and benefits from services like garbage collection, type checking, and exception handling. Written in languages such as C#, VB.NET.

Unmanaged Code: Executes directly by the OS, compiled to native machine code specific to the architecture, e.g., C/C++ programs without .NET CLR involvement.

Q12
What is File Handling in C#?

File Handling: Refers to performing operations such as creating, reading, writing, and appending files in C#. Files are accessed through streams — sequences of bytes — using System.IO namespace classes like FileStream, StreamReader, and StreamWriter. There are two main types of streams:

  • Input stream: Reads data from a file.
  • Output stream: Writes data to a file.
Q13
What is Singleton design pattern in C#?

A Singleton ensures only one instance of a class exists and provides global access to it. Common characteristics include:

  • Private constructor to prevent external instantiation.
  • Static variable to store the single instance.
  • Public static method to return the instance.
  • Often implemented as a sealed class.
Q14
What is Serialization?

Serialization is the process of converting an object into a byte stream for storage or transmission (e.g., over a network). The reverse process is deserialization, which reconstructs the object from the byte stream.

Q15
Name all the C# access modifiers.
  • private — Accessible only within the same class.
  • public — Accessible from anywhere.
  • internal — Accessible within the same assembly.
  • protected — Accessible within the class and its derived classes.
Q16
Differentiate between finally block and finalize method.

finally block: Executes after try/catch regardless of whether an exception occurred. Commonly used for resource cleanup.

Finalize method: A destructor method called by the garbage collector before reclaiming an object. Used to clean up unmanaged resources.

Q17
What is meant by an Interface?

An interface defines a contract containing only declarations (methods, properties, events, indexers) without implementation. Classes or structs implement the interface by providing definitions for its members.

Q18
What is meant by a Partial Class?

A partial class allows splitting a class definition across multiple files. All parts are combined into a single class when compiled, enabling separation of logic for better organization and teamwork.

Q19
What is the difference between read-only and constants?
  • const: Value assigned at compile-time, cannot change thereafter.
  • readonly: Value assigned at declaration or in the constructor, can differ per instance but cannot change after initialization.
Q20
What is a Race Condition in C#?

A Race Condition occurs when two or more threads access the same resource and try to modify it at the same time. This can lead to unpredictable results because the outcome depends on the timing of thread execution. Synchronization techniques like lock, Monitor, or other thread-safe mechanisms are used to prevent race conditions.

Q21
Why are Async and Await used in C#?

Async and Await keywords are used to implement asynchronous programming, allowing operations to run without blocking the main thread. This is useful for I/O-bound and long-running tasks. Async methods return a Task or Task<T> and can be awaited to continue execution after the awaited task finishes.

Q22
What is an Indexer in C#?

An Indexer is a special property in a class that allows instances of the class to be indexed like arrays. It enables access to internal collections using square bracket notation object[index], making code cleaner and more intuitive.

Q23
What is Thread Pooling in C#?

Thread Pooling refers to reusing a pool of worker threads to execute multiple tasks without creating and destroying threads repeatedly. The ThreadPool class in System.Threading manages a pool of threads to improve performance and reduce resource consumption.

Q24
What is an XSD file in C#?

XSD stands for XML Schema Definition. It defines the structure and constraints of XML documents. Without an XSD, an XML file can contain arbitrary elements and attributes. An XSD helps validate the XML content against predefined rules.

Q25
What are I/O classes in C#?

The System.IO namespace contains classes for file and data stream handling. Common classes include:

  • File and FileInfo for file operations.
  • Directory and DirectoryInfo for directory operations.
  • StreamReader and StreamWriter for reading/writing text files.
  • FileStream for working with binary data.
Q26
What are Regular Expressions in C#?

Regular Expressions (regex) are patterns used to match and manipulate strings. In C#, the System.Text.RegularExpressions namespace provides classes like Regex to search, validate, and replace text based on patterns.

Why Choose Our Question Bank?

Get access to expertly crafted answers and comprehensive preparation materials

Complete Collection

Access all 26 carefully curated questions covering every aspect of C# interviews

Expert Answers

Get detailed, professional answers crafted by industry experts with real-world experience

Instant Access

Start preparing immediately with instant access to all questions and answers after sign-up