3
Q:

c# accessors

//c# property get set example
		float amount {get; set;}        
        static float interest = 9.5f;
        public float InitialAmount{
            set{ if(value > 1000) Console.Write("Nope must be < 1000"); }
            get{ return amount;}
        }
        public static float InterestRate
        {
            get {return interest;} //Read only ... no setter
        }
        public static float InterestRate2
        {
        	// no external class can assign this value ... it's private
            private set{interest= value;} 
            get{return interest;}
        }
2
private string _name = "Hello";

public string Name
{
    get
    {
        return _name;
    }
    protected set
    {
        _name = value;
    }
}
0
Person person = new Person();
person.Name = "Joe";  // the set accessor is invoked here

System.Console.Write(person.Name);  // the get accessor is invoked here
0

New to Communities?

Join the community