public class GenericList<T> { private class Node { //... public Node Next; public T Data; } private Node head; //... public T GetNext() { T temp = default(T); Node current = head; if (current != null) { temp = current.Data; current = current.Next; } return temp; } }
这是msdn中的一个介绍default的例子。在函数GetNext()中,使用default获得T的默认值,假如T是引用类型的话,
其默认值为null;如果T是值类型的话,其默认值是0。值类型不可能=null,采用这样的方法就统一了值类型和引用类型的差别
1375

被折叠的 条评论
为什么被折叠?



