Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, How come C# requires a "return" statement within a get{} ??
Isn't it obvious that you would want to return something?

thanks
Posted
Comments
Valery Possoz 14-Feb-15 18:50pm    
Sometimes the get is pretty complex and you need to specify what is returned. How would you do it otherwise?
If you want something simpler just use a variable.
PIEBALDconsult 14-Feb-15 19:08pm    
And you may want more than one.
Sergey Alexandrovich Kryukov 14-Feb-15 20:35pm    
How about thinking a bit before asking? All you had to do was to think a bit. And you could explain your idea showing some alternative syntax, if you thought it was possible.
—SA
phil.o 15-Feb-15 2:06am    
Nothing is "obvious" for a computer. If a code must return something, it must do it with the return statement, there is no other way.

1 solution

This question is exactly the same as asking: "how come 'return' statement is required for all non-void functions? Isn't it obvious that you would want to return something?"

In other word, this question lacks elementary logic. Yes, a need for return is obvious from the signature of a function or a property (again, there is no difference in this respect). But this is not enough. What value should be returned and at what point? This is expressed by the return statement.

Also, this question means seeing no difference between declaration and definition, and also between declaration and statement. The language (as well as the languages where C# return was derived from) is not a declarative language. Statements actually code some action, something really happening to the computer registers, memory or ports. If you declare some function with, say, arguments and return type, eventually the definition needs to specify some action using these parameters and assignment return value or object to something.

—SA
 
Share this answer
 
Comments
BillWoodruff 14-Feb-15 21:36pm    
+5 Eloquent !
Sascha Lefèvre 20-Apr-15 20:44pm    
Sergey, I'm not very much educated in anything below compiler level but when learning about Linq.Expressions I observed that the return-value of an Expression.Block is specified by its last expression and that return-statements are implemented as "goto label". Couldn't this theoretically have been implemented just alike in the C# grammar, so that there would be some sense to the question here?
/Sascha
Sergey Alexandrovich Kryukov 20-Apr-15 22:10pm    
I think you are right, theoretically and formally speaking, such model would be possible. But why? It would not add any expressive power to traditional syntax, and, at the same time, would take out part of the developer's freedom. Also note that C# code won't compile if there are paths on instruction flow which would potentially lead to missing return value. Such design seems to be near-optimal to me, and the "last expression" syntax seems to be quite far from optimum.
—SA
Sascha Lefèvre 20-Apr-15 22:15pm    
Of course! This was just a gedankenexperiment ;-)
Sergey Alexandrovich Kryukov 20-Apr-15 22:59pm    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900