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?
so if something has parenthesis, it is always a method?
2009-08-29 18:00:17
No. Parenthesis are used in other places like overriding precedence in expressions too.
Mehrdad Afshari
2009-08-29 18:04:18
Can you provide an example of precedence in expressions ?
2009-08-29 18:05:22
`(10 + 5) * 2`
Mehrdad Afshari
2009-08-29 18:05:48
thanks very much for your help.
2009-08-29 18:06:51
Good answer Mehrdad
Philip Fourie
2009-08-29 18:08:16
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
2009-08-29 18:11:20
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
2009-08-29 18:14:59
C# can be quite confusing to start with. Take a property returning a delegate, this can result in `Foo.Property()`
Dykam
2009-08-29 18:17:01
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
2009-08-29 18:21:57
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
2009-08-29 18:24:12
Dykam: consistency.
Mehrdad Afshari
2009-08-29 18:29:25
@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
2009-08-29 18:31:20
Rune FS: The compiler can do this without `()`. A method and a property can't have identical names.
Mehrdad Afshari
2009-08-29 18:34:32
+1
A:
Method calls have to be called with parenthesis, where not with Properties
Philip Fourie
2009-08-29 17:57:38
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
2009-08-29 18:01:23
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
2009-08-29 20:35:15