Phone no: +234 705 673 3798 or email us: info@kdesglobal.com
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.
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.
Read Now Top 15 Reasosns why you need a website for your BusinessYou 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.
« Previous Difference between DBMS and RDBMS |
Next » What is Payment Gateway | 10 of the best Payment Gateways In Nigeria | Considerations When Choosing The Best Payment Gateway Provider In Nigeria |
Written by: Idika Destiny
Reading time:
Published 8 hours Ago On Wednesday, September 13, 2023
Updated 19 hours Ago On Thursday, September 14, 2023
616 Views