怎么样在C#5.0可选泛型类型参数? [英] What about optional generic type parameters in C# 5.0?
问题描述
只是一个想法。
那岂不是有在C#?
这将使生活更简单。我累了具有相同的名称,但不同类型的参数多类。也VS不支持此非常Vell港口(文件名): - )
This would make life simpler. I'm tired of having multiple classes with the same name, but different type parameters. Also VS doesn't support this very vell (file names) :-)
这会例如消除对一个非通用的IEnumerable需要:
This would for example eliminate the need for a non-generic IEnumerable:
interface IEnumerable<out T=object>{
IEnumerator<T> GetEnumerator()
}
你觉得呢?
What do you think?
推荐答案
我肯定是它。
我目前正在写不同场景的辅助方法我在哪里想要传递给不同的成员和类方法的引用。要做到这一点,我服用,例如,表达式来; Func键<锡,兜售>>
作为参数传递给助手(即让我用一个lambda到达方法。表达,从而藏在心里强类型)
I'm currently writing helper methods for different scenarios where I want to pass references to different members and methods of classes. To accomplish this I'm taking, for example, an Expression<Func<TIn, TOut>>
as argument to the helper (that lets me reach the method with a lambda expression, thus keeping everything strongly typed).
但是 - 我现在需要为每个不同数量的输入参数一个新的辅助方法,因为我需要有不同的通用参数传递给它量。而不是
BUT - I currently need to define a new helper method for each different number of input arguments, since I need to have a different amount of generic arguments to it. Instead of
HelperMethod<TIn>(Expression<Action<TIn>> arg) // Yes, C# can distinguish
HelperMethod<TOut>(Expression<Func<TOut>> arg) // these two from eachother
HelperMethod<TIn, TOut>(Expression<Func<TIn, TOut>> arg)
HelperMethod<TIn1, TIn2, TOut>(Expression<Func<TIn1, TIn2, TOut>> arg)
// etc
我可以做与做的,顶多两个方法:
I could make do with, at the most, two methods:
HelperMethod<TIn>(Expression<Action<TIn>> arg)
HelperMethod<TOut, TIn1 = DummyType, ...>(Expression<Func<TIn1, ..., TOut> arg)
在我的情况下,这将避免很多重复的代码...
In my case, it would avoid a lot of code duplication...
这篇关于怎么样在C#5.0可选泛型类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!