C#
  Home arrow C# arrow Page 4 - Behind the Scenes Look at C#: Properties c...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Behind the Scenes Look at C#: Properties continued
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-07-20

    Table of Contents:
  • Behind the Scenes Look at C#: Properties continued
  • The Name Property
  • Static Properties
  • Read or Write Properties

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT