I am curious if there is a legitimate reason as to why C# does not support calling a void method as part of the return statement when the calling method's return type is also void.
public void MethodA() { return; } public void MethodB() { return MethodA(); }
So we would normally see this:
public void MethodMeh() { if (expression) { MethodA(); return; } // Do more stuff }
... when we could be using this instead:
public void MethodAwesome() { if (expression) return MethodA(); // Do more stuff }
Is this a language limitation due to how C# handles void?
Like the letter G, C emerged from the Phoenician letter gimel (centuries later, gimel became the third letter of the Hebrew alphabet). In ancient Rome, as the Latin alphabet was being adapted from the Greek and Etruscan alphabets, G and C became disambiguated by adding a bar to the bottom end of the C.
So the C is indeed a very important letter and has no reason to feel ashamed because it makes no sound on it own. Like a man and a women come together to make a unique "sound" in their marriage, so "C" marries "H" to produce a special combined sound. CH itself has three different sounds.
Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".
In the Latin-based orthographies of many European languages, including English, a distinction between hard and soft ⟨c⟩ occurs in which ⟨c⟩ represents two distinct phonemes. The sound of a hard ⟨c⟩ often precedes the non-front vowels ⟨a⟩, ⟨o⟩ and ⟨u⟩, and is that of the voiceless velar stop, /k/ (as in car).
Because it's simply the way the language is defined.
A method can use
return
statements to return control to its caller. In a method returningvoid
,return
statements cannot specify an expression. In a method returning non-void
,return
statements must include an expression that computes the return value.
It's an arbitrary decision (presumably made for compatibility with ANSI C and its other descendants), and other languages do things differently.
For example, in Python, all functions return a value. If you execute a return
statement without a value, or let control reach the end of the function, then it's just like you had written return None
.
In contrast, Pascal limits the terminology of function
to subprograms that have a return value; if you don't want to return anything, you use a procedure
instead.
void
is the absence of information; it doesn’t make any sense to return it. It’s not a type*, and it’s not a value, unlike in some other languages. No, that’s not really a limitation.
* Well, sort of, as @Lucas points out. It’s an informational type; it doesn’t actually represent a type in the usual sense. You can’t have one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With