C#

Automatic Properties In C# 3.0

In C# Properties are used to expose the local member variables.
Till C# 2.0 version properties are defined by declaring a private member variable first and then declaring the property as stated below
private string empName;
Public string Empname
{
       get
       {            
           return empName;
       }
        set
       {               
          empName= value;
       }
}

C# Automatic property

But, C# 3.0 onwards Properties can be declared simply without explicitly declaring the corresponding private member variable.

From C# 9, the ability to implement init accessors as auto-implemented properties has also been introduced.

In C# Automatic property is a property that has an automatically produced backing field that is generated by the compiler.

Primitive getters and setters that only return the value of the backing field are avoided which can save development time by allowing to not write that code.

We can utilize attributes in many circumstances for properties, but we generally prefer properties that are common across several .NET technologies.

Rest will be taken care of internally during compilation. This reduces no of lines of code and improves the development speed.

Public string EmpName { get; set}

C# Automatic Property After Compilation

After compilation the compiler produces the code of automatic property as below.

    [CompilerGenerated]
    private string <EmpName>k__BackingField;
 
    public string EmpName
    {
        [CompilerGenerated]
        get { return <EmpName>k__BackingField; }
 
        [CompilerGenerated]
        set { <EmpName>k__BackingField = value; }
    }

Auto-implemented properties in C# 6.0 and higher versions, we can initialize in a similar fashion as fields like

//Automatic Property C# 6, assigning 
public string EmpName { get; set; } = "Rajeev";

Summary

This article gave you an idea of how to use Automatic Property in C#.I hope this was helpful for you. Please share your opinions in the comment section below.

You may also be interested in reading Object Initializers in C#

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

2 years ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

2 years ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

2 years ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

2 years ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

2 years ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

2 years ago