C# void. If void returns nothing then why does it continue to execute the method's statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C# void. If void returns nothing then why does it continue to execute the method's statement?

static void Method() { Console.WriteLine("Hello"); } If void means return nothing, then why does it continues to execute the statement? Can someone clarify please.

8th Feb 2022, 5:16 AM
Taukoriri Meita
Taukoriri Meita - avatar
4 Answers
+ 1
If a method's return type is void, it does not return anything. Your method calls console.writeline which gets the console and prints something to it. That does not require the method to return anything. You can think of it like calling a restaurant. The restaurant might have an automated system with options. Say, if you press one, you can order food to be delivered. If you press 2, you can leave a message. Option 1 takes in an order and outputs food but option 2 just calls the message system and doesn't output anything or you the caller don't receive anything when pressing 2.
10th Feb 2022, 7:29 AM
Kail Galestorm
Kail Galestorm - avatar
0
Why wouldn't it?
8th Feb 2022, 5:42 AM
Simon Sauter
Simon Sauter - avatar
0
void it means that the return word is not used.. but can, like in your example, writes in console or modify an object
8th Feb 2022, 5:52 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
0
As mountain climbers who give only one reason why they climb, the compiler includes the code in the executable for the same reason: Because it is there.
8th Feb 2022, 5:57 AM
Brian
Brian - avatar