Skip to content
Related Articles

Related Articles

void Keyword in C#
  • Last Updated : 22 Jun, 2020

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: Geeks
       multi = 50*20

Output: Geeks
        multi = 1000

Output: GeeksforGeeks
        sum = 80+20
        sub = 40-10

Output: GeeksforGeeks
        sum = 100
        sub = 30

Example 1:






// C# program for void keyword
using System;
class GFG {
  
    public void SimpleText()
    {
        Console.WriteLine("Geeks");
    }
  
    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:

Geeks
multi = 1000

Example 2:




// C# program for void keyword
using System;
using System.Text;
  
namespace Test {
  
class Sample {
  
    public void SimpleText()
    {
        Console.WriteLine("GeeksforGeeks");
    }
  
    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:

GeeksforGeeks
sum = 100
sub = 30
My Personal Notes arrow_drop_up
Recommended Articles
Page :