加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在c#中使用两个空白括号后跟箭头的目的是什么

发布时间:2020-12-15 17:50:32 所属栏目:百科 来源:网络整理
导读:两个括号和箭头的目的是什么 service.GetDeviceLOV = () = 我一直试图弄清楚它的功能.下面是用于单元测试的整个方法.希望这有助于提供它的背景. //A test for GetDeviceTypes [TestMethod()] [HostType("Moles")] public void GetDeviceTypesTest() { SetUpM
两个括号和箭头的目的是什么

service.GetDeviceLOV = () =>

我一直试图弄清楚它的功能.下面是用于单元测试的整个方法.希望这有助于提供它的背景.

//A test for GetDeviceTypes
    [TestMethod()]
    [HostType("Moles")]
    public void GetDeviceTypesTest()
    {
        SetUpMoles();
        Login ();


        service.GetDeviceLOV = () =>
        {
            return new List<DeviceLOV>() { 

            new DeviceLOV{DeviceType = "Type 1"},new DeviceLOV{DeviceType = "Type 2"},new DeviceLOV {DeviceType = "Type 1"}

        };
        };



        List<string> actual;
        actual = presenter.GetDeviceTypes();

        Assert.AreEqual(2,actual.Count,"actual.Count Should = 2");

    }

解决方法

这是一个 lambda expression,用于创建匿名函数的委托.

在您的情况下,GetDeviceLOV属性是一个委托,它返回List< DeviceLOV> (或者它实现的一些接口,例如IEnumerable< DeviceLOV>).

lambda表达式允许您内联编写“方法”并从中创建委托,并将其分配给该属性.如果没有这种语法,您需要创建一个单独的方法,并直接分配一个委托,即:

private List<DeviceLOV> MakeDeviceList()
{
   return new List<DeviceLOV>
   { 
        new DeviceLOV{DeviceType = "Type 1"},new DeviceLOV {DeviceType = "Type 1"}
    };
};

那你就写下这样的东西:

service.GetDeviceLOV = new Func<List<DeviceLOV>>(this.MakeDeviceList);

lambda表达式允许您编写方法“inline”,并直接指定它.它还提供了附加功能(在这种情况下不使用),因为它允许您引用局部变量(编译器将变成闭包)等.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读