C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Properties: Old wine in new bottle.


Posted Date: 08 Jan 2007    Resource Type: Articles    Category: .NET Framework
Author: Ajit Singh ShekhawatMember Level: Bronze    
Rating: 1 out of 5Points: 10



Properties in C#


Properties are new type of members of classes but in IL there is no properties are exist. All properties are converted into function and then executed. As per microsoft, properties are executed faster than methods. How ?

This is because of only tweeking of compiler. C# compilers (even all .NET Compilers) are tweaked so prperties are executed faster rather than setting a member variable's value. Any way I am giving an examlpe.

Create a class in C# (Windows application)

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Experiments
{
public class Form1 : Form
{
public string Name
{
get { return _name; }
set { _name = value; }
} private string _name = string.Empty;
public Form1()
{
InitializeComponent();
}
}
}


Now open its output (Experiments.exe) into ildasm.exe (A tool from Microsoft for disassembling assemblies). When you look into IL codes you find two methods set_name /get_name for setting and returning values of outr private variable. I am giving part of IL codes below for referance.

.method public hidebysig specialname instance string
get_Name() cil managed
{
// Code size 12 (0xc)
.maxstack 1
.locals init ([0] string CS$1$0000)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldfld string Experiments.Form1::_name
IL_0007: stloc.0
IL_0008: br.s IL_000a
IL_000a: ldloc.0
IL_000b: ret
} // end of method Form1::get_Name

.method public hidebysig specialname instance void
set_Name(string 'value') cil managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: stfld string Experiments.Form1::_name
IL_0008: ret
} // end of method Form1::set_Name


Summary


But please use properties.




Responses

Author: satya priya arya    10 Jan 2007Member Level: Bronze   Points : 0
Let me create a property named "myProp" which created 2 method in IL named "get_myProp()" and "set_myProp()". Its OK.

Now i created 2 method in my code itself named get_myProp(), and set_myProp().
Does compiler allow to create method with different names?

If not, why? bcz as a user point of view, user never defined any method with named get_myProp(), and set_myProp().

Thanks
Satya



Author: Ajit Singh Shekhawat    10 Jan 2007Member Level: Bronze   Points : 0
Thank you very much Satya for your question.

When you create a property (myProp) in a class and try to create functions which named get_myProp(), and set_myProp(), you will get a compilation error because these methods are already created by IL in your class.

Offcourse yes, from user point of view this is a strange behaviour that's why I am giving my article's title "OLD WINE IN NEW BOTTLE"

Thanks
Ajit


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Properties  .  Dot net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C# User Control - an Example
Previous Resource: Easy way for exporting large volume of data ( not formatted data ) to excel using VB .Net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use