Properties are the same as fields, however they can execute code when you access them via getters and setters. This allows for protection of class information and enforcement of business rules.
Let's look at the simplest of properties, which behaves in exactly the same manner as a field.
-
// Just a normal field for comparison
-
publicint normalField;
-
-
// Now a property
-
privateint _propertyDemo
-
-
publicint PropertyDemo
-
{
-
get
-
{
-
return _propertyDemo;
-
}
-
-
set
-
{
-
_propertyDemo =value;
-
}
-
}
You will notice that we declare a private int as well as a public int. The private int _propertyDemo
is the field that will hold the data, whereas the public int PropertyDemo
is the property that will be used to access the private field. The private and public identification names can be anything you wish, be we recommend that you use a _ (underscore) character as a prefix to denote that it is the private property field.
Properties use the get and set keywords to read and write to the private field. In the get we simply return the value of _propertyDemo in the set we assign the keyword value (which represents the value being passed in) to the private field.
Properties are used to protect input from invalid data, apply business logic or to provide security functions. Let's first have a look at data validation in our property. Let's assume that the value can only be between 1 and 12.
-
privateint _month
-
-
publicint Month
-
{
-
get
-
{
-
return _month;
-
}
-
-
set
-
{
-
if((value>=1)&&(value<=12))
-
{
-
_month =value;
-
}
-
}
-
}
This will ensure that the value of _month will always be between 1 and 12. If the user of your class passes in a value outside this range it will not be applied.
We can also implement security in the getter and setter. Security will be covered in later tutorials so we will just use verbose methods for this example.
Let's assume that only certain users can assign to the property and different users can read the value.
-
privateint _mySecureID
-
-
publicint MySecureID
-
{
-
get
-
{
-
// Accounts and General Users can read this property
-
if((USERGROUP =="accounts")||(USERGROUP =="generalusers"))
-
return _mySecureID;
-
else
-
return0;
-
}
-
-
set
-
{
-
// Only Accounts can write to this property
-
if(USERGROUP =="accounts")
-
_mySecureID=value;
-
}
-
}
Please Note: This is not a secure code example; it's purely intended to illustrate properties.
- // Just a normal field for comparison
- publicint normalField;
- // Now a property
- privateint _propertyDemo
- publicint PropertyDemo
- {
- get
- {
- return _propertyDemo;
- }
- set
- {
- _propertyDemo =value;
- }
- }
private int _propertyDemo
is the field that will hold the data, whereas the public int PropertyDemo
is the property that will be used to access the private field. The private and public identification names can be anything you wish, be we recommend that you use a _ (underscore) character as a prefix to denote that it is the private property field.- privateint _month
- publicint Month
- {
- get
- {
- return _month;
- }
- set
- {
- if((value>=1)&&(value<=12))
- {
- _month =value;
- }
- }
- }
- privateint _mySecureID
- publicint MySecureID
- {
- get
- {
- // Accounts and General Users can read this property
- if((USERGROUP =="accounts")||(USERGROUP =="generalusers"))
- return _mySecureID;
- else
- return0;
- }
- set
- {
- // Only Accounts can write to this property
- if(USERGROUP =="accounts")
- _mySecureID=value;
- }
- }
Indexers
Indexers, also known as Iterators or Smart Arrays, are very similar to properties, but they allow you to use an index on an object to obtain values. Instead of creating a property name and public/private fields you simply use the this keyword to reference the object itself. Indexers allow you to access data within a class or object by treating it like an array.
-
class demoClass
-
{
-
privatestring[] data =newstring[5];
-
publicstringthis[int index]
-
{
-
get
-
{
-
return data[index];
-
}
-
set
-
{
-
data[index]=value;
-
}
-
}
-
}
In this short example public string this
defines the indexer with an integer index. When accessed the code in the get or set executes in the same way as a property.
-
demoClass sample =new demoClass()
-
-
sample[0]="This";
-
sample[1]="Is";
-
sample[2]="a";
-
sample[3]="Indexer";
-
-
Console.WriteLine(sample[0]);
-
Console.WriteLine(sample[1]);
-
Console.WriteLine(sample[2]);
-
Console.WriteLine(sample[3]);
- class demoClass
- {
- privatestring[] data =newstring[5];
- publicstringthis[int index]
- {
- get
- {
- return data[index];
- }
- set
- {
- data[index]=value;
- }
- }
- }
public string this
defines the indexer with an integer index. When accessed the code in the get or set executes in the same way as a property.- demoClass sample =new demoClass()
- sample[0]="This";
- sample[1]="Is";
- sample[2]="a";
- sample[3]="Indexer";
- Console.WriteLine(sample[0]);
- Console.WriteLine(sample[1]);
- Console.WriteLine(sample[2]);
- Console.WriteLine(sample[3]);
No comments:
Post a Comment