C# void Keyword with Example

In this tutorial, we will learn about the void keyword, its usage, syntax, and example in C#. By IncludeHelp Last updated : April 04, 2023

C# void Keyword

In C#, void keyword is a reference type of data type, it is used to specify the return type of a method in C#. The void is an alias of System.Void.

Note: If there is no parameter in a C# method, void cannot be used as a parameter.

Syntax of void

public void function_name([parameters])
{
 //body of the function
}

Example of void keyword in C#

using System;
using System.Text;

namespace Test {
  class Example {
    public void printText() {
      Console.WriteLine("Hello world!");
    }
    
    public void sum(int a, int b) {
      Console.WriteLine("sum = " + (a + b));
    }
  };

  class Program {
    static void Main(string[] args) {
      //calling functions
      Example ex = new Example();
      ex.printText();
      ex.sum(10, 20);

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

Hello world!
sum = 30



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.