Home > DeveloperSection > Forums > How to use property in c#
Ankit Singh

Total Post:341

Points:2389
Posted on    January-20-2016 10:11 PM

 C# C# 
Ratings:


 1 Reply(s)
 159  View(s)
Rate this:
I want to use property in c# how to do this please help me.


aditya kumar Patel

Total Post:245

Points:1743
Posted on    January-20-2016 10:19 PM

A property is a data member. in a class have multiple data members. which is easily access by class object. we can get and set a value of property.

using System;
namespace tutorialspoint
{
    class Moters
    {
        private string engineno = "ADHG54578JAS";
        private string _modelname = "maruti suzuki swift";
 
        public string EngineNo
        {
            get
            {
                return engineno;
            }
            set
            {
                engineno = value;
            }
        }

      
        public string ModelName
        {
            get
            {
                return _modelname;
            }
            set
            {
                _modelname = value;
            }
        }

     
        public override string ToString()
        {
            return "EngineNo = " + EngineNo + ", ModelName = " + ModelName ;
        }
    }

    class Example
    {
        public static void Main()
        {
            Moters m = new Moters();        
            m.EngineNo = "2164DGAHSGD";
            m.ModelName = "safari storme ";
            Console.WriteLine("motors info: {0}", m);
            Console.WriteLine("motors info: {0}", m);
            Console.ReadKey();
        }
    }
}


Don't want to miss updates? Please click the below button!

Follow MindStick