Before reading this article, please go through the following article:
Introduction to C# Class PropertiesProperties play a vital role in Object Oriented Programming. They allow us to access the private variables of a class from outside the class. It is good to use a private variable in a class. Properties look like a combination of variables and methods. Properties have sections: "a get and a set" method .The get method should return the variable, while the set method should assign a value to it. Step 1Open a new project with the name "LearnProperties" as in the following:Step 2Now to add the new classes to the project use "Project" -> "Add class" with the class name "BikeColor" as in the following:.Step 3After adding the new class your codes looks like the following:Step 4Insert the following code in the class BikeColor as in the following:Step 5Insert the following code in the Main Module. After adding the following code it will show the error.This is because you are accessing the private variable from outside the class; that is not allowed in OOP.Press F5. It will show the following error.Step 6Now we will try to access the private variable using the property _MyBikeColor. Then it will work fine.ConclusionAfter creating this simple example we have learned the importance of properties in OOP. Properties help us to access the private variable of the class. Moreover, properties help us to protect the private variable of the class from the unauthorized access.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members: