C# properties Edit

Rakesh Kumar Sutar | 20 January 2021 | 119

Introduction

Properties in C# is used to access private variables using get and set method.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello_World
{
    
    class Program
    {
        private string name; // field
        public string Name   // property
        {
            get { return name; }
            set { name = value; }
        }
        static void Main(String[] args)
        {
            Program myObj = new Program();
            myObj.Name = "Rakesh Kumar Sutar";
            Console.WriteLine("     " myObj.Name);
            Console.ReadLine();
        }
    }
}

Output