C# · CodeProject · Dotnet

Proving that Properties are methods with out looking into IL

Hi,

Today i was just thinking what if i had read/heard a theoretical point from some where/some one that properties are actually methods internally and they are prefixed by get_ and set_ for get and set accessors  respectively but wanted to validate it practically.

I am sure now you’ll think of saying: dude go and see IL for that assembly using ILDASM or similar tool. But lets assume we didn’t had any of these tools or knowledge of reading IL code. How else can we suppose to check/validate this theoretical point?

If your experienced good c# programmer, i am sure you already know the solution.

Other way to put this point is, lets assume that a c++ guy is writing a c# code and has declared a property called SomeProperty and also wanted to write a method get_SomeProperty() due to C++ coding style, but fails to compile his code because compiler issues some weird error which he can’t understand why.

In both the case, i would like to just give a small explanation with the code here:

 1: static string SomeValue
 2: {
 3:     get;
 4:     set;
 5: }
 6:  
 7: public string get_SomeValue() //Compiler Error.
 8: {
 9:     return null;
 10: }
 11:  
 12: public string get_SomeValue(string someValue)
 13: {
 14:     return null;
 15: }

In the above code, compiler issues error for the explicit method specified by developer string get_SomeValue(). The error is as shown:

Error    1    Type ‘ConsoleApplication2.Program’ already reserves a member called ‘get_SomeValue’ with the same parameter types

This is because, compiler has already generated a method with the same signature for the property which is defined in the code. Since its a method, we can overload it and compiler doesn’t has any issues with it. You also get the same error if you write 2 method with same signature in your code as well.

Thus we can prove that the theoretical point on properties are methods having get_ and set_ prefixes are true with out looking into IL.

Thanks 🙂

Advertisements

2 thoughts on “Proving that Properties are methods with out looking into IL

Leave a Reply

Please log in using one of these methods to post your comment:

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