Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# not allow me to call a void method as part of the return statement?

Tags:

c#

void

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 image 667
Matt Beckman Avatar asked Jul 10 '14 00:07

Matt Beckman


People also ask

Why does letter C exist?

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.

Does the letter C need to exist?

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.

Why is C named so?

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".

Why does C make two sounds?

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


2 Answers

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 returning void, 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.

like image 128
dan04 Avatar answered Oct 15 '22 01:10

dan04


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.

like image 24
Ry- Avatar answered Oct 15 '22 00:10

Ry-