Archives
Archives /
2011
-
Recursively Traversing IEnumerable<T>
Most of the samples I saw about traversing an IEnumerable<T> use recursion, this can cause some overload in the stack depending on the hierarchy depth. The version I propose use a Queue<T> to achieve the same; note that this is I a common algorithm named Breadth-first search, and note that you can use a Stack<T> instead to turn to Depth-first search algorithm. Here you have the extension method:
-
Getting the default value for a Type
Sometimes we need to get a default value for an specific type but we only know the type in runtime, then we can not use default(T). One way to do this is to use a simple extension method: