C#
  Home arrow C# arrow 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


    (Page 1 of 4 )

    We will continue our discussion covering C# properties. We will cover the MSIL code that the compiler generates when you define a C# property, read-only, write-only properties, and static properties. This article is the fourth in a series covering C#.

    MSIL Code and Properties

    To discuss the generated MSIL code I will modify the worker example to a simpler version, so we can concentrate on one property and see what's going on with the MSIL code. Copy the following code and compile it, then load the application with the Ildasm.exe tool.

    using System;
    namespace property
    {
    class Class1
    {
    static void Main(string[] args)
    {
    Worker person = new Worker();
    person.Name = "Michael";
    Console.WriteLine(person.Name);
    }
    }

    public class Worker
    {
    private string name;

    public string Name
    {
    get
    {
    return name;
    }

    set
    {
    name = value;
    }
    }
    }
    }

    Navigate to the worker class in the generated MSIL code.

    It's clear that we have a property called Name, but why do we have two more methods (get_Name and set_Name)? Actually, the C# compiler generates a property along with its associated get_propertyName and set_propertyName methods, which read and write the value from/to the private field. You may ask yourself, why does the compiler generate a property while it has already been generated -- the get and set methods? Why do we need the property then?

    Actually, the runtime doesn't know about the C# construction property. The runtime has a complete knowledge of methods, and as you will see shortly, the compiler determines when to call the method get_Name and the method set_Name each time you read and write with the property. We need the property construction for reflection; for now think of reflection as a way to get information about a loaded object in run time. Now, let's look at the Name property.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - 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#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





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