Click here to Skip to main content
12,403,535 members (71,636 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.2K views
Posted

Proving that Properties are methods with out looking into IL

, 30 Sep 2011 LGPL3
Rate this:
Please Sign up or sign in to vote.
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.

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 :)

License

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

Share

About the Author

zenwalker1985
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/

You may also be interested in...

Comments and Discussions

 
QuestionProperties include methods, but that doesn't mean they 'are' methods Pin
supercat930-Sep-11 11:47
membersupercat930-Sep-11 11:47 
AnswerRe: Properties include methods, but that doesn't mean they 'are' methods Pin
zenwalker198530-Sep-11 22:40
memberzenwalker198530-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 30 Sep 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid