Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyZhao/9933999 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/9933999 to your computer and use it in GitHub Desktop.
/*
* 引入voidable修饰符,用于修饰泛型参数。
* 被修饰的泛型参数T,可以在代码里具体化为void类型。
*/
class Task<voidable T> { } // 带有voidable修饰
class List<T> { } // 不带voidable修饰
/*
* 于是乎
*/
Task<int> intTask; // 正常
Task<void> voidTask; // 正常
typeof(Task<>).MakeGenericType(typeof(int)); // 正常
typeof(Task<>).MakeGenericType(typeof(void)); // 正常
List<int> intList; // 正常
List<void> voidList; // 编译失败
typeof(List<>).MakeGenericType(typeof(int)); // 正常
typeof(List<>).MakeGenericType(typeof(void)); // 抛出NotSupportedException
@Ivony
Copy link

Ivony commented Apr 2, 2014

还要补充一下voidable T在类型里面使用的编译限制。

@JeffreyZhao
Copy link
Author

@Ivony 比如哪些?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment