void Keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. void is a keyword, it is a reference type of data type and used to specify the return type of a method in C#. It is an alias of System.Void.
Syntax:
public void function_name([parameters]) { //body of the function }
Note: void cannot be used as a parameter if there is no parameter in a C# method.
Example:
Input: Beginner multi = 50*20 Output: Beginner multi = 1000 Output: w3wiki sum = 80+20 sub = 40-10 Output: w3wiki sum = 100 sub = 30
Example 1:
// C# program for void keyword using System; class GFG { public void SimpleText() { Console.WriteLine( "Beginner" ); } public void sum( int a, int b) { Console.WriteLine( "multi = " + (a * b)); } }; class Prog { static void Main( string [] args) { // calling functions GFG ex = new GFG(); ex.SimpleText(); ex.sum(50, 20); } } |
Output:
Beginner multi = 1000
Example 2:
// C# program for void keyword using System; using System.Text; namespace Test { class Sample { public void SimpleText() { Console.WriteLine( "w3wiki" ); } public void sum( int a, int b) { Console.WriteLine( "sum = " + (a + b)); } public void sub( int c, int d) { Console.WriteLine( "sub = " + (c - d)); } }; class Prog { static void Main( string [] args) { // calling functions Sample ex = new Sample(); ex.SimpleText(); ex.sum(80, 20); ex.sub(40, 10); // hit ENTER to exit Console.ReadLine(); } } } |
Output:
w3wiki sum = 100 sub = 30