Introduction to Properties in C#, Properties in C#, Properties C#, c# PropertiesHello friends, I hope you all are doing great. In today’s tutorial, we will have a look at Properties in C#. It’s our 20th tutorial in C# Series and a quite important one as we are gonna use properties a lot in our coming lectures.

If you haven’t studied C# Methods & C# Classes then I would suggest you to read them first before starting today’s tutorial, as we are gonna use methods today. So, let’s get started with Properties in C#:

Introduction to Properties in C#

  • Introduction to Properties in C#, Properties in C#, Properties C#, c# PropertiesToday I am not gonna start with the definition as I did in previous tutorial, instead we will first understand the concept and then will define it.
  • So, let’s create a new C# Class named StudentsClass shown in figure on right side.
  • this class has 3 fields in it, first one is int and other two are string and all these three fields are public.
  • Moreover, it has a C# Method in it, which is also public and printing the Full Name.
  • In our Main Function, I have created new instance of this Class and then invoked the Method.
  • So what if, we want to add some restrictions on these fields i.e. we can’t have a negative roll number & the name fields can’t be null.
  • We don’t want garbage values as well, to pass on to our project so its always wise to use private fields instead of public and place some controls on the coming values.
  • Introduction to Properties in C#, Properties in C#, Properties C#, c# PropertiesWe can do this by using C# Methods, as I did in this right image. ( you may need to click the image to look at the zoomed version)
  • So, here I have created two C# Methods, which are receiving values from external classes and then I have placed the check on the coming value and if it satisfies the condition then I have passed it on to my field or variable.
  • Now the field is also private, instead of public.
  • But you must be wondering that its a lot of work to add two functions for every field, so here comes the properties in C#.
  • In the below figure, I have created a C# Property named RollNo for the private field _RollNo and then I have used accessor in C#, which are:
    • set: To save the value of Property.
    • get: To read the value of Property.

Introduction to Properties in C#, Properties in C#, Properties C#, c# Properties

  • In the above figure, you can see that I have created a property named RollNo and in that property, I have used set accessor & get accessor.
  • Moreover, in the Main function, now we are treating RollNo as a Property and using dot operator to set or get the value.
  • Instead of using the setRollNo function, we are simply using SC.Roll = 20; to set the Roll no. and compiler will automatically move to set accessor of this property and the value we will assign it i.e. 21 it will be used by the value keyword.
  • You can see in set accessor that, I have used value keyword and this keyword will have the value coming from invoking request i.e. 21 in our case.
  • Similarly, when we want to read the Roll No, we are using SC.RollNo and the compiler will automatically know that it need to move into RollNo Property and then check the get accessor.
  • So, we can say that our property RollNo is a read / write property, if it just has the get accessor then it will be read only property.

Auto Implemented Properties in C#

  • In the above case, we have seen tha we have to place some logic in our set & get accessor, but in most of the cases, we don’t need to add any additional logic in our accessors.
  • For example, I am creating fields for City, Phone Number, Email Address etc. then in such cases, we can make use of C# Auto-Implemented Properties which were introduced in C# 3.0.
  • In the below figure, I have created  Properties with get & set accessors:

Introduction to Properties in C#, Properties in C#, Properties C#, c# Properties

  • You can see that now I have created 3 new C# Properties and haven’t even created any field for them, that’s created & implemented automatically by C#.
  • So, when I create a C# Property then its auto-Implemented its private field, which we can control by using set & get accessors, if we want to.
  • So, our whole field vanished & protected and our new Property code is also lies in just one line, so kind of brilliant idea, introduced in C# 3.0.

Object Initializer Syntax

  • Now let’s have a look at a new Object Initializer Syntax, which was also introduced in C# 3.0 and I think it’s best among all.
  • Let’s instantiate our StudentsClass using this new Object Initializer Syntax, as shown in below figure:

Introduction to Properties in C#, Properties in C#, Properties C#, c# Properties

  • You can see in above figure that it’s now quite simple to assign & get data to & from Class Properties.
  • I am updating all Members of C# Class in just single line, although you can add single property per line as well, but I like it that way, simple and clear.

So, that was all about Properties in C# and I hope now you can set & get them quite easily. In our coming tutorial, we will have a look at Delegates in C#. Till then take care & have fun !!! 🙂

Leave a Reply