C#
  Home arrow C# arrow Page 2 - 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 - The Name Property


    (Page 2 of 4 )

    It's defined using the directive .property and marked as an instance member using the instance keyword, followed by the data type of the property and finally the name of the property. The get method is listed here using the .get directive. Note that the return data type is the same as the data type of the property. The set method is listed in the property using the .set directive. Note that it has only one parameter of type string. Now let's take a look at the methods MSIL code. Double click on the get_Name method.

    Again, spending some time understanding the MSIL generated code crystallizes in your mind how the C# code gets executed. The get_Name method returns the value of the property (through the private field). The ldarg.0 instruction loads the first parameter, and as we said before, the get_Name method is a parameterless method.

    What actually happens is that the MSIL code always refer to the this pointer as the first method's argument. So all the instance methods have the first argument (it's zero-based so it's number 0) refer to the this pointer. The ldfld instruction (stands for Load Field, as we have said before) loads the field of the object in hand. In other words, it loads the field of the object that we have a pointer to (the this pointer from the first step). As you can see it loads the field Worker::name.

    The instruction stloc.0 stores the value of the name field into the first local variable, and the ldloc.0 instruction (stands for Load Location) loads the value back to the stack. The method returns with the value of the field. Let's take a look at the set_Name method.

    The first issue I want to discuss is the Set_Name(string 'value'). As you know, 'value' is a keyword in C# so you can't just use it in your code as an identifier. The C# compiler always generates the set_xxx methods with one parameter named 'value,' and it has the same data type as the property. The get method returns the same data type as defined in the property too, so it's just one data type in both of the methods. In other words, there was no magic when you used an expression like this.name = value to assign the value of the value parameter to the private field name.

    Okay, the ldarg.0 loads the this pointer on the stack, then the instruction ldarg.1 loads the argument number 2 (which is the 'value' parameter) on the stack. After that, the instruction stfld stores the value of the 'value' parameter to the field Worker::name, and then the method returns using the instruction ret. I think that now you have a better understanding of the generated MSIL code. Let's look at the Main method's MSIL code.

    I think that now, understanding the Main method is not a big issue. The newobj instruction instantiates a new object (in our case an object of type worker). The stloc.0 stores the reference of the person object on the stack, then the ldloc.0 loads the reference. The ldstr "Michael" instruction loads the string Michael, then a call to the method set_Name(string) is put in place by using the instruction callvirt (call virtual method). Note that the constant value "Michael" is passed to the method (through the instructions) and that's why the value keyword in C# (which is the name of the paramter in MSIL) refers to the value itself (in our example "Michael").

    Again, we load the location zero using the instruction ldloc.0, which contains the object reference person, and call the method get_Name(). Then we call the Console.WriteLine method and return to the caller using the instruction ret. Some of you may think that using a property to control the access to a field will slow down the application performance, but this is far from the truth. Actually the runtime JIT compiler knows how to optimize the code of the more method calls (the get and set methods which result in more lines of code to access the field), and believe it or not, it generates even faster code than the code that uses public fields.

    I think many of you will want to go to the nearest bookshop and get a book about MSIL and the CLR. I advise you to master C# first, then get a book about MSIL. In fact, it's uncommon to write MSIL using the assembler, but there are some very scientific applications which need just that. Compiler designers who need to develop a compiler for their programming languages that target the .NET runtime will also find that they need to master MSIL code.

    We have been discussing properties on instances, but we haven't discussed static properties, so let's do that.

    More C# Articles
    More By Michael Youssef


     

    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 1 hosted by Hostway
    Stay green...Green IT