Just as C# 2 introduced T?
as a short-hand for Nullable<T>
, shouldn't C# consider introducing a short-hand for even the more popular IEnumerable<T>
? Like T*
?
Wouldn't this help make something that should be simple to read, like
Func<IEnumerable<string>, IEnumerable<string>> f;
in fact simple to read?
Func<string*, string*> f;
What would be the downside, if any? Naturally, T*
introduces some syntactic overlap with pointers in C# but then what would be an alternative? T+
?
Bear in mind:
- Pointers are only allowed in C# when code is compiled with the
/unsafe
switch and even then in methods explicitly marked withunsafe
. - Pointer syntax is not allowed in C# for a generic type argument. You cannot, for example, say
IEnumerable<int*>
today to mean a sequence of integer pointers. The compilation will fail with CS0306.
Rationale:
With the introduction of LINQ, the IEnumerable<T>
has taken a center stage as the most generic way to represent a collection or sequence. The generic syntax, however, when used within delegates or method signatures can make code long and unreadable. For example, here is a simple use of Func<T, R>
to declare a function variable that accepts and returns a sequence of strings:
Func<IEnumerable<string>, IEnumerable<string>> f;
This is a simple case and already makes the code unnecessarily long to write and read back. So, just as C# 2 introduced T?
as a shorthand for Nullable<T>
, should C# consider introducing a shorthand for IEnumerable<T>
, like T*
?