Introduction
A property is a type of class member that combines a field that we can access it. Let us suppose we are willing to fix a limit of a variable, like not more then 10; for that type of condition, while it is possible to accomplish this condition (goal) with a private variable along with a method to access its value, a property is the right solution for that. Properties are similar to indexers. When we use a property, we only use get and set accessors. These accessors are used to get and set the value of a variable. The benefit of a property is we use the variable name as it is in the expression to assign it like a normal variable.
Syntax of Properties
type name {
get {
// get accessor code
}
set{
// set accessor code
}
}
As in the preceding syntax, type specifies the type of property such as int, string, and so on. name is the name of the property. Once the property has been defined then name is called to the appropriate accessor. We should understand the main key point of a property; that is, do not define the storage location. A property accesses the field itself.
Let us understand the property using an example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace properties
- {
- class Program
- {
- static void Main(string[] args)
- {
- PropertyExample obj = new PropertyExample();
- Console.WriteLine("Initial value of the obj.myProperty is " + obj.myProperty);
- obj.myProperty = 812;
- Console.WriteLine("Now the obj.myProperty is " + obj.myProperty);
- obj.myProperty = -23;
- Console.WriteLine("When we pass the negative value "+ obj.myProperty);
- Console.ReadKey();
- }
- }
- public class PropertyExample
- {
- private int x;
- public PropertyExample()
- {
- x = 0;
- }
- public int myProperty
- {
- get { return x; }
- set
- {
- if (value > 0)
- x = value;
- }
- }
- }
- }
This program has one private field x and a property myProperty that manages access to x. As we see in the definition section, a property does not define a storage location. Instead properties simply manage access to a field. myProperty is specified as public, that can be accessed by code outside of the class and private variable x that manages it. The get accessor simple returns the value to x and a set accessor sets the value of x with a condition. The type of property defined by myProperty is called a read-write property, that means it allow its underlying field to read and write. It is also possible to make read-only and write-only properties. To create a read-only property we need to define only a get accessor and same write- only property define only a set accessor.
Auto-Implemented Properties
In C# 3.0 or above versions of C#, it became possible to implement a very simple property without having to explicitely define the variable managed by the property. Instead, we can let the compiler automatically supply the underlying variable. This is called an auto-implemented property.
Syntax
type name { get; set ; }
In this syntax type specifies the type of the property and name specifies the name. get and set are immediately followed by a semicolon. The accessors is an auto-implemented property with no bodies. This syntax tells the compiler to create a storage location (known as a backing field) that holds the value. These variables can be accessed only through the property.
Object Initialize with Properties
We can initialize our object. In this we can explicitely call a constructor, like where creating an object, when we use an object initilizer, we need to specify the initial values for that field and properties that we want to initialize.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace properties
- {
- class Program
- {
- static void Main(string[] args)
- {
- emp obj = new emp( );
- obj.empId = 102;
- obj.empName = "rajeev";
- Console.WriteLine(obj.empId +" "+ obj.empName);
- Console.ReadKey();
- }
- }
- class emp
- {
- public int empId { get; set; }
- public string empName { get; set; }
- }
- }
Property limitations
Properties have some important limitations.
- A property does not define a storage location. In other words it cannot be used in a ref or out parameter of a method.
- We cannot overload a property.
- We cannot alter the variable when the get accessor is called.
Summary
In this article we learned about properties. The basic syntax and why we use properties in C# and what the the benefits and limitations of properties are.