如何让方法在同一个类中调用另一个方法?

Min*_*lla 4 c# methods class

我有一个名为methods.cs包含两个方法的类.

例如method1method2

我想打个method2电话method1

代码澄清:

public static void Method1()
{
// Method1
}

public static void Method2()
{
// Method2
}
Run Code Online (Sandbox Code Playgroud)

我想打个Method2电话Method1.我怎样才能做到这一点?

小智 6

很高兴看到你寻求帮助!为了在另一个方法中调用Method,同一个Class中包含的方法非常简单.只需通过它的名字来称呼它!这是一个很好的关于方法的小教程,我的例子如下!

public class ClassName
{
    // Method definition to call in another Method
    public void MethodToCall()
    {
        // Add what you want to be performed here
    }

    // Method definition performing a Call to another Method
    public void MethodCalling()
    {
        // Method being called. Do this by using its Method Name
        // be sure to not forget the semicolon! :) 
        MethodToCall();
    }
}
Run Code Online (Sandbox Code Playgroud)

祝你好运,希望对你有所帮助!


Chr*_*air 5

也许我错过了你的问题,但它应该像这样简单:

public static void Method1()
{

}

public static void Method2()
{
    Method1();
}
Run Code Online (Sandbox Code Playgroud)