GET SET MODIFIER IN C#

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  .NET 
C#
 502  View(s)
Ratings:
Rate this:
How to use Get Set Modifier in c# please help me
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: Get Set Modifier in c#

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!