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(); } } }
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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.