September 26, 2017
C# properties
C# allows properties definition. We can use a property to provide a read only, write only, or read/write access of class variables. The following is example of read/write, read, and write access of a variable named “firstname” using properties:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication6 { class Program { string firstname="this is default value"; public String Name { get { return firstname; } set { firstname = value; } } public String Name1 { get { return firstname; } } public String Name2 { set { firstname=value; } } static void Main(string[] args) { Program p = new Program(); // p.Name = "Ali"; // Console.WriteLine(p.Name); // Console.WriteLine(p.Name1); p.Name2 = "Pakistan"; Console.WriteLine(p.Name1); } } }