에 대해 무엇을 선택 일반적인 유형 매개변수에 C#5.0?[마감]

StackOverflow https://stackoverflow.com/questions/2541659

  •  23-09-2019
  •  | 
  •  

문제

다만 생각했다.

지 않을 유지하는 데 유용하게 사용될 옵션 입력 매개변수에 C#?

이것은 삶을 간단합니다.내가 피곤의 여러 이름이 같은 클래스,하지만 다른 유형을 매개 변수입니다.또한 대을 지원하지 않는 이 vell(파일명):-)

이것은 예를 들어에 대한 필요성을 제거 아닌 일반페:

interface IEnumerable<out T=object>{
  IEnumerator<T> GetEnumerator()
}

당신은 무엇을 생각하십니까?

도움이 되었습니까?

해결책

I am definitely for it.

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...

다른 팁

What would be the main use for this language feature? I can see that it could help with some administrative tasks like file names and less typing and such but beyond that I don't see how useful this would be.

Also, this feature would significantly complicate any generic constraints that could be placed on the generic type parameter and the default type itself would have to function as a sort of generic constraint itself.

I think this would complicate the language without offering any real benefit to the developer.

그것은 명확하지 않을 나에게 무엇을 정확하게 당신이 제안.현재 수도 있 유형과 동일한 이름이지만 서로 다른 매개변수 유형,그러나지 않는 관련된 어떤 방법에 의해 상속 무엇을 귀하의 제안을까요?

는 경우,또한 기본 클래스 라이브러리 디자인했다 지상에서,거의 확실히 없을 것이 아닌 일반 인터페이스페이-그것만이 존재하기 때문에 CLR 지원하지 않았다 제네릭할 때 인터페이스가 도입되었고,일반 인터페이스를 상속에서만 그것도록 유산 코드는 계속해서 작동합니다.새로 선언한 클래스를 만들고 있는 대한 CLR 지원하는 제도는 문제가 더 이상 관련이 있습니다.

I recently came across a case that could have used something like this, just not quite. I had a method that performs a transformation of sorts, between class A and its associated class AChild and class B and class BChild. Usually, the pair A/AChild was the same as B/BChild, but sometimes A was a base class of B and AChild a base class of BChild.

It would have been nice to be able to say that my type parameter TB defaulted to TA, and that TBChild defaulted to TAChild.

Note that this was a situation in which it was necessary to write out the type parameters, as inference would not work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top