关键字是用于某些内部过程或表示某些预定义动作的语言中的单词。 void是关键字,它是数据类型的引用类型,用于指定C#中方法的返回类型。它是System.Void的别名。
句法:
public void function_name([parameters])
{
//body of the function
}
注意:如果C#方法中没有参数,则void不能用作参数。
例子:
Input: Geeks
multi = 50*20
Output: Geeks
multi = 1000
Output: GeeksforGeeks
sum = 80+20
sub = 40-10
Output: GeeksforGeeks
sum = 100
sub = 30
范例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);
}
}
输出:
Geeks
multi = 1000
范例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();
}
}
}
输出:
GeeksforGeeks
sum = 100
sub = 30