Read-Only property
In some scenarios, there are certain field-data members for which we're interested only in reading value from them. In those scenarios we omit the set { } scope and write only get { } scope.
Example:
class Company
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company(string name; int Num)
{
//below statement will produce error as 'Name' is a Read-Only property, so we can't assign // value through property
//Name = name;
// we can still assign nameOfCompany the value by avoiding the use of property here and //directly assigning value to private field-data
nameOfCompany = name;
LicenseNum = Num;
}
//Notice here 'Name' property is Read-Only property
public string Name
{
get
{ return nameOfCompany; }
}
//similarly property 'LicenseNum' for private field-data licenseNumber
public int LicenseNum
{
get
{ return licenseNumber; }
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company(string name; int Num)
{
//below statement will produce error as 'Name' is a Read-Only property, so we can't assign // value through property
//Name = name;
// we can still assign nameOfCompany the value by avoiding the use of property here and //directly assigning value to private field-data
nameOfCompany = name;
LicenseNum = Num;
}
//Notice here 'Name' property is Read-Only property
public string Name
{
get
{ return nameOfCompany; }
}
//similarly property 'LicenseNum' for private field-data licenseNumber
public int LicenseNum
{
get
{ return licenseNumber; }
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
Write-Only property
When there's no logic or business rule to be written in set scope of the property then .NET has provided a very short syntax to solve the purpose which comes very handy, just omit the get { } scope from the property syntax.
Example:
class Company
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company(string name; int Num)
{
//below statement will produce error as 'Name' is a Read-Only property, so we can't //assign value through property
//Name = name;
// we can still assign nameOfCompany the value by avoiding the use of property here //and directly assigning value to private field-data
nameOfCompany = name;
LicenseNum = Num;
}
//Notice here 'Name' property is Read-Only property
public string Name
{
get
{ return nameOfCompany; }
}
//Notice here the property LicenseNum is a Write-Only property as there's no get{} scope
public int LicenseNum
{
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
{
// field-data declared private
private string nameOfCompany;
private int licenseNumber;
//customised constructor
public Company(string name; int Num)
{
//below statement will produce error as 'Name' is a Read-Only property, so we can't //assign value through property
//Name = name;
// we can still assign nameOfCompany the value by avoiding the use of property here //and directly assigning value to private field-data
nameOfCompany = name;
LicenseNum = Num;
}
//Notice here 'Name' property is Read-Only property
public string Name
{
get
{ return nameOfCompany; }
}
//Notice here the property LicenseNum is a Write-Only property as there's no get{} scope
public int LicenseNum
{
set
{ // check condition before assigning value to private field-data
if(value != 0 && value < 0)
licenseNumber = value;
else
Console.WriteLine("Invalid License Number");
}
}
}
Therefore if we try to invoke the get { } scope of the property 'LicenseNum' this time, we'll get error.
Example
class Program
{
static void Main(string[] args)
{
Company objRef = new Company("XYZ InfoTech Pvt. Ltd', 12343);
//here we're trying to invoke the get { } scope of the property 'LicenseNum' which will //produce error as it is a 'Write-Only' property
Console.WriteLine("Company license number is : {0}" , objRef.LicenseNum);
Console.ReadLine();
}
}
Note:
We can still get the value of private field-data 'licenseNumber', but for that we've to make a new method in class 'Company' which directly accesses the private field-data 'licenseNumber' and thus calling that method from Main(string[] args) can provide us the value of private field-data licenseNumber.
Example
//add the following method in class Company
public void displayLicenseNum()
{
Console.WriteLine("License Number is : {0}, licenseNumber);
}
Thus call to above written displayLicenseNum() method from Main(string[] args) will provide us the value of private field-data 'licenseNumber'.
No comments:
Post a Comment