Practical Implementation of Properties in C# - Informational Webber

Practical Implementation of Properties in C#

Before implementing the concept of Properties in C# language, we must be aware of the fact that every variable is by default Private in that class. So what basically Properties in C# is?

What is Properties in C# ?

Properties is just an approach to access the variable (by default private) of one class into another class without breaking the concept of data hiding. Suppose we have opened a account in a bank and we have deposited some amount in that account. In order to withdraw amount from that account, we will have to follow a procedure to get that amount. That is I have to fill Money Withdrawal form.

In terms of Properties, he cash we have deposited in bank account is private to account. But to get access to that account, certain procedure needs to be followed. The procedure will not break concept of data hiding.

Things to remember while using Properties in C#

  1. Type of Property will be as same as type of variable declared.
  2. Every variable will have separate property defined.
  3. Give name to property similar to variable name.
  4. Properties is used to access private variable of one class into another class without breaking Data Hiding.

Program for Properties in C#

using System;

class Candidate
{
string skills;
int age;
public string CanSkills
{
get
{return skills;}
set { skills = value;}
}
public int CanAge
{
get{return age;}
set{age = value;}
}
}

class Hr
{
public static void Main()
{
Candidate shubham = new Candidate();
shubham.CanSkills = “A, B, C”;
shubham.CanAge = 23;
Console.WriteLine(“Skill set of Shubham is “+shubham.CanSkills);
Console.WriteLine(“Age of shubham is “+shubham.CanAge);
}
}

Output:

Skill set of Shubham is A, B, C

Age of shubham is 23

I hope this article remains helpful to you in understanding the concept of Properties in C#. Please do like, comment and share this article. Because Sharing is Caring! This article would definitely help in understanding basic concepts.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.