Simplified Differences between C# Property and Variable

Written by: Idika Destiny

Reading time:
Published 15 hours Ago On Wednesday, September 13, 2023
Updated 2 hours Ago On Thursday, September 14, 2023
665 Views



Properties and Variables are two ways of storing a single unit of data with a specific datatype in C# Both Properties and Variables have different characteristics and are used in different situation.

Definition of a Variable?

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

We can declare a class variable with a data type and an optional access modifier such as public, private, protected etc. and the access level of that variable is set by default based on the modifier it was declared with.

For example, a class variable with a private access modifier can be accessed only by the members of that particular class but not by/from any other component. Similarly, 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 – So therefore you cannot declare or serialize these variables inside an interface and expect every other implementation declare these variables.

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

Definition of 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.

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.




The need for a top business owner or organization to have a professional, scalable, Fast, Optimized,Efficient, Very Secured web application (website) can never be over emphasized.
However, With this great tool (Web Application) Business Owners will definitely and Undoubtedly solidify their online presence, improve their Search Engine ranking, eliminate the likelihood of Missing out on search engine queries / results by prospective clients whom may search for a business like theirs on search engines like Bing and google, stay toe to toe with Compititors who already have a web application etc.
Read Now Top 15 Reasosns why you need a website for your Business
You don’t need to do all of these alone, We got you covered!! Contact us now your satisfaction is always our priority. price definitely won't be a problem.

Thanks for reading



Difference between DBMS and RDBMS

What is Payment Gateway | 10 of the best Payment Gateways In Nigeria | Considerations When Choosing The Best Payment Gateway Provider In Nigeria