C# Properties

 

using System;


namespace Propertytest
{
    class Person
    {
        private int age;
        private string gender;

        public int Age
        {
            get { return age; }
            set { age = value+40; }
        }


        public string Gender
        {
            get { return gender; }
            set
            {
                if (value =="male"||value=="female")
                {
                    gender = value;
                }
            }
        }

    }


    class Program
    {

        static void Main(string[] args)
        {
            Person tom=new Person();

            tom.Age = 30;
            Console.WriteLine(tom.Age);
            tom.Gender = "male";
            Console.WriteLine(tom.Gender);
            Console.ReadKey();
        }
    }
}

 

Read Only:

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

namespace ReadOnly
{
    public class Employee
    {
        private static int counter;

        public Employee()
        {
            counter++; //here counteer is increasing
        }

        public static int Counter
        {
            get { return counter; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee e1=new Employee();
            Employee e2=new Employee();
            Employee e3=new Employee();

            Console.WriteLine("No. of employees: "+Employee.Counter);
            Console.ReadKey();
        }
    }
}

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *