In C# programming, properties are one of the most important features used to control access to class data. When learning C# Properties, especially Read Only Properties in C# and Write Only Properties in C#, developers understand how encapsulation works in object-oriented programming. This concept is essential for building secure, maintainable, and scalable applications in .NET development.
Properties act as a bridge between private fields and public access. Instead of directly exposing fields, we use properties to define controlled access through get and set accessors. These accessors allow us to implement validation, logic, security, and restrictions when reading or writing 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, properties ensure data encapsulation in C#.
Basic property syntax:
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
Here:
But what if we want to allow only reading or only writing? That is where Read Only and Write Only Properties in C# become useful.
A Read Only Property in C# is a property that exposes only a get accessor. This means the property value can be read from outside the class, but cannot be modified directly.
It is commonly used when:
class Student
{
private int rollNumber;
public Student(int roll)
{
rollNumber = roll;
}
public int RollNumber
{
get { return rollNumber; }
}
}
In this example:
C# allows a cleaner syntax using auto-properties:
class Employee
{
public int Id { get; }
public Employee(int id)
{
Id = id;
}
}
Here:
class Circle
{
public double Radius { get; set; }
public double Area => 3.14 * Radius * Radius;
}
Area is a computed read-only property. It calculates value dynamically.
A Write Only Property in C# exposes only a set accessor. This means the value can be assigned, but cannot be read from outside the class.
Although less common, write-only properties are useful when:
class User
{
private string password;
public string Password
{
set { password = value; }
}
}
Here:
class Logger
{
private string message;
public string Message
{
set
{
message = value;
Console.WriteLine("Logged: " + message);
}
}
}
The property is used to trigger behavior instead of storing just data.
C# allows different access levels for get and set.
class Product
{
public string Name { get; private set; }
public Product(string name)
{
Name = name;
}
}
Here:
This is a common technique in C# Encapsulation and Object-Oriented Programming in C#.
Many beginners confuse properties with readonly fields.
class Demo
{
public readonly int Value;
public Demo(int value)
{
Value = value;
}
}
class Demo
{
public int Value { get; }
public Demo(int value)
{
Value = value;
}
}
Key Differences:
Account balance should be read-only from outside.
class BankAccount
{
public decimal Balance { get; private set; }
public void Deposit(decimal amount)
{
Balance += amount;
}
}
Password should be write-only.
Application settings can be read-only after initialization.
Properties in C# are compiled into methods. The compiler converts get and set into special methods. There is no significant performance overhead compared to fields.
However:
class Person
{
public string Name { get; init; }
}
Init-only properties allow assignment only during object initialization.
abstract class Shape
{
public abstract double Area { get; }
}
Understanding C# Read Only Properties and C# Write Only Properties is essential for mastering C# programming and .NET Framework development.
They help achieve:
Using properties correctly improves maintainability, readability, and scalability of applications.
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