0
0
C Sharp (C#)programming~5 mins

Return values and void methods in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a return value in a method?
A return value is the result a method sends back to the part of the program that called it. It can be a number, text, or any data type.
Click to reveal answer
beginner
What does a void method mean in C#?
A void method does not return any value. It just runs some code and finishes without sending anything back.
Click to reveal answer
beginner
How do you return a value from a method in C#?
Use the return keyword followed by the value you want to send back. For example: return 5;
Click to reveal answer
intermediate
Can a method have both void and return a value?
No. A method declared as void cannot return a value. If it returns a value, it must have a matching return type.
Click to reveal answer
intermediate
What happens if a method with a return type does not have a return statement?
The program will give a compile-time error because it expects a value to be returned from the method.
Click to reveal answer
What keyword do you use to send a value back from a method?
Areturn
Bsend
Coutput
Dyield
What type of method does NOT return any value?
Abool
Bstring
Cint
Dvoid
Which of these is a valid method header for a method that returns a number?
Aint GetNumber()
Bvoid GetNumber()
Cvoid GetNumber(int number)
Dstring GetNumber()
What will happen if a method declared to return int does not have a return statement?
AIt will return 0 automatically
BIt will cause a compile-time error
CIt will return null
DIt will run but return garbage value
Can a void method use the return keyword?
ANo, never
BYes, with a value
CYes, but only without a value
DOnly in constructors
Explain the difference between a method that returns a value and a void method.
Think about whether the method sends something back or just runs code.
You got /3 concepts.
    Describe what happens if you forget to include a return statement in a method that should return a value.
    What does the compiler expect from methods with return types?
    You got /3 concepts.