The get set modifier mostly used for storing and retrieving value from the private field. The get modifier must return a value of property type where set modifier returns void. The set modifier uses an implicit parameter called value. In simple word, the get method used for retrieving value from private field whereas set method used for storing value in private variables.
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Get_SetTuorial { class Getset { // String Variable declared as private private static string _BookName; public void print() { Console.WriteLine("\nMy Book is " + BookName); }
public string BookName //Creating BookName property { get //get method for returning value { return _BookName; } set // set method for storing value in BookName field. { _BookName = value; } } }
class MainApp { static void Main(string[] args) { Getset Obj = new Getset(); Console.Write("Enter your name:\t"); // Accepting value via BookName property Obj.BookName = Console.ReadLine(); Obj.print(); Console.ReadLine(); } } }
Liked By
Write Answer
Get Set Modifier in c#
Join MindStick Community
You have need login or register for voting of answers or question.
Anonymous User
25-Nov-2015The get set modifier mostly used for storing and retrieving value from the private field. The get modifier must return a value of property type where set modifier returns void. The set modifier uses an implicit parameter called value. In simple word, the get method used for retrieving value from private field whereas set method used for storing value in private variables.