调用构造函数在C#中的函数 [英] Call constructor as a function in C#
问题描述
有没有在C#的方式来引用类的构造函数作为标准功能?其原因是,Visual Studio的抱怨有关修改的功能在他们lambda表达式,往往它的一个简单的SELECT语句。
Is there a way in C# to reference a class constructor as a standard function? The reason is that Visual Studio complains about modifying functions with lambdas in them, and often its a simple select statement.
例如 VAR ItemColors = selectedColors.Select(X =>新建的SolidColorBrush(X));
,其中selectedColors只是一个的IEnumerable< System.Windows.Media.Color方式>
For example var ItemColors = selectedColors.Select (x => new SolidColorBrush(x));
, where selectedColors is just an IEnumerable<System.Windows.Media.Color>
.
从技术上讲,应该不是拉姆达是多余的?选择将一个函数接受类型T和返回类型U的纯色画笔取(正确的),在这里输入T和返回U.只有我看到没有办法在C#中做到这一点。在F#它会像让ItemColors =地图selectedColors(新的SolidColorBrush)
。
Technically speaking, shouldn't the lambda be redundant? select takes a function accepting a type T and returning type U. The solid color brush takes (the correct) type T here and returns a U. Only I see no way to do this in C#. In F# it would be something like let ItemColors = map selectedColors (new SolidColorBrush)
.
TL; DR:我想我在寻找 VAR ItemColors = selectedColors.select的有效形式(新的SolidColorBrush)
该好好尝试一下需要兰巴。这是可能在C#中,还是在语言没有办法表达这种结构?
TL;DR: I guess I'm looking for the valid form of var ItemColors = selectedColors.select (new SolidColorBrush)
which doens't require a lamba. Is this possible in C# or does the language have no way to express this construct?
推荐答案
不,你不能引用C#构造函数方法组,并把它作为一个委托参数。这样做的最好办法是通过在你的问题的lambda语法。
No you cannot reference a C# constructor as a method group and pass it as a delegate parameter. The best way to do so is via the lambda syntax in your question.
这篇关于调用构造函数在C#中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!