Properties in C-Sharp in detail with example  

A property is a public data member , provides a flexible mechanism to read, write, or compute the value of a private field. In otherwords, if you want to access private members outside class then we use properties in C#.

0 344 Technology | C-Sharp
Tue Dec 25 , 2018

Properties are specific methods called accessors. The accessor of a property contains the executable statements that helps in getting (reading or computing) or setting (writing) the property.

The accessor declaration can contain a get accessor, a set accessor, or both.
Get Accessor: It specifies that the value of a field can access publicly. It returns a single value and it specifies the read-only property.
Set Accessor: It will specify the assignment of a value to a private field in a property. It returns a single value and it specifies the write-only property.


Syntax : Property declaration


<access_modifier> <return_type> <property_name>
{
 get 
 { 
  // body 
 }

 set
 { 
  // body 
 }
}


Types of Properties :

  • Read/Write Properties: A property with both get and set accessor is a Read/Write property.
  • Read Only Properties: A property with only get accessor is a Read only property.
  • Write Only Properties: A property with only set accessor is a Write only property.
  • Static Properties: A property may be declared as a static property by using the static keyword. It is used to access static members only and they are accessed through the name of the class rather than object of the class.
  • Abstract Properties: A property may be declared as a abstract property by using the abstract keyword, indicating that it has no implementation in the base class. The base class indicates the property's type and whether it has get and set accessors in any derived classes. A base class that defines an abstract property must also be marked as abstract itself.

Example: C# properties

using System;
public class Vehicle
{
      private string color;
      private string name;
      private int no_of_wheels;
      private static string model_no; 
      
      // Declare a Prop_Color property of type string:
      public string Prop_Color 
      {
         get 
	 {
            return color;
         }
         set 
	 {
            color = value;
         }
      }
      
      // Declare a Prop_Name property of type string:
      public string Prop_Name
      {
         get 
	 {
            return name;
         }
         set 
	 {
            name = value;
         }
      }
      
      // Declare a Prop_No_of_wheels property of type int:
      public int Prop_No_of_wheels 
      {
         get 
	 {
            return no_of_wheels;
         }
         set 
	 {
            no_of_wheels = value;
         }
      }

      // Declare a Prop_Model_no static property of type string:
      public static string Prop_Model_no
      {
         get 
	 { 
	    return model_no;
         }
         set 
	 {
            model_no = value;
         }
      }
      
      public static void Main() 
      {
         // Create a new Vehicle object:
         Vehicle v = new Vehicle();
         
         // Setting color, name and the no_of_wheels of the vehicle
         v.Prop_Color  = "white";
         Console.WriteLine(v.Prop_Color); 
         v.Prop_Name = "bmw";
	 Console.WriteLine(v.Prop_Name); 
         v.Prop_No_of_wheels = 4;
         Console.WriteLine(v.Prop_No_of_wheels); 

	 //Setting model_no of the vehicle
         Vehicle.Prop_Model_no="ConceitCode7102018";
         Console.WriteLine(Vehicle.Prop_Model_no); 																			  
      }
   } 

Output

Color  : white
Name : bmw
Wheel : 4
Model : ConceitCode7102018


Subscribe & Get Regular Updates On Your E-mail