base-class-library

How can I convert BitArray to single int?

我怕爱的太早我们不能终老 提交于 2019-11-26 18:53:40
How can I convert BitArray to a single int ? private int getIntFromBitArray(BitArray bitArray) { if (bitArray.Length > 32) throw new ArgumentException("Argument length shall be at most 32 bits."); int[] array = new int[1]; bitArray.CopyTo(array, 0); return array[0]; } private int getIntFromBitArray(BitArray bitArray) { int value = 0; for (int i = 0; i < bitArray.Count; i++) { if (bitArray[i]) value += Convert.ToInt16(Math.Pow(2, i)); } return value; } This version: works for up to 64 bits doesn't rely on knowledge of BitArray implementation details doesn't needlessly allocate memory doesn't

Why do BCL Collections use struct enumerators, not classes?

懵懂的女人 提交于 2019-11-26 18:28:48
We all know mutable structs are evil in general. I'm also pretty sure that because IEnumerable<T>.GetEnumerator() returns type IEnumerator<T> , the structs are immediately boxed into a reference type, costing more than if they were simply reference types to begin with. So why, in the BCL generic collections, are all the enumerators mutable structs? Surely there had to have been a good reason. The only thing that occurs to me is that structs can be copied easily, thus preserving the enumerator state at an arbitrary point. But adding a Copy() method to the IEnumerator interface would have been

How did Microsoft create assemblies that have circular references?

冷暖自知 提交于 2019-11-26 17:04:10
In the .NET BCL there are circular references between: System.dll and System.Xml.dll System.dll and System.Configuration.dll System.Xml.dll and System.Configuration.dll Here's a screenshot from .NET Reflector that shows what I mean: How Microsoft created these assemblies is a mystery to me. Is a special compilation process required to allow this? I imagine something interesting is going on here. 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

Why is there no Char.Empty like String.Empty?

会有一股神秘感。 提交于 2019-11-26 16:02:08
Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings. Edit: The reason for this usage was this: myString.Replace ('c', '') So remove all instances of 'c's from myString. Jon Skeet There's no such thing as an empty char. The closest you can get is '\0' , the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse "" and " " " arguments

Convert from BitArray to Byte

我与影子孤独终老i 提交于 2019-11-26 11:17:07
问题 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); 回答1: This should work: byte ConvertToByte(BitArray bits) { if (bits.Count != 8) { throw

Why is a Dictionary “not ordered”?

大憨熊 提交于 2019-11-26 10:35:54
I have read this in answer to many questions on here. But what exactly does it mean? var test = new Dictionary<int, string>(); test.Add(0, "zero"); test.Add(1, "one"); test.Add(2, "two"); test.Add(3, "three"); Assert(test.ElementAt(2).Value == "two"); The above code seems to work as expected. So in what manner is a dictionary considered unordered? Under what circumstances could the above code fail? Jon Skeet 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

Why do BCL Collections use struct enumerators, not classes?

╄→гoц情女王★ 提交于 2019-11-26 06:20:00
问题 We all know mutable structs are evil in general. I\'m also pretty sure that because IEnumerable<T>.GetEnumerator() returns type IEnumerator<T> , the structs are immediately boxed into a reference type, costing more than if they were simply reference types to begin with. So why, in the BCL generic collections, are all the enumerators mutable structs? Surely there had to have been a good reason. The only thing that occurs to me is that structs can be copied easily, thus preserving the

How did Microsoft create assemblies that have circular references?

混江龙づ霸主 提交于 2019-11-26 05:17:38
问题 In the .NET BCL there are circular references between: System.dll and System.Xml.dll System.dll and System.Configuration.dll System.Xml.dll and System.Configuration.dll Here\'s a screenshot from .NET Reflector that shows what I mean: How Microsoft created these assemblies is a mystery to me. Is a special compilation process required to allow this? I imagine something interesting is going on here. 回答1: I can only tell how the Mono Project does this. The theorem is quite simple, though it gives

Why is there no Char.Empty like String.Empty?

为君一笑 提交于 2019-11-26 04:40:28
问题 Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings. Edit: The reason for this usage was this: myString.Replace (\'c\', \'\') So remove all instances of \'c\'s from myString. 回答1: There's no such thing as an empty char. The closest you can get is '\0' , the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily,

Why is a Dictionary “not ordered”?

孤街醉人 提交于 2019-11-26 03:29:01
问题 I have read this in answer to many questions on here. But what exactly does it mean? var test = new Dictionary<int, string>(); test.Add(0, \"zero\"); test.Add(1, \"one\"); test.Add(2, \"two\"); test.Add(3, \"three\"); Assert(test.ElementAt(2).Value == \"two\"); The above code seems to work as expected. So in what manner is a dictionary considered unordered? Under what circumstances could the above code fail? 回答1: Well, for one thing it's not clear whether you expect this to be insertion-order