In C# programming, properties play a vital role in implementing encapsulation in C# and controlling access to class data. A property in C# is a member of a class that provides a flexible mechanism to read, write, or compute the value of a private field. Instead of exposing fields directly, developers use properties to protect data integrity and apply validation logic.
If you are learning C# OOP concepts, understanding C# Properties is essential because they are tightly connected with object-oriented programming in C#, data hiding, abstraction, and secure code design.
In this detailed tutorial, we will explore:
A Property in C# is a special member of a class that provides a way to access private fields safely. It uses get and set accessors to retrieve and assign values.
In simple terms:
class Student
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
Here:
Using public fields directly breaks encapsulation in C#. Properties provide:
class Student
{
public int Age;
}
Anyone can assign negative values:
Student s = new Student();
s.Age = -5;
This can lead to invalid data.
class Student
{
private int age;
public int Age
{
get { return age; }
set
{
if (value > 0)
age = value;
}
}
}
Now invalid values cannot be assigned.
The get accessor returns the value of a field.
The set accessor assigns a value using the implicit keyword value.
using System;
class Employee
{
private double salary;
public double Salary
{
get { return salary; }
set
{
if (value >= 0)
salary = value;
}
}
}
class Program
{
static void Main()
{
Employee emp = new Employee();
emp.Salary = 50000;
Console.WriteLine(emp.Salary);
}
}
C# provides a simplified way to declare properties without defining a private field manually. These are called Auto-Implemented Properties in C#.
public string Name { get; set; }
class Student
{
public string Name { get; set; }
public int RollNumber { get; set; }
}
The compiler automatically creates a hidden private backing field.
A property with only a get accessor is called a Read-Only Property in C#.
class Person
{
private DateTime birthDate;
public Person(DateTime date)
{
birthDate = date;
}
public DateTime BirthDate
{
get { return birthDate; }
}
}
A property with only a set accessor is called a Write-Only Property in C#.
class Secret
{
private string password;
public string Password
{
set { password = value; }
}
}
A Static Property in C# belongs to the class rather than an instance.
class Company
{
public static string CompanyName { get; set; }
}
Access without creating object:
Company.CompanyName = "Tech Corp";
Modern C# supports Expression-Bodied Properties for concise syntax.
class Circle
{
public double Radius { get; set; }
public double Area => 3.14 * Radius * Radius;
}
You can control accessibility of get and set separately.
public string Name { get; private set; }
Here, set is accessible only within the class.
A backing field is a private variable that stores the value of a property.
private int marks;
public int Marks
{
get { return marks; }
set { marks = value; }
}
Both properties and methods provide access to data. However:
Use properties when:
Encapsulation is a core OOP concept in C#. Properties ensure data hiding and controlled access.
Advantages:
public string Name { get; init; }
Can be set only during object initialization.
public int YearOfBirth => DateTime.Now.Year - Age;
Fields store data directly. Properties provide controlled access.
A property without explicit backing field.
Yes, to restrict modification.
Understanding C# Properties is fundamental for mastering C# programming language and object-oriented programming in C#. Properties enhance encapsulation in C#, improve code maintainability, and ensure data security.
From basic getter and setter methods to advanced features like expression-bodied and init-only properties, properties are an essential part of professional C# development.
C# is primarily used on the Windows .NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved