How to define and invoke methods in C#?

In C#, a method can be defined using the keyword “void” with the following syntax:

访问修饰符 返回值类型 方法名(参数列表)
{
    // 方法体
}

For example, defining a straightforward method to print a message.

public void PrintMessage(string message)
{
    Console.WriteLine(message);
}

Simply call the method by using the method name and appropriate parameter list in the code.

PrintMessage("Hello, World!");

Of course, if the method has a return value, you can use a different return type instead of “void” and use the “return” keyword in the method body to return the value.

广告
Closing in 10 seconds
bannerAds