see http://www.csharp-station.com/Tutorials/Lesson10.aspx
Overview of Properties
Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. In other languages, this is often accomplished by programs implementing specialized getter and setter methods. C# properties enable this type of protection while also letting you access the property just like it was a field.
Another benefit of properties over fields is that you can change their internal implementation over time. With a public field, the underlying data type must always be the same because calling code depends on the field being the same. However, with a property, you can change the implementation. For example, if a customer has an ID that is originally stored as an int, you might have a requirements change that made you perform a validation to ensure that calling code could never set the ID to a negative value. If it was a field, you would never be able to do this, but a property allows you to make such a change without breaking code. Now, lets see how to use properties.
continued: http://www.csharp-station.com/Tutorials/Lesson10.aspx
from http://www.devarticles.com/c/a/C-Sharp/Understanding-Properties-in-C-Sharp/
There is syntax for putting get and set into a member property without any code at all:
Abstract Properties
A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.
If the abstract class contains only set accessor, we can implement only set in the derived class.
The following program shows an abstract property in action.
//C# : Property : Abstract
//Author: rajeshvs@msn.comusing System;
abstract class Abstract
{
public abstract int X
{
get;
set;
}
}
class Concrete : Abstract
{
public override int X
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
}
class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1.X = 10;
Console.WriteLine(c1.X);//Displays 'SET GET 10'
}
}
Read more - http://w.po.st/share/entry/redir?publisherKey=www.devshed.com-591&url=http%3A%2F%2Fwww.devarticles.com%2Fc%2Fa%2FC-Sharp%2FUnderstanding-Properties-in-C-Sharp%2F&title=Understanding%20Properties%20in%20C%23&sharer=copypaste
Overview of Properties
Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. In other languages, this is often accomplished by programs implementing specialized getter and setter methods. C# properties enable this type of protection while also letting you access the property just like it was a field.
Another benefit of properties over fields is that you can change their internal implementation over time. With a public field, the underlying data type must always be the same because calling code depends on the field being the same. However, with a property, you can change the implementation. For example, if a customer has an ID that is originally stored as an int, you might have a requirements change that made you perform a validation to ensure that calling code could never set the ID to a negative value. If it was a field, you would never be able to do this, but a property allows you to make such a change without breaking code. Now, lets see how to use properties.
continued: http://www.csharp-station.com/Tutorials/Lesson10.aspx
from http://www.devarticles.com/c/a/C-Sharp/Understanding-Properties-in-C-Sharp/
There is syntax for putting get and set into a member property without any code at all:
Abstract Properties
A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.
If the abstract class contains only set accessor, we can implement only set in the derived class.
The following program shows an abstract property in action.
//C# : Property : Abstract
//Author: rajeshvs@msn.comusing System;
abstract class Abstract
{
public abstract int X
{
get;
set;
}
}
class Concrete : Abstract
{
public override int X
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
}
class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1.X = 10;
Console.WriteLine(c1.X);//Displays 'SET GET 10'
}
}
Read more - http://w.po.st/share/entry/redir?publisherKey=www.devshed.com-591&url=http%3A%2F%2Fwww.devarticles.com%2Fc%2Fa%2FC-Sharp%2FUnderstanding-Properties-in-C-Sharp%2F&title=Understanding%20Properties%20in%20C%23&sharer=copypaste
No comments:
Post a Comment