C# 如何让方法调用同一个类中的另一个方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16227073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:27:34  来源:igfitidea点击:

How to make method call another method in the same class?

c#classmethods

提问by Mina Hafzalla

I have a class called methods.cscontains two methods.

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

For example method1and method2

例如method1method2

I want to make method2call method1

我想method2打电话method1

Code clarification:

代码说明:

public static void Method1()
{
// Method1
}

public static void Method2()
{
// Method2
}

I want to make Method2call Method1. How can I achieve that?

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

回答by Chris Sinclair

Maybe I'm missing something from your question, but it should be as simple as this:

也许我从你的问题中遗漏了一些东西,但它应该像这样简单:

public static void Method1()
{

}

public static void Method2()
{
    Method1();
}

回答by John Saunders

public static void Method2()
{
// Method2
    Method1();
}

回答by Gjeltema

This is almost exactly the same question as you just asked:

这与您刚刚提出的问题几乎完全相同:

How to make method call another one in classes C#?

如何在类 C# 中调用另一个方法?

But...

但...

public class MyClass
{
   public static void Method1()
    {
        // Method1
    }

    public static void Method2()
    {
        Method1();
    }
}   

回答by Gjeltema

Great to see you seeking help! In order to call Method in another Method that is contained in the same Class is pretty simple. Just call it by its name! Here is a nice little tutorial on Methodsand my example is below!

很高兴看到你寻求帮助!为了调用包含在同一个类中的另一个方法中的方法非常简单。就叫它的名字吧!这是一个关于方法的不错的小教程,下面是我的示例!

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();
    }
}

Best of luck to you and hope this helps!

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