clr

Is casting the same thing as converting?

天涯浪子 提交于 2019-11-26 12:26:04
问题 In Jesse Liberty\'s Learning C# book, he says \"Objects of one type can be converted into objects of another type. This is called casting.\" If you investigate the IL generated from the code below, you can clearly see that the casted assignment isn\'t doing the same thing as the converted assignment. In the former, you can see the boxing/unboxing occurring; in the latter you can see a call to a convert method. I know in the end it may be just a silly semantic difference--but is casting just

sizeof(int) on x64?

做~自己de王妃 提交于 2019-11-26 12:19:33
问题 When I do sizeof(int) in my C#.NET project I get a return value of 4. I set the project type to x64, so why does it say 4 instead of 8? Is this because I\'m running managed code? 回答1: There are various 64-bit data models; Microsoft uses LP64 for .NET: both long s and pointers are 64-bits (although C-style pointers can only be used in C# in unsafe contexts or as a IntPtr value which cannot be used for pointer-arithmetic). Contrast this with ILP64 where int s are also 64-bits. Thus, on all

explicitly cast generic type parameters to any interface

做~自己de王妃 提交于 2019-11-26 12:18:59
问题 In Generics FAQ: Best Practices says : The compiler will let you explicitly cast generic type parameters to any interface, but not to a class: interface ISomeInterface {...} class SomeClass {...} class MyClass<T> { void SomeMethod(T t) { ISomeInterface obj1 = (ISomeInterface)t;//Compiles SomeClass obj2 = (SomeClass)t; //Does not compile } } I see limitation reasonable for both, classes and interfaces, unless the class/interface is not specified as constraint type. So why such behavior, why it

In a switch vs dictionary for a value of Func, which is faster and why?

ⅰ亾dé卋堺 提交于 2019-11-26 12:13:11
问题 Suppose there is the following code: private static int DoSwitch(string arg) { switch (arg) { case \"a\": return 0; case \"b\": return 1; case \"c\": return 2; case \"d\": return 3; } return -1; } private static Dictionary<string, Func<int>> dict = new Dictionary<string, Func<int>> { {\"a\", () => 0 }, {\"b\", () => 1 }, {\"c\", () => 2 }, {\"d\", () => 3 }, }; private static int DoDictionary(string arg) { return dict[arg](); } By iterating over both methods and comparing, I am getting that

How many String objects will be created when using a plus sign?

流过昼夜 提交于 2019-11-26 11:49:57
问题 How many String objects will be created when using a plus sign in the below code? String result = \"1\" + \"2\" + \"3\" + \"4\"; If it was as below, I would have said three String objects: \"1\", \"2\", \"12\". String result = \"1\" + \"2\"; I also know that String objects are cached in the String Intern Pool/Table for performance improvement, but that\'s not the question. 回答1: Surprisingly, it depends. If you do this in a method: void Foo() { String one = "1"; String two = "2"; String result

Equivalent of Class Loaders in .NET

点点圈 提交于 2019-11-26 11:49:47
问题 Does anyone know if it possible to define the equivalent of a \"java custom class loader\" in .NET? To give a little background: I am in the process of developing a new programming language that targets the CLR, called \"Liberty\". One of the features of the language is its ability to define \"type constructors\", which are methods that are executed by the compiler at compile time and generate types as output. They are sort of a generalization of generics (the language does have normal

C# &#39;is&#39; operator performance

江枫思渺然 提交于 2019-11-26 11:45:28
I have a program that requires fast performance. Within one of its inner loops, I need to test the type of an object to see whether it inherits from a certain interface. One way to do this would be with the CLR's built-in type-checking functionality. The most elegant method there probably being the 'is' keyword: if (obj is ISpecialType) Another approach would be to give the base class my own virtual GetType() function which returns a pre-defined enum value (in my case, actually, i only need a bool). That method would be fast, but less elegant. I have heard that there is an IL instruction

Are C# uninitialized variables dangerous?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:20:54
I'm familiar with the C# spec, section 5.3 which says that a variable has to be assigned before use. In C and unmanaged C++ this makes sense as the stack isn't cleared and the memory location used for a pointer could be anywhere (leading to hard to track down bug). But I am under the impression that there are not truly "unassigned" values allowed by the runtime. In particular that a reference type that is not initialized will always have a null value, never the value left over from a previous invocation of the method or random value. Is this correct, or have I been mistakenly assuming that a

Asynchronous iterator Task<IEnumerable<T>>

瘦欲@ 提交于 2019-11-26 11:16:17
问题 I’m trying to implement an asynchronous function that returns an iterator. The idea is the following: private async Task<IEnumerable<char>> TestAsync(string testString) { foreach (char c in testString.ToCharArray()) { // do other work yield return c; } } However, there is an error message that the function cannot be an iterator block because Task<IEnumerable<char>> is not an iterator interface type. Is there a solution? 回答1: It sounds like what you may really be looking for is something like

When is a method eligible to be inlined by the CLR?

点点圈 提交于 2019-11-26 10:56:27
问题 I\'ve observed a lot of \"stack-introspective\" code in applications, which often implicitly rely on their containing methods not being inlined for their correctness. Such methods commonly involve calls to: MethodBase.GetCurrentMethod Assembly.GetCallingAssembly Assembly.GetExecutingAssembly Now, I find the information surrounding these methods to be very confusing. I\'ve heard that the run-time will not inline a method that calls GetCurrentMethod, but I can\'t find any documentation to that