1. What Are Auto-Implemented, Read-Only, and init Properties in C#?
What is a Property?
A property is a class member that provides controlled access to data using get, set, or init accessors.
Properties are commonly used to implement encapsulation in C#.
A. Auto-Implemented Property
An auto-implemented property allows us to declare a property without explicitly creating a backing field.
The compiler automatically generates a hidden backing field.
Example:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; } = "";
}
// Usage
Employee emp = new Employee();
emp.Id = 101;
emp.Name = "John";
Console.WriteLine(emp.Name);
Output:
John
Explanation:
-
getretrieves the property value. -
setassigns or modifies the property value. -
The compiler automatically manages the backing field.
Key Points:
-
Reduces boilerplate code.
-
Supports reading and writing values.
-
Can be initialized with default values.
-
Commonly used in DTOs, ViewModels, and EF Core entities.
B. Read-Only Property
A read-only property allows a value to be retrieved but does not expose a setter.
A get-only auto-property can be initialized in its declaration or assigned inside a constructor.
Example:
public class Employee
{
public int Id { get; }
public string Name { get; }
public Employee(int id, string name)
{
Id = id;
Name = name;
}
}
// Usage
Employee emp = new Employee(101, "John");
Console.WriteLine(emp.Name);
// Compilation Error
// emp.Name = "David";
Output:
John
Explanation:
-
The constructor initializes
IdandName. -
Other code can read their values.
-
The values cannot be reassigned after construction.
Key Points:
-
Uses only the
getaccessor. -
Useful for protecting object state.
-
Get-only auto-properties can be assigned in constructors.
-
Read-only properties can also be computed from other values.
-
A read-only property does not automatically make a referenced object immutable.
C. init Property
An init property was introduced in C# 9.
It allows a property to be assigned during object initialization but prevents ordinary reassignment afterward.
Example:
public class Employee
{
public int Id { get; init; }
public string Name { get; init; } = "";
}
// Usage
Employee emp = new Employee
{
Id = 101,
Name = "John"
};
Console.WriteLine(emp.Name);
// Compilation Error
// emp.Name = "David";
Output:
John
Explanation:
-
Values are assigned using an object initializer.
-
Once initialization is completed, the properties cannot be reassigned through normal code.
-
Unlike a get-only auto-property, an
initproperty can be assigned in an object initializer.
Execution Flow
Employee Object
|
+---------+---------+
| | |
v v v
get/set get only get/init
| | |
v v v
Read and Read only Assign during
modify after initialization
anytime creation |
v
Read afterward
Comparison
|
Feature |
Auto ( |
Read-Only ( |
|
|---|---|---|---|
|
Read value |
Yes |
Yes |
Yes |
|
Modify after creation |
Yes |
No setter |
No ordinary reassignment |
|
Assign in constructor |
Yes |
Yes |
Yes |
|
Assign in object initializer |
Yes |
No |
Yes |
|
Supports encapsulation |
Yes |
Yes |
Yes |
|
Typical usage |
DTOs, entities |
Fixed/computed values |
Immutable-style models |
Real-World Usage in ASP.NET Core
public class EmployeeDto
{
public int Id { get; init; }
public string Name { get; init; } = "";
public string Department { get; set; } = "";
}
Here, Id and Name are intended to remain unchanged after initialization, while Department can be updated.
Interview Answer:
Auto-implemented properties provide automatic backing fields with get and set. Read-only properties expose values without allowing ordinary reassignment, while init properties allow values to be assigned during initialization and prevent subsequent reassignment.
2. What Is Method Overloading, and How Does C# Resolve Overloaded Methods?
What is Method Overloading?
Method overloading means defining multiple methods with the same name but different parameter lists within a class.
It is an example of compile-time polymorphism.
Methods can be overloaded by changing:
-
Number of parameters.
-
Types of parameters.
-
Order of parameter types.
-
Parameter passing modes, subject to C# signature rules.
Simple Example
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
// Usage
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(10, 20));
Console.WriteLine(calc.Add(10.5, 20.5));
Console.WriteLine(calc.Add(10, 20, 30));
Output:
30
31
60
Explanation
|
Method Call |
Selected Method |
|---|---|
|
|
|
|
|
|
|
|
|
The compiler identifies the appropriate method based on the supplied arguments.
How Does C# Resolve Overloaded Methods?
C# performs overload resolution at compile time.
The compiler determines which method is the best match based on argument count, types, conversions, and other applicability rules.
Example – Exact Match vs Implicit Conversion
public class Calculator
{
public void Display(int value)
{
Console.WriteLine("Integer Method");
}
public void Display(double value)
{
Console.WriteLine("Double Method");
}
}
// Usage
Calculator calc = new Calculator();
calc.Display(10);
calc.Display(10.5);
Output:
Integer Method
Double Method
Explanation:
-
10is anint, soDisplay(int)is selected. -
10.5is adouble, soDisplay(double)is selected.
Although an int can be implicitly converted to double, the exact int match is preferred here.
Overload Resolution Flow
Method Call
|
v
Find Methods by Name
|
v
Check Argument Count
|
v
Check Parameter Types
|
v
Check Valid Conversions
|
v
Determine Best Match
|
+------+------+
| |
v v
One Best Match No Unique
| Best Match
v |
Call Method v
Compile Error
This is a simplified representation. Actual C# overload resolution also considers optional parameters, generic type inference, named arguments, params, and other language rules.
3. Can Methods Be Overloaded by Changing Only the Return Type?
No. Return type alone cannot distinguish overloaded methods.
Invalid Example
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
// Compilation Error
public double Add(int a, int b)
{
return a + b;
}
}
Both methods have the same name and parameter types.
C# cannot overload methods based only on their return types.
4. What Happens When Overload Resolution Is Ambiguous?
If multiple applicable overloads are equally good and the compiler cannot select a unique best method, it produces a compilation error.
Example
public class Calculator
{
public void Display(int a, double b)
{
Console.WriteLine("Method 1");
}
public void Display(double a, int b)
{
Console.WriteLine("Method 2");
}
}
Calculator calc = new Calculator();
// Compilation Error: Ambiguous call
calc.Display(10, 20);
Explanation:
-
The first overload matches the first argument exactly.
-
The second overload matches the second argument exactly.
-
Both require one implicit conversion from
inttodouble. -
Neither overload is better overall.
Therefore, the compiler reports an ambiguous call.
Solution:
calc.Display(10, 20.0);
Output:
Method 1
Now Display(int, double) is an exact match.
5. Method Overloading vs Method Overriding
|
Feature |
Method Overloading |
Method Overriding |
|---|---|---|
|
Purpose |
Same operation with different parameters |
Change inherited method behavior |
|
Inheritance required |
No |
Yes |
|
Method name |
Same |
Same |
|
Parameters |
Different signatures |
Matching signature |
|
Keywords |
No special keyword |
|
|
Polymorphism |
Compile-time |
Runtime |
|
Method selection |
Compile-time overload resolution |
Runtime dispatch for virtual calls |
Important Key Points
-
Method overloading is compile-time polymorphism.
-
Overloaded methods must have different parameter signatures.
-
Return type alone cannot overload a method.
-
C# generally prefers a better parameter conversion over a worse one.
-
Exact matches are preferred over implicit numeric conversions in simple cases.
-
Ambiguous overloads produce compilation errors.
-
Static and instance methods can both be overloaded.
-
Constructors also support overloading.
Interview Answer:
Method overloading allows multiple methods with the same name but different parameter signatures. C# resolves overloaded methods at compile time by identifying applicable candidates and selecting the best match based on arguments, parameter types, and conversion rules. If no unique best method exists, the compiler reports an error.