What is a 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

Meaning of confusing comment above “string.Empty” in .NET/BCL source?

The important part is not what happens IN this class, but what happens, when another class uses (and links to) it. Let me explain with another example: Assume you have a Assembly1.dll containing a class declaring public static const int SOME_ERROR_CODE=0x10; public static readonly int SOME_OTHER_ERROR_CODE=0x20; and another class consuming this e.g. public int TryFoo() … Read more

Why does TimeSpan.FromSeconds(double) round to milliseconds?

As you’ve found out yourself, it’s a documented feature. It’s described in the documentation of TimeSpan: Parameters value Type: System.Double A number of seconds, accurate to the nearest millisecond. The reason for this is probably because a double is not that accurate at all. It is always a good idea to do some rounding when … Read more

The current status of System.Net.Http vs. Microsoft.Net.Http

This has been for a long time and continues to be confusing. I have seen such messaging myself but as of right now, it appears System.Net.Http is the correct choice, at least for .NET on the Windows platform and has no external dependencies. For .NET Core, I have used Microsoft.Net.Http although it does require Microsoft.BCL. … Read more

Why no AutoResetEventSlim in BCL?

ManualResetEvent and ManualResetEventSlim both are designed so that they remained signaled after calling. This is typically for a very different scenario than AutoResetEvent. AutoResetEvent immediately returns to the unsignaled state after usage, which is typically used for a different set of scenarios. From AutoResetEvents documentation: Typically, you use this class when threads need exclusive access … Read more

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)