Read-Only and Write-Only Properties
You can leave one or the other (but not both) of a property’s accessors undefined by omitting its declaration.
The code will output:
- A property with only
get
accessor is called a read-only property. - A property with only
set
accessor is called a write-only property. - At least one of the two accessors must be defined, or the compiler will produce an error message.
class circle { private double _radius; public double radius //read-write property, has both get and set accessor { set { _radius = value; } get { return _radius; } } public double area //read-only property, has only set accessor { get { return 3.416F * _radius * _radius; } } } class Program { static void Main(string[] args) { circle myCircle = new circle() { radius = 13.4 }; Console.WriteLine("circle area is " + myCircle.area); } }
circle area is 613,376940135956
Accessors Accessiblity
The
Output from above code will be:
get
and set
accessors can have different access levels. The typical use case for this is to have
a public property with an internal or private access modifier on the setter.
public class Person { public string firstName, lastName; private ushort _age; public Person() { age = 0; //set age from inside class } public ushort age { get { return _age; } //age can read from outside class private set { _age = value; } //age can only be set from inside the class } public void anniversary() { Console.WriteLine("Happy birthday to You"); ++age; //set age from inside class } } class Program { static void Main(string[] args) { Person me = new Person() { firstName = "Yang", lastName = "Sopiana" }; Console.WriteLine("my age " + me.age); //read age from outside class me.anniversary(); me.anniversary(); Console.WriteLine("my age " + me.age); //read age from outside class Console.Read(); } }
my age 0
Happy birthday to You
Happy birthday to You
my age 2
Happy birthday to You
Happy birthday to You
my age 2
Automatic Property Implementation
Because properties are so often associated with backing fields, C# provides automatically implemented properties, or auto-implemented
properties, which allow you to just declare the property, without declaring a backing field. The compiler creates a hidden backing field
for you and automatically hooks up the get and set accessors to it.
class myClass { public int autoProperty // Allocates memory { set; get; } } class Program { static void Main() { myClass foo = new myClass(); Console.WriteLine("autoProperty:" + foo.autoProperty); foo.autoProperty = 40; Console.WriteLine("autoProperty:" + foo.autoProperty); Console.Read(); } }
Properties Vs Public Fields
There are many advantages of using properties than fields, as follow:
- Since properties are function members, as opposed to data members, they allow you to process the input and output, which you can’t do with public fields.
- Property can be set to read-only or write-only properties, but you can’t have these characteristics with a field.
- Property enable to differentiate between read and write access level
Static Property
Property can be set to static just like fields or static method. The characteristic of static property are:- Can not access instance members of a class
- Exist regardless of whether there are instances of the class or not
- Must be referenced by the class name, rather than an instance name, when being accessed from outside the class
0 comments:
Post a Comment