properties in c# example

properties in c# example


What Are Properties in C#?

Properties in C# are special methods called accessors. They are used to read, write, or compute the values of private fields in a class. Properties encapsulate a get and a set method, which provide control over fields.

Types of Properties in C#

  1. Auto-Implemented Properties: These properties simplify syntax when no additional logic is required in the property accessors.
  2. Read-Only and Write-Only Properties: Restrict the property to be either readable or writable.
  3. Computed Properties: Properties that do not store a value but compute it when accessed.
  4. Expression-bodied Properties: Uses lambda expressions to simplify read-only properties.

Example: Basic Property

public class Person
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

C# Property Get; Set Shorthand (Auto-Implemented Properties)

Introduced in C# 3.0, auto-implemented properties reduce the need for boilerplate code.

public class Person
{
    public string Name { get; set; }
}

Why We Use Get; Set Property in C#

Getters and setters provide encapsulation of data. You can control how important variables are accessed and assigned in a class:

  • Getters (get): Return the property value.
  • Setters (set): Assign a new value to the property. They can contain validation to ensure data integrity.

C# Property vs Field

A field is a variable declared at the class level. C# properties are members that provide mechanisms to read from or write to the private fields of a class.

C# Computed Property

Computed properties calculate a value on-the-fly, rather than storing it.

public class Person
{
    public int BirthYear { get; set; }

    public int Age
    {
        get { return DateTime.Now.Year - BirthYear; }
    }
}

Auto-Implemented Properties C#

Auto-implemented properties simplify the declaration when no extra processing is needed in the accessors.

public class Person
{
    public string Name { get; set; } = "John Doe";
}

C# Public Get; Private Set

This pattern allows the property to be set within the class but read from any context.

public class Person
{
    public string Name { get; private set; } = "John Doe";

    public Person(string name)
    {
        Name = name;
    }
}

C# Get; Set Without Private Variable

When using auto-implemented properties, you don't need to explicitly declare a private variable.

public class Person
{
    public string Name { get; set; }
}

C# Get; Set Default Value

You can assign default values to properties at the time of declaration.

public class Person
{
    public string Name { get; set; } = "John Doe";
}

Properties in C# are powerful tools that help in managing the state of objects with enhanced security and flexibility. By understanding and using different types of properties effectively, developers can write cleaner, more maintainable code that safeguards the data integrity of their applications.

Leave a reply Your email address will not be published. Required fields are marked*

Categories Clouds