开发者

What is TSource in C# .NET?

Question

What is TSource?

Here is an example from MSDN:

public static IEnumerable<TSource> Union<TSource>(
    this IEnumerable<TSource> first,
    IEnumerable<TSource> second,
    IEqualityComparer<TSource> comparer
)

Is it a type? Couldn't find any MSDN documentation on it. I assume it can't be a type since I couldn't click on it in .NET Reflector.

Is it a .NET 开发者_开发问答keyword? Didn't find it in the C# keywords list.

Is it something the .NET compiler interprets in a special way?

What I already know

I know that T is a generic type parameter which is used sort of as a placeholder in a generic method. Example from What Are Generics from MSDN:

public class Stack<T>
{
   T[] m_Items; 
   public void Push(T item)
   {...}
   public T Pop()
   {...}
}
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int number = stack.Pop();


TSource is just a generic type parameter. You can tell that because it comes in angle brackets after the method name in the declaration:

public static IEnumerable<TSource> Union<TSource>

You can use any identifier for a type parameter name, so this would be equally valid:

public static IEnumerable<Foo> Union<Foo>

Conventionally, however, either T or a name beginning with T is used. In this cases, it's indicating the type of the "source" element of the union. LINQ methods typically use the following type parameter names:

  • TSource: element type of the input (source)
  • TResult: element type of the output (result)
  • TKey: element type of a key used for things like grouping
  • TElement: element type of an intermediate sequence - this is more rarely used, but it appears in some overloads of GroupBy and similar methods


TSource is the same as T just named differently to be more descriptive

You can see more examples of this in Func<T, TResult> and numerous other generic classes. Here is a quick naming convention for generics from msdn.

Name generic type parameters with descriptive names, unless a single letter name is completely self explanatory and a descriptive name would not add value.

public interface ISessionChannel<TSession> 
{...}
public delegate TOutput Converter<TInput,TOutput>(TInput from);


It is a generics parameter for a type. TSource is just the name to "be replaced with a real type". All TSource must be replaced with the same type.

If you have

public static bool Foo<T1, T2>(
    T1 item1,
    T2 item2,
    T1 item3,
    T2 item4,
)

then item1 and item3 must of of the same type, item2 and item4 must be of the same type. But item1 and item2 does not have to be of the same type.


TSource is equivalent to T. When having more than one generic type it's better to name them in a "readable" manner rather than using single letters.

For example, for the .NET type of Func, the MSDN docs have the example of "Func<T, TResult>". Naming the generic parameter in this way makes it easier for you to know what you should be expecting from the object.


TSource is like T, only called TSource. They could have called U, V, Z. They called it TSource.


TSource is just a more meaningful name instead of T. This is needed, because classes and methods can have more than one type parameter (and a method can have a different one than the class, ofc.), ie. IDictionary<TKey, TValue>.


<TSource>

T means Type. The type you want to use here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜