Properties in C#

Introduction

A property is a type of class member that combines a field that we can access it. Let us suppose we are willing to fix a limit of a variable, like not more then 10; for that type of condition, while it is possible to accomplish this condition (goal) with a private variable along with a method to access its value, a property is the right solution for that. Properties are similar to indexers. When we use a property, we only use get and set accessors. These accessors are used to get and set the value of a variable. The benefit of a property is we use the variable name as it is in the expression to assign it like a normal variable.

Syntax of Properties

type name {
                     get {
                                // get accessor code
                            }
                      set{
                                // set accessor code
                            }
                    }

As in the preceding syntax, type specifies the type of property such as int, string, and so on. name is the name of the property. Once the property has been defined then name is called to the appropriate accessor. We should understand the main key point of a property; that is, do not define the storage location. A property accesses the field itself.
Let us understand the property using an example.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.    
  7. namespace properties  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             PropertyExample obj = new PropertyExample();  
  14.             Console.WriteLine("Initial value of the obj.myProperty is " + obj.myProperty);  
  15.             obj.myProperty = 812;  
  16.             Console.WriteLine("Now the obj.myProperty is " + obj.myProperty);  
  17.             obj.myProperty = -23;  
  18.             Console.WriteLine("When we pass the negative value "+ obj.myProperty);  
  19.             Console.ReadKey();  
  20.         }  
  21.     }  
  22.     public class PropertyExample  
  23.     {  
  24.         private int x;  
  25.         public PropertyExample()  
  26.         {  
  27.             x = 0;  
  28.         }  
  29.         public int myProperty  
  30.         {  
  31.             get { return x; }  
  32.             set  
  33.             {  
  34.                 if (value > 0)  
  35.                     x = value;  
  36.             }  
  37.         }  
  38.     }  
  39. }  

property

This program has one private field x and a property myProperty that manages access to x. As we see in the definition section, a property does not define a storage location. Instead properties simply manage access to a field. myProperty is specified as public, that can be accessed by code outside of the class and private variable x that manages it. The get accessor simple returns the value to x and a set accessor sets the value of x with a condition. The type of property defined by myProperty is called a read-write property, that means it allow its underlying field to read and write. It is also possible to make read-only and write-only properties. To create a read-only property we need to define only a get accessor and same write- only property define only a set accessor.

Auto-Implemented Properties

In C# 3.0 or above versions of C#, it became possible to implement a very simple property without having to explicitely define the variable managed by the property. Instead, we can let the compiler automatically supply the underlying variable. This is called an auto-implemented property.

Syntax

type name { get; set ; }

In this syntax type specifies the type of the property and name specifies the name. get and set are immediately followed by a semicolon. The accessors is an auto-implemented property with no bodies. This syntax tells the compiler to create a storage location (known as a backing field) that holds the value. These variables can be accessed only through the property.

Object Initialize with Properties

We can initialize our object. In this we can explicitely call a constructor, like where creating an object, when we use an object initilizer, we need to specify the initial values for that field and properties that we want to initialize.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.    
  7. namespace properties  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.            emp obj = new emp( );  
  14.            obj.empId = 102;  
  15.            obj.empName = "rajeev";  
  16.             Console.WriteLine(obj.empId +" "+ obj.empName);  
  17.             Console.ReadKey();  
  18.         }  
  19.     }  
  20.     class emp  
  21.     {  
  22.         public int empId { getset; }  
  23.         public string empName { getset; }  
  24.     }  
  25. }  
initilizer

Property limitations

Properties have some important limitations.

  • A property does not define a storage location. In other words it cannot be used in a ref or out parameter of a method.
  • We cannot overload a property.
  • We cannot alter the variable when the get accessor is called.

Summary

In this article we learned about properties. The basic syntax and why we use properties in C# and what the the benefits and limitations of properties are.


Similar Articles