Behind the Scenes Look at C#: Properties continued - Read or Write Properties
(Page 4 of 4 )
All of the above examples of properties define read/write properties through the declaration of the get and the set accessors in the property declaration. You can declare a read-only property and a write-only property. Each of them is perfect in a specific scenario.
For example, you can use a read-only property when you need to define a property such that the client code can't change its value; the client code can only read (or get) the value. You declare a read-only property by omitting the set accessor part of the property -- thus it contains the logic that can gets the value only.
As I said before, it's very uncommon to declare a property as write-only but it does happen, and you may need it. For example, you have to store some very sensitive information (maybe a password) which can't be read by client code (but of course it will be read by the class members through the private field). To declare such a property you need to omit the get accessor part of the property.
The following version of the Worker example defines read-only and write-only properties. Note that you need to initialize the private field that the read-only property reads through the use of a construction called class constructor. The class constructor is simply a method that has the same name as the class, and is called when you create an instance of the class. It's a perfect place to put object initialization code. We will discuss the class constructor in detail in this series.
The Worker class has a read-only property that has only the get accessor defined, which is the FullName property. This property returns a string representation of the full name of the worker, and you can't change or assign a value to this property because it's read-only. Note that the FullName property returns the value of the private field firstName concatenated with the value of the private field lastName. In other words, the property doesn't to have its own private field that the property uses to store the data.
The Worker class contains a write-only property. It's the Password property. Although I don't like to define any write-only properties in my classes, I thought that you should know about it. Maybe one day you may find a scenario where you need to use this kind of property.
Copy the following code and compile it:
using System;
namespace property
{
class Class1
{
static void Main(string[] args)
{
try
{
Worker.Department = "IT";
Worker w1 = new Worker();
w1.FirstName = "Michael";
w1.LastName = "Youssef";
Console.WriteLine(w1.FullName);
Console.WriteLine("Please Enter the Password");
w1.Password = Console.ReadLine();
Console.WriteLine("Your Password has been stored");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
public class Worker
{
private string firstName;
private string lastName;
private static string department;
private string password;
public string FirstName
{
get
{
return this.firstName;
}
set
{
if(value == String.Empty)
{
throw new ArgumentNullException();
}
else
{
this.firstName = value;
}
}
}
public string LastName
{
get
{
return this.lastName;
}
set
{
if(value == String.Empty)
{
throw new ArgumentNullException();
}
else
{
this.lastName = value;
}
}
}
public static string Department
{
get
{
return department;
}
set
{
if(value != "IT")
{
throw new ArgumentException("All Workers must be in the IT Department");
}
else
{
Worker.department = "IT";
}
}
}
public string FullName
{
get
{
return this.firstName + " " + this.LastName;
}
}
public string Password
{
set
{
this.password = value;
}
}
}
}
Run the code and you will be prompted to enter your password; then it will be stored in the property.

Let's load the MSIL code to differentiate between read-only, write-only and read/write properties. Load the application with the Ildasm.exe tool and navigate to the Worker class, then expand it.

As you can see, because the FullName property is a read-only property, it has only the get_FullName method. The Password property has only the method set_Password because we have defined it as a write-only property.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |