Properties in c#

A property is a member that provides a flexible mechanism to read the private data or to update the private data. these properties can also be used to compute on the variable. with this the data can be accessible easily and helps to promote safety of data.

SYNTAX :

public datatype Propertyname
{
   get
     {
              return var;
     }
   set
    {
                var = value;
    }

}

Get : this is used the retrieve the value present in the private variable
Set : this is used to update the private variable



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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
       {
           employee e = new employee();
           e.ReadData();
            employee f = new employee(e);// classname obj = new classname(orj)
           f.getData();
           Console.WriteLine("sal : " + f.Sal);
           //Syntax to create array of objects
           Console.Read();
        }
    }
    class employee
    {
        private int eno;
        private string eName;
        private double sal;
        public double Sal
        {
            get
            {
                return sal;
            }
            set
            {
                sal = value ;
            }
        }
        
        
        public employee()
        {

        }

        public employee(employee prev)
        {
            eno = prev.eno;
            eName = prev.eName;
            sal = prev.sal;
        }

        public void getData()
        {
            Console.WriteLine("Eno :-" + eno + " Ename :-" + eName+ " Ename :-" + eName);
        }
        public void ReadData()
        {
            Console.Write("enter Eno :");
            eno = Convert.ToInt32(Console.ReadLine());
            Console.Write("enter Ename :");
            eName = Console.ReadLine();
            Console.Write("enter Sal :");
            sal = Convert.ToDouble(Console.ReadLine());

        }
    }
}

Properties in c# Properties in c# Reviewed by Share less to Learn More on 7:05 PM Rating: 5

No comments