具有返回类型的代表 [英] Delegates with return type

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

问题描述

您好我有两个方法,返回类型1是实例,另一个是静态方法。我试图用代理访问这些方法,但我无法访问它我收到错误错误的返回类型



 < span class =code-keyword>使用系统; 
使用 System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delgates_Ex1
{

public delegate void ExampleDelgate( int x, int y);


class 民主党
{
public int 添加( int x, int y)

{

Console.WriteLine( 加法方法);
return (x + y);

}


public static int mul( int x, int y)
{
Console.WriteLine( 乘法方法);

return (x * y);
}

}


class 计划
{

静态 void Main( string [] args)
{
Democlass d = new Democlass();

ExampleDelgate ed = new ExampleDelgate(d.Add);

ed( 30 10 );

Console.WriteLine( \ n);

ed = new ExampleDelgate(Democlass.mul);
ed( 30 * 10 );

}
}
}

解决方案

更改代表的退货类型从 void int

  public   delegate   int  ExampleDelgate( int  x, int  y); 



此外,当你尝试使用委托访问 mul 方法,您应该传递 30 10 作为参数而不是乘法:

 ed =  new  ExampleDelgate(Democlass.mul) ; 
// ed(30 * 10); //删除此行
ed( 30 10 ); // 添加此


ProgramFOX已经解决了您的问题,但是您也可能会发现此CodeProject文章很有用 - C# - Delegates 101 - 一个实际例子 [ ^ ]


除了正确的解决方案1:



您不需要单独的委托类型声明如此简单的签名;它已经为你完成了,并且是 System.Func< int,int,int> 。您根本不需要单独的类型声明:



 System.Func< int, int ,int> ed =  new  System.Func< int, int ,int>(Demo.Mul); 





此外,您甚至不需要单独的(命名)方法实现:

 System.Func< int, int ,int> ed =  new  System.Func< int, int ,int>((x,y)= >  {
return x * y;
});





使用lambda的特殊功能:您可以在某些方法中将上面显示的内容设置为本地,而不是在其他任何地方暴露。你为什么需要这样的东西?仅举一件事,请参阅我的短文:隐藏调用方法主体内部的特殊方法 [ ^ ]。



-SA

Hi I have two methods with return type one being instance and the other one is static method. I am trying to access those methods with delegates but i am unable to access it i get a error " Has wrong return type

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delgates_Ex1
{

    public delegate void ExampleDelgate (int x, int y);


    class Democlass
    {
        public int Add(int x, int y)

        {

            Console.WriteLine("Addition Method");
            return (x + y);
              
        }


        public static int mul(int x, int y)
        {
            Console.WriteLine("Multiplication Method");

            return (x * y);
        }

    }


    class Program
    {

        static void Main(string[] args)
        {
            Democlass d = new Democlass();

            ExampleDelgate ed = new ExampleDelgate(d.Add);

            ed(30, 10);

            Console.WriteLine("\n");

            ed = new ExampleDelgate(Democlass.mul);
            ed(30 * 10);

        }
    }
}

解决方案

Change the return type of your delegate from void to int:

public delegate int ExampleDelgate (int x, int y);


Also, when you try to access the mul method with a delegate, you should pass 30 and 10 as parameters and not as a multiplication:

ed = new ExampleDelgate(Democlass.mul);
//ed(30 * 10); // remove this line
ed(30, 10); // add this


ProgramFOX has solved your problem, however you may also find this CodeProject article useful - C# - Delegates 101 - A Practical Example[^]


In addition to the correct Solution 1:

You don't need a separate delegate type declaration for such a simple signature; it is already done for you and is System.Func<int, int, int>. You don't need a separate type declaration at all:

System.Func<int, int, int> ed = new System.Func<int, int, int>(Demo.Mul);



Moreover, you don't even need a separate (named) method implementation:

System.Func<int, int, int> ed = new System.Func<int, int, int>( (x, y) => {
    return x * y;
});



The special feature of using lambda: you can make the like shown above local in some method, not exposed anywhere else. Why would you need such a thing? For just one thing, please see my short article: Hide Ad-hoc Methods Inside the Calling Method’s Body[^].

—SA


这篇关于具有返回类型的代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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