1How Do Properties Work in C#?
Hi friends! Today we will learn about Properties and Indexers in C#. We will cover get, set, init and required keywords.
A property provides controlled access to a class field. A field stores data directly, whereas a property allows us to control how that data is read or modified.
Indexers allow us to access objects using an index, similar to accessing elements in an array. These features are commonly used in enterprise applications, DTOs and domain models.
2Think About a Student Record
Imagine we are developing a student management application.
When creating a student, we must provide a name. Once the student object is created, we want to prevent changing the name during later initialization. We can use init.
We also need to read the student's name but allow updates to the city. We can control this using get and set.
Finally, imagine storing marks for multiple subjects. Instead of writing separate properties for every subject, we can use an indexer to access marks using an index.
3Understand Get, Set, Init and Required
1. Get Accessor
Get is used to read a property value. When we access the property, its get accessor returns the value.
2. Set Accessor
Set is used to assign or update a property value. The assigned value is available through a special keyword called value.
3. Init Accessor
Init allows a property to be assigned during object initialization or construction. It cannot be reassigned through normal assignment after initialization.
4. Required Keyword
Required forces the caller to initialize a property when creating an object. It helps prevent objects from being created without important information. Introduced in C# 11.
5. Indexer
An indexer allows an object to be accessed using square brackets. We declare it using the this keyword.
| Feature | Purpose |
|---|---|
| get | Read a value |
| set | Assign or update a value |
| init | Assign during initialization |
| required | Force initialization |
| Indexer | Access data using an index |
4Build a Student Management Program
Let's implement all these concepts in one working C# console application. The marks indexer will allow us to access subject marks using positions.
using System;
class Student
{
public required string Name { get; init; }
public string City { get; set; }
private int[] marks = { 85, 90, 95 };
public int this[int index]
{
get { return marks[index]; }
set { marks[index] = value; }
}
}
class Program
{
static void Main()
{
Student student = new Student
{
Name = "Rahul",
City = "Bangalore"
};
student.City = "Mysore";
student[0] = 88;
Console.WriteLine(student.Name);
Console.WriteLine(student.City);
Console.WriteLine(student[0]);
Console.WriteLine(student[1]);
}
}
Here, Name is required and uses init. City uses get and set, so we can update it. The indexer reads and updates marks using square brackets.
5Check the Program Result
Rahul Mysore 88 90
The student name is initialized during object creation. The city is updated afterwards. The first mark changes from 85 to 88 through the indexer.
6Watch the Program Run in VS Code
Follow how the student properties are initialized, updated and accessed through the indexer.
7Which Feature Should You Use?
Properties provide controlled access to class data. Get and set support reading and updating. Init and required help create properly initialized objects. Indexers make collections easier to access.
- Use get and set for normal property access.
- Use init and required for object initialization rules.
- Use indexers when objects need indexed access.
8Questions Interviewers Love to Ask
What is the difference between a field and a property?
Answer: A field stores data directly. A property provides controlled access through get and set accessors.
What is the difference between set and init?
Answer: Set allows a property to be updated later. Init restricts assignment to initialization and construction.
Why do we use the required keyword?
Answer: It forces callers to initialize required members when creating an object, helping prevent missing mandatory values.
What is an indexer in C#?
Answer: An indexer allows an object to be accessed using square bracket syntax. It is declared using the this keyword.
Can we combine required and init in one property?
Answer: Yes. Required ensures initialization, while init prevents normal reassignment after initialization. Both work together in C# 11 and later.