Showing posts with label Property. Show all posts
Showing posts with label Property. Show all posts
Sunday, October 20, 2013
Friday, May 31, 2013
Auto-Implemented Properties
We have seen many types of properties so far, you would have notices
that defining a simple get/set property involves many lines of code. A simple
private variable with a public property definition needs about 10 lines of
code, if this is the case then complex class with many properties will have
hundreds of lines of code. Auto
Implemented properties help us to overcome this by simplifying property
definition of simple properties.
Auto-Implemented Properties were introduced in .Net 3.0 to simplify property definition; the following example defines a 2 auto implemented properties Name and Age with just 2 lines of code.
Auto-Implemented Properties were introduced in .Net 3.0 to simplify property definition; the following example defines a 2 auto implemented properties Name and Age with just 2 lines of code.
Static Properties
Static members of a class are those which can be
accessed without creating an instance of the containing class, similarly static
properties can be assessed directly by prefixing the property name with the
class name and a dot (.). Static
properties should have the keyword static mentioned in their definition.
In the following example we will
define a static property and access
the same directly without creating an instance of the class.
Abstract Properties
Abstract
Properties
are similar to Interface Properties, Abstract
Properties are those which are defined in an Abstract class, similar to
Interface Properties they do not have any definition, we just need to define the
get {} and/or set {} accessors.
The class which inherits the abstract class will use the property and define the get {} and/or set {} accessors in the derived class.
The following example defines an abstract class with an abstract property.
The class which inherits the abstract class will use the property and define the get {} and/or set {} accessors in the derived class.
The following example defines an abstract class with an abstract property.
Thursday, May 30, 2013
Interface Properties
Interface properties are those which are
defined in the interface and implemented in the class which implements the
interface. Interface properties do not have a definition, they just specify the
name of the property and its accessibility Read-Write, or Read-Only or
Write-Only.
The following example defines an Interface Property Name in the interface IEmployee
The following example defines an Interface Property Name in the interface IEmployee
Properties with Validation
In general properties have a get {} and set {} accessor which is used to get
and set the values of the underlying private member in the class, these
properties just make sure that the values which is being set from the external
source matches the expected data type of the underlying private member. Apart
from this they do not perform any other validation.
There are specific where the value to be set to a private member should satisfy certain business validations, since the member is private there is no way to set the value directly from external sources, the only way to set a value to the member is through the set {} accessor, we can perform a set of validations in the set {} accessor before assigning values to the variable.
There are specific where the value to be set to a private member should satisfy certain business validations, since the member is private there is no way to set the value directly from external sources, the only way to set a value to the member is through the set {} accessor, we can perform a set of validations in the set {} accessor before assigning values to the variable.
In the following example we impose a
simple validation to the UserName property, the value assigned to the property should
be a string of length greater than or equal to 8, else an exception should be
thrown.
Write only Properties
As the name suggests write-only properties are those which can be assigned from external
classes but the value cannot be read from any external class.
In general a property has get {} and set {} accessors,
to create a write-only property we
just need to define the set {} accessor for the property without the get {} accessor.
The following example defines a write-only property for the private variable strName
Read only Properties
As the name suggests read-only properties are those which can be consumed by external
classes but cannot be reset by any operation external to the class.
In general a property has get {} and set {} accessors,
to create a read-only property we
just need to define the get {} accessor for the property without the set {} accessor.
The following example defines a read-only property for the private variable strName
Wednesday, May 29, 2013
What is a Property?
Object oriented programming
languages support encapsulation, which is also called as data hiding. Encapsulation
is hiding the private variables of a class from the external world, if we are
hiding the members then how do we get and set the values of these members? It
is when Properties become significant.
Properties define get and set assessors which can be used to get and set values of private variables in a class.
Properties define get and set assessors which can be used to get and set values of private variables in a class.
Subscribe to:
Posts (Atom)