Get Set Modifier in c#

Total Post:341

Points:2389
C#  .NET 
C#
 765  View(s)
Ratings:
Rate this:
How to use Get Set Modifier in c# please help me
  1. Post:281

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

      Modified On Apr-10-2018 06:09:12 AM

Answer

NEWSLETTER

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