Properties, How it is different from variable fields.
Properties are a natural extension to fields.
Unlike a variable, a property is not stored in a memory location.
It is made up of functions. Thus even though a property and a field share the same syntax a property has the advantage that code gets called.
When we initialize a variable, no code in our class gets called.
We are not able to execute any code for a variable access or initialization at all. In the case of a property, we can execute tons of code.
This is one singular reason for the popularity and the use of properties.
i.e. One simple example is setting the value of a variable. If it is through a variable, we have no control over the value used. If the same access is through a property, the programmer has no inkling of whether it is a property or a variable, we can build range checks to make sure that the variable does not cross certain bounds.
public class zzz
{
public static void Main()
{
aa a = new aa();
int gg = a.ff + 9;
System.Console.WriteLine(gg);
}
}
public class aa
{
public int ff {
get
{
System.Console.WriteLine(“in get”);
return 12;
}
}
}
Output
in get
21
A property should have at least one accessor, in our case, a get as we want to read the value of the property. Thus a.ff calls the get accessor which returns an int, in this case 12. If we did not have access to the code of the class aa, we would have assumed ff to have been a variable.
a.cs
public class zzz
{
public static void Main()
{
aa a = new aa();
a.ff = 19;
System.Console.WriteLine(a.ff);
}
}
public class aa
{
public int ff {
get
{
System.Console.WriteLine(“in get”);
return 12;
}
set
{
System.Console.WriteLine(value);
}
}
}
Output
19
in get
12
The reason we use a property and not a variable is because if we change the value of a variable/field, then code in our class is not aware of the change. Also we have no control over what values the variable will contain. The user can change them to whatever he/she likes and we cannot implement range checks on the variable. Also the user may want to associate some action with the changes in the value of the variable. Using a property, reading or writing to the variable also can be monitored.
a.cs
public class zzz
{
public static void Main()
{
aa a = new aa();
a.ff = 19;
System.Console.WriteLine(a.ff);
}
}
public class aa
{
int f1;
public int ff {
get
{
System.Console.WriteLine(“in get”);
return f1;
}
}
}
Compiler Error
a.cs(6,1): error CS0200: Property or indexer ‘aa.ff’ cannot be assigned to — it is read only
You are allowed to declare a property readonly by omitting the set accessor. No one is now allowed to change the value of the property. It now behaves as a const or readonly field.
Indexers
An indexer lets us access members of a class as if it were an array.
a.cs
public class zzz
{
public static void Main()
{
yyy a = new yyy();
a[1] = 10;
}
}
public class yyy {
}
Compiler Error
a.cs(6,1): error CS0021: Cannot apply indexing with [] to an expression of type ‘yyy’
We have created an object a that looks like yyy. The object a, in no sense of the word is an array. We are assuming that a is an array and we’ve used the array syntax a[], hence it gives us an error.
a.cs
public class zzz
{
public static void Main()
{
yyy a = new yyy();
a[1] = 10;
}
}
public class yyy
{
public int this[int i]
{
set {
System.Console.WriteLine(“in get “ + value + “ “ + i);
}
}
}
Output
in get 10 1
Indexers
An indexer lets us access members of a class as if it were an array.
a.cs
public class zzz
{
public static void Main()
{
yyy a = new yyy();
a[1] = 10;
}
}
public class yyy {
}
Compiler Error
a.cs(6,1): error CS0021: Cannot apply indexing with [] to an expression of type ‘yyy’
We have created an object a that looks like yyy. The object a, in no sense of the word is an array. We are assuming that a is an array and we’ve used the array syntax a[], hence it gives us an error.
a.cs
public class zzz
{
public static void Main()
{
yyy a = new yyy();
a[1] = 10;
}
}
public class yyy
{
public int this[int i]
{
set {
System.Console.WriteLine(“in get “ + value + “ “ + i);
}
}
}
Output
in get 10 1
No comments:
Post a Comment