调用方法 [英] Call a Method

查看:54
本文介绍了调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为C#中的变量分配一个方法,并使用该变量来调用它,而不是直接调用该方法.这可能吗?如果可以,我将如何解决呢?

谢谢

I''d like to assign a method to a variable in C#, and use the variable to call it instead of calling the method directly. Is this possible, and if so, how would I go about it?

Thanks

推荐答案

使用委托.它们是为此类事物而创建的.

单击此处
Use delegates. They are created for such things.

Click here


//D is the name of the delegate
public delegate void D();
class Student
{
    public void Total()
    {
        Console.Write("Enter the first number :");
        int FirstNumber = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the second number :");
        int SecondNumber = Convert.ToInt32(Console.ReadLine());
        int Result = FirstNumber+SecondNumber;
        Console.Write("Total :"+Result);
    }
}

class Program
{
    static void Main(string[]args)
    {
        Student stu = new Student();
        D d = new D(stu.Total);
        d.Invoke();
        Console.ReadKey();
    }
}


这篇关于调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆