How to create multiple directories from a single full path in C#?

I would call Directory.CreateDirectory(@”C:\dir0\dir1\dir2\dir3\dir4\”). Contrary to popular belief, Directory.CreateDirectory will automatically create whichever parent directories do not exist. In MSDN’s words, Creates all directories and subdirectories as specified by path. If the entire path already exists, it will do nothing. (It won’t throw an exception)

Why does List implement IReadOnlyList in .NET 4.5?

Because List<T> implements all of the necessary methods/properties/etc. (and then some) of IReadOnlyList<T>. An interface is a contract that says “I can do at least these things.” The documentation for IReadOnlyList<T> says it represents a read-only collection of elements. That’s right. There are no mutator methods in that interface. That’s what read-only means, right? IReadOnlyList<T> … Read more

What is C# analog of C++ std::pair?

Tuples are available since .NET4.0 and support generics: Tuple<string, int> t = new Tuple<string, int>(“Hello”, 4); In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following: public class Pair<T, U> { public Pair() { } public Pair(T first, U second) { this.First = first; this.Second = second; } public T First … Read more

How did Microsoft create assemblies that have circular references?

I can only tell how the Mono Project does this. The theorem is quite simple, though it gives a code mess. They first compile System.Configuration.dll, without the part needing the reference to System.Xml.dll. After this, they compile System.Xml.dll the normal way. Now comes the magic. They recompile System.configuration.dll, with the part needing the reference to … Read more

Why is a Dictionary “not ordered”?

Well, for one thing it’s not clear whether you expect this to be insertion-order or key-order. For example, what would you expect the result to be if you wrote: var test = new Dictionary<int, string>(); test.Add(3, “three”); test.Add(2, “two”); test.Add(1, “one”); test.Add(0, “zero”); Console.WriteLine(test.ElementAt(0).Value); Would you expect “three” or “zero”? As it happens, I think … Read more