C# Return Void
- The void keyword can be used as a return data type in a method.
- void tells users the method won't return a value.
- return can be used in the void method without any data type following the return statement.
- void can be used in unsafe code which will be introduced in our advanced lessons.
- void is an alias of the System.Void type in the .NET Framework.
![]() |
---|
void cannot be used as a data type in the safe mode in C#. |
Example 01-44-01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System; class TestVoid { public int score; static void Main(string[] args) { TestVoid t = new TestVoid(); t.score = 80; t.print(); t.result(); t.score = -10; t.print(); t.result(); Console.Read(); } public void print(){ Console.Write("Print the result: "); } public void result(){ if ( score < 0 ) { return; } else { if ( score < 60 ) Console.WriteLine("Failed."); else Console.WriteLine("Passed."); } } }
Output
Print the result: Passed. Print the result:
Explanation
- Line 2: Define a class TestVoid.
- Line 4: Declare a public int field score.
- Line 6: Create an instance of class Test and assign it to the variable t.
- Line 19-21: Define a void method print(). The method does not need to return a value because of void.
- Line 23-35: Define another void method result().
- Line 24: if statement.
- Line 26: return statement without data. The statement makes the control go to the method calling statement.
- Line 30-33: if score < 60, output "Failed" otherwise output "Passed".
- Line 6-17: Main method of the class.
- Line 8: Instantiate the class TestVoid.
- Line 9: Set the field score value.
- Line 10-11: Invoke the 2 methods of the class.
- Line 13: Assign -10 to the field score.
- Line 14: Run the method print() to output a string. The control returns to this statement after calling.
- Line 15: Run result(). Because -10 < 0 then run return statement in line 26 to terminate the method call. The control goes back to this line.
- The return statement is used to terminate execution of the method and transfer the control to the method calling statement.
- If the return type of the method is void, the return statement can be omitted.
- The return statement is obligatory for the method with non-void return type and the return type must be compatible with the type defined at the beginning of the method.
For example, you will get compile-time error for the method below.
public float test() { return 55.5; }
Because the default type of the above literal 55.5 is double type. To fix the problem, you can change the return statement as follows.
return 55.5F;
55.5F is the float type literal which meets the return type. Another solution is to cast to float explicitly.
return (float)55.5;
Example 01-44-02
In the following example, we will show you how to calculate the area of a circle and how to return an object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System; namespace TestReturn { class Circle { public const double PI = 3.14; public double radius = 1.0; public double getArea() { return radius * radius * PI; } public Circle getCircle() { return new Circle(); } } class TestCircle { static void Main(string[] args) { Circle c = new Circle(); c.radius = 20; Console.WriteLine("The area of the circle is {0}", c.getArea()); Console.WriteLine("The default area is {0}", c.getCircle().getArea()); Console.Read(); } } }
Output
The area of the circle is 1256 The default area is 3.14
Explanation
- Line 5-19: Define a class Circle.
- Line 7: Define a public constant PI with double type.
- Line 8: Declare a double type variable radius.
- Line 10-13: Get the area of the circle method. It defines a returned double type.
- Line 12: Based on the formula to calculate the area of the circle and return the result.
- Line 15-18: Define another method getCircle. The returned type is an object of the class Circle.
- Line 17: Return statement. It returns an object by using new keyword to create the instance.
- Line 21-32: Define a TestCircle class.
- Line 25: Create a Circle instance and assign it to the variable c.
- Line 26: Set the field value of the object.
- Line 28: Output the area of the circle by calling the method getArea() of the class.
- Line 29: Output the default area. c.getCircle() returns a new instance and the default value of the field radius is 1.0 which is defined in line 10. Then run getArea method of the instance and output the result.