|
C# Properties Tutorial
|
What are properties and why they are used? |
Properties are used to protect a field in a class by reading and writing to it through
the property.Unlike fields, properties do not designate storage locations instead
they refer the underlying fields. Before properties came into existence, let us
see how we use to protect fields inside a class with Getter and setter methods.
The code snippet below shows a Customer class with a private field.
If we have to read or write to this field, the only way is to access them thru
the respective getter and setter methods.
In the example below GetFirstName() method returns the value present in _firstName
private field.SetFirstName() method will set the private field _firstName with the
value in the parameter that we pass to the method. In the Main method we create
an instance of the Customer class and call the SetFirstName() method passing it
a string parameter and finally we print the value present in the _firstName private
field onto the console using GetFirstName() method. |
|
using
System;
public
class Customer
{
private
string _firstName;
public
string GetFirstName()
{
return _firstName;
}
public
void SetFirstName(string
FirstName)
{
_firstName = FirstName;
}
}
class
Program
{
public
static void Main()
{
Customer C = new
Customer();
C.SetFirstName("Prasad");
Console.WriteLine(C.GetFirstName());
}
}
|
Lets write the same
class using Csharp properties. Example below demonstrates the same.We have a privtae field in the class, which is exposed to the outside world using properties. Properties
in csharp has two accessors, get and set.The get accessor returns the value of the
field.The set accessor sets the value of the field with the contents of value. The
value shown in the set accessor is a C# reserved keyword. |
|
using
System;
namespace
Test
{
public
class Customer
{
private string _firstName;
//Private Field declaration
public string FirstName
//Property Declaration
{
get
//get
accessor, returns the value present in _firstName private
{
//field of the class
return _firstName;
}
set
//set
accessor, assigns the passed in value to the _firstName
{
//private field of the class
_firstName = value;
}
}
}
class
Program
{
public static void
Main()
{
Customer C = new
Customer();
//Instantiate
the Customer Class
C.FirstName
= "Parsad";
//set accessor of FirstName property
is invoked
Console.WriteLine(C.FirstName);
//get accessor of FirstName property is invoked
}
}
}
|
Properties in C# can be
Read Only : Read Only properties will have only the get accessor. To make the FirstName
a readonly property, remove the set accessor.
Write Only : Write Only properties will have only the set accessor. To make the
FirstName a writeonly property, remove the get accessor.
Both : Will have both get and set accessors.
|
Static Properties in C# |
|
Properties in csharp be also be static.A property declared using the static modifier
is classified as a static property; otherwise, it is classified as an instance property.
Like other static members, a static property is not associated with a specific instance
and cannot be referenced through an instance. Instead, it is associated with the
type and can only be referenced through the type name. It is an error to use a virtual,
abstract, or override modifiers on a static property declaration.When a property
declaration includes a static modifier, the property is said to be a static property.
When no static modifier is present, the property is said to be an instance property.
A static property is not associated with a specific instance, and it is a compile-time
error to invoke a static property on an instance(object) of a class. On the other
hand, non static properties also called as instance properties can be invoked only
on an instance of a class and not on the name of the class. Example below
demonstrates the same |
|
using
System;
public
class Customer
{
private
static int _customerID=10;
//Declare a static variable to hold customer id
public
static int CustomerID //Declare
a public static property to return customer id
{
//from the _customerID
private field
get
{
return _customerID;
}
}
}
class
Program
{
public
static void Main()
{
//As CustomerID property is static we can call this property on the name of the
class
Console.WriteLine(Customer.CustomerID.ToString());
Customer C = new
Customer();
//You cannot call static properties on instance of a class.
//Line below would generate an error
Console.WriteLine(C.CustomerID.ToString());
}
}
|
Properties in interfaces in C# |
Properties can be declared on interfaces as well.We will discuss about interfaces
in our later articel. As interfaces will contain only declarations and not implementations,
The accessor of an interface property should not have a body. The purpose of the
accessors in interfaces is to indicate whether the property is read-write, read-only,
or write-only.The following is an example of an interface property.
In the example below:
- we have interface ICustomerInterface which has a Name
property without implementation i.e no get and set accessor implementation.
- Customer class inherits from ICustomerInterface,
and provides implementation for the Name property inherited from
ICustomerInterface.
|
|
using
System;
public
interface ICustomerInterface
{
string
Name //Interface Property declaration.
{
get;
//get accessor without body
set;
//set accessor without body
}
}
public
class Customer
: ICustomerInterface
//Customer class implements the ICustomerInterface
{
private
string
_name;
//Declare a private variable to hold
customer name
public
string Name
//Implement the Name property inherited from the Interface
{
get
{
return _name;
}
set
{
_name
= value;
}
}
}
class
Program
{
public
static void Main()
{
Customer C = new
Customer();
C.Name =
"Prasad";
Console.WriteLine(C.Name);
}
}
|
|
Other related articles :
|
Njoy Programming
ByPrasad Cherukuri
|