base-class-library

Why no AutoResetEventSlim in BCL?

拟墨画扇 提交于 2019-11-27 17:09:20
问题 Why isn't there an AutoResetEventSlim class in BCL? Can it be simulated using ManualResetEventSlim ? 回答1: 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

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

回眸只為那壹抹淺笑 提交于 2019-11-27 14:22:22
问题 TimeSpan.FromSeconds takes a double, and can represent values down to 100 nanoseconds, however this method inexplicably rounds the time to whole milliseconds. Given that I've just spent half an hour to pinpoint this (documented!) behaviour, knowing why this might be the case would make it easier to put up with the wasted time. Can anyone suggest why this seemingly counter-productive behaviour is implemented? TimeSpan.FromSeconds(0.12345678).TotalSeconds // 0.123 TimeSpan.FromTicks((long)

Convert from BitArray to Byte

﹥>﹥吖頭↗ 提交于 2019-11-27 08:34:30
I have a BitArray with the length of 8, and I need a function to convert it to a byte . How to do it? Specifically, I need a correct function of ConvertToByte : BitArray bit = new BitArray(new bool[] { false, false, false, false, false, false, false, true }); //How to write ConvertToByte byte myByte = ConvertToByte(bit); var recoveredBit = new BitArray(new[] { myByte }); Assert.AreEqual(bit, recoveredBit); This should work: byte ConvertToByte(BitArray bits) { if (bits.Count != 8) { throw new ArgumentException("bits"); } byte[] bytes = new byte[1]; bits.CopyTo(bytes, 0); return bytes[0]; } A

Bug in Directory.GetParent?

被刻印的时光 ゝ 提交于 2019-11-27 06:42:45
问题 I was hit in the face by a very weird behavior of the System.IO.Directory.GetParent method: string path1 = @"C:\foo\bar"; DirectoryInfo parent1 = Directory.GetParent(path1); Console.WriteLine (parent1.FullName); // Prints C:\foo, as expected // Notice the extra backslash. It should still refer to the same location, right ? string path2 = @"C:\foo\bar\"; DirectoryInfo parent2 = Directory.GetParent(path2); Console.WriteLine (parent2.FullName); // Prints C:\foo\bar !!! I would consider it a bug,

Why do the overloads of String.Format exist?

拈花ヽ惹草 提交于 2019-11-27 06:37:32
问题 I was using Reflector to look at the implementation of String.Format and had always been under the impression that the overloads of String.Format that took 1, 2 & 3 arguments were optimized versions of the method that takes an object array. However, what I found was that internally they create an object array and then call a method that takes an object array. 1 arg public static string Format(string format, object arg0) { if (format == null) { throw new ArgumentNullException("format"); }

Why does List<T> implement IReadOnlyList<T> in .NET 4.5?

你离开我真会死。 提交于 2019-11-27 05:23:35
Why does List<T> implement IReadOnlyList<T> in .NET 4.5? List<T> isn't read only... 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> is used in the "typical" (contract) way, not as a marker . Interfaces only describes functionality which will be implemented.

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

浪尽此生 提交于 2019-11-27 05:11:21
问题 If you have a full path like: "C:\dir0\dir1\dir2\dir3\dir4\" how would you best implement it so that all directories are present? Is there a method for this in the BCL? If not, what's the most elegant way to do this? 回答1: 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

installing nuget package “same key has already been added.”

社会主义新天地 提交于 2019-11-27 03:04:25
问题 i am trying to install Microsoft.Bcl.Build 1.0.14 nuget returns Installing 'Microsoft.Bcl.Build 1.0.14'. Successfully installed 'Microsoft.Bcl.Build 1.0.14'. Adding 'Microsoft.Bcl.Build 1.0.14' to LeadTracker.Calendar. Uninstalling 'Microsoft.Bcl.Build 1.0.14'. Successfully uninstalled 'Microsoft.Bcl.Build 1.0.14'. Install failed. Rolling back... An item with the same key has already been added. This also happens with json.net and other packages. I can add nancy and topshelf but not json.net

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

↘锁芯ラ 提交于 2019-11-27 02:37:10
I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based. Thank you! Jorge Ferreira 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 { get; set; } public U Second { get; set; } }; And use it like this: Pair

Efficient, Immutable, Extensible Collections for .NET [duplicate]

雨燕双飞 提交于 2019-11-26 21:19:02
问题 This question already has an answer here: Immutable collections? 10 answers It seems to me there is an extreme lack of safe, immutable collection types for .NET, in particular BCL but I've not seen much work done outside either. Do anyone have any pointers to a (preferably) production quality, fast, immutable collections library for .NET. A fast list type is essential. I'm not yet prepared to switch to F#. *Edit: Note to searchers, this is being rolled into the BCL soon: .NET immutable