Easy differences between property and variable in C#

In this detailed comparison, let us look at what are the key differences between property and variable in C# with examples

Properties and Variables are two ways of storing a single unit of data of a specific type in C#. However these structures have different characteristics and are used in different scenarios.

In this article, let us look at each of these in detail and understand the differences between them.

What is a Variable?

A Variable is a named memory location which can store data of a specific data type. We can read from and write data to a specific memory location using this variable, based on the scope that is defined with it.

We can declare a class variable with a data type and an optional access modifier (public, private, protected etc.) and the access level of that variable is thus set based on the modifier.

For example, a class variable with a private access modifier can be accessed only the members of that particular class but not any other component and a variable with protected scope can be used by a child class of that class. These store the data in a memory location directly, without any encapsulation.

variables are a part of concrete classes – thus you cannot declare these variables inside an interface and expect every other implementation declare these variables. These can’t be serialized either.

So have a complex encapsulation around a variable or enforce implementations, we go for Properties.

What is a Property?

It is a general convention for a class to have private class variables and public setter and get methods around these variables.

Properties combine all of these into a single statement – a Property provides a way to encapsulate and access a class variable. Generally, a Property has a backing variable that represents a memory location. But we don’t see or access that variable directly and instead use the properties directly.

Properties do have access modifiers, using which you can manipulate and control access – but this is applied over the backing variable that the property represents.

The syntax of a Property is as below –

class Car
{
    private string _modelId;

    public string ModelId
    {
        get { return _modelId; }
        set
        {
            if (value.Length < 10)
            {
                throw new Exception("ID Can't be Less than 10 CHARS");
            }
            else
            {
                _modelId = value;
            }
        }
    }

    public string ModelName { get; private set; }

    public Car(string modelName)
    {
        ModelName = modelName;
    }
}

In the above example, the ModelId has a private field _modelId that the Property encapsulates and provides access methods. This property also adds a validation logic when setting a new value to the variable.

On the other side, we have a property ModelName that doesn’t have any variable that it represents explicitly. This property also has a backing variable, but it is implicit to the property.

The Property has a public get accessor, but the set accessor is private, which means any other class or component cannot set value to this property.

Since properties don’t represent a variable directly and are similar to methods, these properties can be declared in an interface and enforce to be implemented. These can also be serialized.

Summary – Property vs Variable in c#

VariableProperty
a named memory location which can store data of a specific data typeprovides a way to encapsulate and access a class variable
can be used with access modifiers that represent access to the memory locationcan be used with access modifiers that represent access to the variable that the property encapsulates
doesn’t provide any encapsulation to the data that is storedprovides encapsulation to the variable that holds the data by adding any business or validation logic as required
represent actual storing locationrepresent a private backing field on which the properties act
can’t be declared in interfacescan be declared in interfaces and enforce implementation
can’t be serializedcan be serialized

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

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