C# is a statically typed, object-oriented programming language developed by Microsoft as part of the .NET platform. One of its powerful features is its support for properties, which provide a flexible mechanism to read, write, or compute the values of private fields. This encapsulation mechanism is a vital part of object-oriented design, promoting data security and abstraction.
Properties in C# are members of a class that provide a flexible mechanism to access or assign values to private fields. Unlike fields, properties can have logic inside their accessors to validate or transform data before returning or setting it.
A property generally contains two accessors:
public class Sample
{
private int _value;
public int Value
{
get { return _value; }
set { _value = value; }
}
}
In the above example, the property Value encapsulates the field _value with both get and set accessors. However, it is possible to create properties with only a get or only a set accessor, leading us to Read-Only and Write-Only properties.
A read-only property allows only reading of data. It exposes a get accessor but does not provide a set accessor. Such properties are useful when we want to allow external code to read a property value but restrict it from modifying it.
public class Student
{
private string _name;
public Student(string name)
{
_name = name;
}
public string Name
{
get { return _name; }
}
}
Here, Name is a read-only property. It can be read from outside the class, but it cannot be set.
public class Product
{
public string Code { get; }
public Product(string code)
{
Code = code;
}
}
In C# 6.0 and later, you can declare an auto-implemented property with only a get accessor. It can be assigned in the constructor, making it read-only outside the class.
A write-only property provides only a set accessor and no get accessor. It is rarely used but can be useful in specific scenarios where you want to allow data to be written but not read from outside the class.
public class Logger
{
private string _log;
public string Log
{
set { _log = value; }
}
}
Here, the property Log allows data to be set, but not retrieved. This can be useful in security-sensitive applications such as logging passwords or sensitive information.
public class User
{
private string _passwordHash;
public string Password
{
set { _passwordHash = Hash(value); }
}
private string Hash(string input)
{
// Simulate hashing
return "HASHED_" + input;
}
}
This ensures that the password is never stored in plain text and cannot be retrieved from the class instance.
| Feature | Read-Only | Write-Only |
|---|---|---|
| Accessor | Only get | Only set |
| Usage | Read value | Assign value |
| Visibility | Value visible to outside | Value hidden from outside |
| Common Use | IDs, read-only config | Passwords, logs |
| Security | Moderate | High |
public class Configuration
{
public string Environment { get; internal set; }
}
This allows the value to be read publicly, but only set internally, offering a hybrid between read-only and fully accessible properties.
public class Employee
{
public string Name { get; protected set; }
}
Here, the property Name can only be set by the class itself or its derived classes. This pattern supports inheritance scenarios.
public class Session
{
public string Token { get; private set; }
public Session()
{
Token = GenerateToken();
}
private string GenerateToken()
{
return Guid.NewGuid().ToString();
}
}
This allows data to be generated and managed internally while exposing it safely.
Read-only and write-only properties are essential tools in the C# programmer's toolkit. They offer powerful mechanisms to control access to internal data, enforce encapsulation, and implement secure and maintainable code. While read-only properties are quite common and useful in many scenarios, write-only properties are less frequently used and should be applied judiciously. By understanding and utilizing these property types appropriately, developers can create clean, robust, and secure applications in C#.
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