views:

110

answers:

3

I am new to programming. Using C# & ASP.Net, how would I differentiate between a property & a method? How can I tell if something is a property or a method?

+3  A: 
Mehrdad Afshari
so if something has parenthesis, it is always a method?
No. Parenthesis are used in other places like overriding precedence in expressions too.
Mehrdad Afshari
Can you provide an example of precedence in expressions ?
`(10 + 5) * 2`
Mehrdad Afshari
thanks very much for your help.
Good answer Mehrdad
Philip Fourie
I'd say that's almost coorect but stating that you always use parenthesis when _declaring_ a method would be even more correct. After allsomeEvent += someName; is valid if someName is a property (with a delegate suitable for the expression) or a method with a signature matching the delegate. (I know it's not a method call, but you can use methods without calling them)
Rune FS
Rune FS: I explicitly mentioned "calling" in my second line. Anyway, that's just a side note. The question was about identifying what's a method and what's a property and looking at the icon is the best way.
Mehrdad Afshari
C# can be quite confusing to start with. Take a property returning a delegate, this can result in `Foo.Property()`
Dykam
Indeed. The `()` is not really a way to distinguish a method from a property. I wrote that to tell the OP how he should use it.
Mehrdad Afshari
I know. More people struggle with (), in any language. Had one asking why a function taking nothing has to be used with (), it seemed useless to him. Didn't have a clear explanation.
Dykam
Dykam: consistency.
Mehrdad Afshari
@Dykam to distinguish it from a property. if the () was omitted the compiler would look for a property (except if the left hand side was expecting a delegate where it would take the method without () as a delegate)
Rune FS
Rune FS: The compiler can do this without `()`. A method and a property can't have identical names.
Mehrdad Afshari
+1  A: 

Method calls have to be called with parenthesis, where not with Properties

Philip Fourie
Depends on the language. Although Visual Studio will automatically insert parentheses into VB.NET code, VB.NET doesn't require parentheses to call methods which take 0 parameters.
Juliet
Thanks for the tip Juliet. Though I am using C#, this is good to know about VB
The question stated it was for C#
Philip Fourie
A: 

Another approach (i.e. not using intellisense) is to call the method/property and compile your code. If you've used the wrong syntax the compiler will let you know.

Jason Williams