C# Key Concepts - Quick Interview Guide
Class
Definition: Reference type that defines data and behavior.
Usage: Used to model objects like Person or Car.
Example:
class Person { public string Name; public void Speak() => Console.WriteLine("Hi"); }
Struct
Definition: Value type used to store small related data.
Usage: Used for lightweight data like Point or Color.
Example:
struct Point { public int X, Y; }
Enum
Definition: Value type representing a set of named constants.
Usage: Used for days, statuses, roles.
Example:
enum Days { Sunday, Monday, Tuesday }
Delegate
Definition: Holds reference to methods with a specific signature.
Usage: Used for callbacks, events.
Example:
delegate void Greet(string name);
Interface
Definition: Defines a contract with method/property signatures.
Usage: Used for abstraction, DI.
Example:
interface IDrive { void Start(); }
Record
Definition: Immutable reference type with value-based equality.
Usage: Used for data models.
Example:
record Person(string Name, int Age);
Fields
Definition: Variables inside a class holding data.
C# Key Concepts - Quick Interview Guide
Usage: Used to store object's state.
Example:
public int age; private string name;
Methods
Definition: Define the actions/behavior of a class.
Usage: Used for reusable logic.
Example:
public void Greet() { Console.WriteLine("Hello"); }
Constructors
Definition: Special method that initializes a class.
Usage: Used to set initial values.
Example:
public Person(string name) { this.name = name; }
Finalizers
Definition: Called when an object is garbage collected.
Usage: Used to release unmanaged resources.
Example:
~Person() { Console.WriteLine("Destroyed"); }
Properties
Definition: Provide controlled access to fields.
Usage: Used for encapsulation.
Example:
public string Name { get; set; }
Indexers
Definition: Allow object to be accessed like an array.
Usage: Used in custom collections.
Example:
public string this[int index] { get { return data[index]; } set { data[index] = value; } }
Events
Definition: Notify other classes when something happens.
Usage: Used in UI or pub-sub pattern.
Example:
public event Action OnClick;
C# Key Concepts - Quick Interview Guide
Deconstructors
Definition: Break an object into its parts.
Usage: Used in tuple deconstruction.
Example:
public void Deconstruct(out string name, out int age) { name = this.Name; age = this.Age; }