Home > DeveloperSection > Forums > Get Set Modifier in c#
Ankit Singh

Total Post:341

Points:2389
Posted on    November-25-2015 9:56 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 255  View(s)
Rate this:
How to use Get Set Modifier in c# please help me


aditya kumar Patel

Total Post:250

Points:1782
Posted on    November-25-2015 10:03 PM

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();
        }
    }
}


Don't want to miss updates? Please click the below button!

Follow MindStick