Click here to Skip to main content
15,884,176 members
Articles / All Topics

Proving that Properties are Methods Without Looking into IL

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Sep 2011LGPL31 min read 7.1K   2
How to prove that properties are methods without looking into IL

Today, I was just thinking what if I had read/heard a theoretical point from somewhere/someone 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 let's assume we didn’t have any of these tools or knowledge of reading IL code. How else can we suppose to check/validate this theoretical point?

If you are an experienced good C# programmer, I am sure you already know the solution.

Other way to put this point is, let's assume that a C++ guy is writing a C# code and has declared a property called SomeProperty and also wants 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 cases, I would like to just give a small explanation with the code here:

C#
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:

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

This is because the compiler has already generated a method with the same signature for the property which is defined in the code. Since it is a method, we can overload it and compiler doesn’t have any issues with it. You also get the same error if you write 2 methods with the 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 without looking into IL.

Thanks for reading! :)

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
QuestionProperties include methods, but that doesn't mean they 'are' methods Pin
supercat930-Sep-11 11:47
supercat930-Sep-11 11:47 
AnswerRe: Properties include methods, but that doesn't mean they 'are' methods Pin
zenwalker198530-Sep-11 22:40
zenwalker198530-Sep-11 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.