base-class-library

Go To Statement Considered Harmful?

只谈情不闲聊 提交于 2019-12-04 23:00:44
If the statement above is correct, then why when I use reflector on .Net BCL I see it is used a lot? EDIT: let me rephrase: are all the GO-TO's I see in reflector written by humans or compilers? The above isn't really correct - it was a polemical device used by Dijkstra at a time when gotos were about the only flow control structure in use. In fact, several people have produced rebuttals, including Knuth's classic "Structured Programming Using Goto" paper (title from memory). And there are some situations (error handling, state machines) where gotos can produce clearer code (IMHO), than the

Why does IPAddress.MapToIPv4() throw ArgumentOutOfRangeException?

坚强是说给别人听的谎言 提交于 2019-12-04 17:02:36
问题 This code throws an ArgumentOutOfRangeException on the last line var initAddress = IPAddress.Parse("1.65.128.190"); var ipv6Address = initAddress.MapToIPv6(); Assert.IsTrue(ipv6Address.IsIPv4MappedToIPv6); var ipv4Address = ipv6Address.MapToIPv4(); Can anyone explain why MapToIPv6() and MapToIPv4() are not round trip compatible? edit: The exception originates from the IPAddress constructor, called by MapToIPv4(). Also, when the first line is var initAddress = IPAddress.Parse("1.65.128.90");

Mathematical explanation why Decimal's conversion to Double is broken and Decimal.GetHashCode separates equal instances

為{幸葍}努か 提交于 2019-12-04 16:33:10
问题 I am not sure if this non-standard way of stating a Stack Overflow question is good or bad, but here goes: What is the best (mathematical or otherwise technical) explanation why the code: static void Main() { decimal[] arr = { 42m, 42.0m, 42.00m, 42.000m, 42.0000m, 42.00000m, 42.000000m, 42.0000000m, 42.00000000m, 42.000000000m, 42.0000000000m, 42.00000000000m, 42.000000000000m, 42.0000000000000m, 42.00000000000000m, 42.000000000000000m, 42.0000000000000000m, 42.00000000000000000m, 42

Adding Assemblies to BuildManager using CodeDOM causing intermittent errors

帅比萌擦擦* 提交于 2019-12-04 13:58:07
I am using CodeDOM to create an in-memory assembly at run time like so: public Assembly Compile(CodeCompileUnit targetUnit) { string path = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); var compilerParameters = new CompilerParameters { GenerateInMemory = true, IncludeDebugInformation = true, TreatWarningsAsErrors = true, WarningLevel = 4, CompilerOptions = "/nostdlib", }; //For system.dll compilerParameters.ReferencedAssemblies.Add(typeof(System.Diagnostics.Debug).Assembly.Location); //For system.core.dll compilerParameters.ReferencedAssemblies.Add

What's the reason of using implicit/explicit convertions instead of constructors?

前提是你 提交于 2019-12-04 04:23:49
An example would be: XNamespace ns = "my namespace" Why not?: XNamespace ns = new XNamespace ( "my namespace" ) What's the idea behind using implicit/explicit convertions instead of constructors? Convenience? Is there a guideline for this? Convenience? More or less, yes. Consider the case for when you’ve got a number-like object (say, a Complex ) on which you do calculations. Clearly, writing code such as: Complex result = c1 * new Complex(2) + new Complex(32); is very annoying and hard to read. Implicit conversions help here (an alternative would be operator overloads in this example, but

Is there any kind of “ReferenceComparer” in .NET?

寵の児 提交于 2019-12-03 22:39:28
There are several places in BCL where one can make use of IEqualityComparer . Like Enumerable.Contains or Dictionary Constructor . I can provide my comparer if I'm not happy with the default one. Sometimes I want to know whether the collection contains that very object that I have reference to. Not the one that is considered "equal" in any other meaning. The question is: whether there exists standard equality comparer in the BCL that relies only on ReferenceEquals method? The one that I wrote myself is this: class ReferenceComparer<T> : IEqualityComparer<T> where T : class { private static

.NET 4.5 CustomReflectionContext: what is it useful for?

亡梦爱人 提交于 2019-12-03 17:12:45
问题 What's New in the .NET Framework 4.5 Developer Preview mentions Ability to customize a reflection context to override default reflection behavior through the CustomReflectionContext class. What is the purpose of the ReflectionContext ? MSDN is not quite clear on the subject. 回答1: In the past with .NET, there has been tension between wanting to be able to automate certain features through reflection, and being able to customize them. For example, take the Properties panel in Visual Studio - in

How Microsoft.Bcl.Async works?

梦想的初衷 提交于 2019-12-03 17:02:38
问题 Microsoft.Bcl.Async enables developers to use async/await keywords without .NET Framework 4.5 that they are supposed to target to use them. That's great, thanks to the incredibly hard work of people in the Microsoft CLR and language teams. Now I am curious how this can work. async/await require the compiler to do some heavy lifting to turn code into something that can do await operations. The compiler originally throws compile errors under .NET Framework 4.0, even if it clearly knows what

Is there a memory leak in the ConcurrentBag<T> implementation? [duplicate]

我与影子孤独终老i 提交于 2019-12-03 16:22:15
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Possible memoryleak in ConcurrentBag? Edit1: The actual question is. Can you confirm this or is my sample wrong and I am missing somthing obvious? I have thought that ConcurrentBag is simpy a replacement for an unorderd list. But I was wrong. ConcurrentBag does add itself to as ThreadLocal to the creating thread which does basically cause a memory leak. class Program { static void Main(string[] args) { var

Why is a “bindingRedirect” added to the app.config file after adding the Microsoft.Bcl.Async package?

只谈情不闲聊 提交于 2019-12-03 14:19:58
问题 I was wondering why nuget added the following code to my applications app.config file, after installing the Microsoft.Bcl.Async : <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Threading.Tasks" publicKeyToken=