#260 – How Properties Look Under-the-Covers

The typical pattern for implementing a property in a C# class is shown below–you define a private backing variable in which to store the property value, as well as get and set accessors that read/write the property value.

    public class Dog
    {
        // An typical instance property
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

We can use the IL DASM tool to take a look at how this property is actually implemented.  Start up the IL Disassembler tool and then do a File | Open and load the .exe containing the property shown above.  You’ll see the following:

We see our private backing variable–name–as well as two methods that represent the get/set accessors–get_Name and set_Name. These are the methods that the compiler generates, which implement the accessors.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: