Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Properties in C#

Introduction to Properties

Properties in C# are members that provide a flexible mechanism to read, write, or compute the values of private fields. They are special methods called accessors. Properties can be used as if they are public data members, but they actually include special methods called accessors. This enables data to be accessed easily and helps to promote the safety and flexibility of methods.

Defining Properties

A property is defined using the following syntax:

public class Example
{
    private int _value;
    public int Value
    {
        get { return _value; }
        set { _value = value; }
    }
}
                

In the above example, Value is a property that wraps the private field _value. The get accessor returns the value of the private field, and the set accessor assigns a value to the private field.

Auto-Implemented Properties

C# also provides auto-implemented properties which simplify property declarations when no additional logic is required in the property accessors. Here's the syntax:

public class Example
{
    public int Value { get; set; }
}
                

In this case, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

Read-Only Properties

Sometimes you may want to create read-only properties. This can be done by defining a property with only a get accessor:

public class Example
{
    private int _value;
    public int Value
    {
        get { return _value; }
    }

    public Example(int value)
    {
        _value = value;
    }
}
                

Computed Properties

Properties can also be computed, meaning their value is not stored but calculated based on other fields or properties:

public class Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }

    public int Area
    {
        get { return Width * Height; }
    }
}
                

Property with Validation

Properties can also include validation logic within the set accessor:

public class Person
{
    private int _age;
    public int Age
    {
        get { return _age; }
        set
        {
            if (value < 0 || value > 120)
            {
                throw new ArgumentOutOfRangeException("Age must be between 0 and 120.");
            }
            _age = value;
        }
    }
}
                

Static Properties

Properties can also be static, which means they belong to the type itself rather than an instance of the type:

public class Configuration
{
    private static string _configValue;
    public static string ConfigValue
    {
        get { return _configValue; }
        set { _configValue = value; }
    }
}
                

Examples in Use

Here is an example of a class using various kinds of properties:

public class Car
{
    public string Make { get; set; }
    public string Model { get; set; }
    private int _year;
    public int Year
    {
        get { return _year; }
        set
        {
            if (value > DateTime.Now.Year)
            {
                throw new ArgumentOutOfRangeException("Year cannot be in the future.");
            }
            _year = value;
        }
    }
    public int Age
    {
        get { return DateTime.Now.Year - _year; }
    }
}
                

Conclusion

Properties in C# provide a powerful mechanism for controlling access to the fields of a class. They enable encapsulation, validation, and computed values, making your code more robust and maintainable. Whether you need simple accessors or complex validation, properties are an essential feature of C# programming.