Use Properties in C#

Properties is used for the access (read and write values) of the private variables of class. Member variables or methods in a class or structures are called Fields. Properties are special kinds of class fields and are accessed using the same syntax.

Keywords that can be used with C# properties

Properties can be marked as private, internal, public or protected internal. These access modifiers determine how you can access the property. A single property can have different access modifiers for the get and set accessors. For instance, the set accessor may be protected or private while the get accessor may be public to allow read-only access.

For Example:

  1. private string address = “park street”;  
  2. public string Address // Properties name  
  3. {  
  4.     get   
  5.     {  
  6.         return address;  
  7.     }  
  8.     protected set  
  9.     {  
  10.         Address = value;  
  11.     }  
  12. }   

In the above example, the Address property includes a set and get accessor. The get accessor attains the accessibility level of the property, which in this case is public. The set accessor has protected as the access modifier; it is explicitly restricted. Properties declared as abstract have no implementation in the base class; derived classes write their own property implementation.

The static keyword can also be used for declaring a property. You can access a static property even if no instance of a class exists. The virtual keyword can be used to mark a property. This will allow you to override the property behavior in a derived class. The overridden property can be sealed to indicate it is no longer virtual in the derived class.

Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace cSharpCorner   
  6. {  
  7.     class Program   
  8.     {  
  9.         private string code = "OB12";  
  10.         private string name = "Amonium";  
  11.         private bool latest = true;  
  12.         // Declare a Code property of type string:  
  13.         public string Code   
  14.         {  
  15.             get {  
  16.                 return code;  
  17.             }  
  18.             set {  
  19.                 code = value;  
  20.             }  
  21.         }  
  22.         // Declare a Name property of type string:  
  23.         public string Name   
  24.         {  
  25.             get {  
  26.                 return name;  
  27.             }  
  28.             set {  
  29.                 name = value;  
  30.             }  
  31.         }  
  32.         // Declare a Age property of type int:  
  33.         public bool Latest  
  34.         {  
  35.             get {  
  36.                 return latest;  
  37.             }  
  38.             set {  
  39.                 latest = value;  
  40.             }  
  41.         }  
  42.         public override string ToString()  
  43.         {  
  44.             return "Code = " + Code + ", Name = " + Name + ", Latest = " + Latest;  
  45.         }  
  46.     }  
  47.     class ExampleDemo   
  48.     {  
  49.         public static void Main()   
  50.       {  
  51.             // Create a new Program object:  
  52.             Program s = new Program();  
  53.             // Setting code, name and the Latest of the Chemical  
  54.             s.Code = "001";  
  55.             s.Name = "Sodium";  
  56.             s.Latest = false;  
  57.             Console.WriteLine("chemical Info: {0}", s);  
  58.             s.Code = "002";  
  59.             s.Name = "Water";  
  60.             s.Latest = false;  
  61.             s.Latest = true;  
  62.             Console.WriteLine("chemical Info: {0}", s);  
  63.             Console.ReadKey();  
  64.         }  
  65.     }  
  66. }   

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now