C#
  Home arrow C# arrow Page 4 - Behind the Scenes Look at C#: Properties
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
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2005-07-13

    Table of Contents:
  • Behind the Scenes Look at C#: Properties
  • The Private Fields Example
  • C# Properties
  • Modifying the Worker Class

  • 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 - Modifying the Worker Class


    (Page 4 of 4 )

    Let's modify our Worker class to utilize properties instead of the Getxxx/Setxxx methods.

    using System;
    namespace Properties
    {
    class Class1
    {
    static void Main(string[] args)
    {
    try
    {
    Worker worker1 = new Worker();
    Console.WriteLine("Enter Your First Name");
    worker1.FirstName = Console.ReadLine();
    Console.WriteLine("Enter Your Last Name");
    worker1.LastName = Console.ReadLine();
    Console.WriteLine("Enter Your Department");
    worker1.Department = Console.ReadLine();
    Console.WriteLine("Enter Your Age");
    worker1.Age = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine(worker1);
    Console.ReadLine();
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex.Message);
    Console.ReadLine();
    }
    }
    }

    public class Worker
    {
    private string firstName;
    private string lastName;
    private string department;
    private int age;


    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 string Department
    {
    get
    {
    return this.department;
    }

    set
    {
    switch(value)
    {
    case "IT":
    this.department = "IT";
    break;
    case "HR":
    this.department = "HR";
    break;
    case "Sales":
    this.department = "Sales";
    break;
    default:
    throw new ArgumentException("invalid department");
    }
    }
    }

    public int Age
    {
    get
    {
    return this.age;
    }

    set
    {
    if(value > 18 && value < 40)
    {
    this.age = value;
    }
    else
    {
    throw new ArgumentException("The worker can't be over 40");
    }
    }
    }

    public override string ToString()
    {
    return firstName +" "+lastName+" is working in "+department+
    " and he's "+age+" years old";
    }

    }
    }

    The result of running this code will be the same:

    As you can see, we still do our data validation and throw exceptions from inside the set part of the properties. Let's take a look at the MSIL of this application. Load the file with the Ildasm.exe and navigate to the class Worker.

    get_Age, get_Department and set_Age and set_Department? Yes, the C# compiler generates those methods as the Getxxx and Setxxx methods. In the next part of this article, we will discuss the MSIL Generated code of C# Properties, static properties, read-only properties and more. 


    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