boxing

Why the compiler emits box instructions to compare instances of a reference type?

柔情痞子 提交于 2019-11-28 17:48:39
Here is a simple generic type with a unique generic parameter constrained to reference types: class A<T> where T : class { public bool F(T r1, T r2) { return r1 == r2; } } The generated IL by csc.exe is : ldarg.1 box !T ldarg.2 box !T ceq So each parameter is boxed before proceeding with the comparison. But if the constraint indicates that "T" should never be a value type, why is the compiler trying to box r1 and r2 ? It's required to satisfy the verifiability constraints for the generated IL. Note that unverifiable doesn't necessarily mean incorrect . It works just fine without the box

How to test whether a value is boxed in C# / .NET?

你。 提交于 2019-11-28 17:04:16
I'm looking for a way to write code that tests whether a value is boxed. My preliminary investigations indicate that .NET goes out of its way to conceal the fact, meaning that GetType() and IsValueType don't reveal the difference between a boxed value and an unboxed value. For example, in the following LinqPad C# expressions, I have faith that o1 is boxed and i1 is not boxed, but I would like a way to test it in code, or, second best, a way to know FOR SURE when looking at any variable or value, even if its type is "dynamic" or "object," whether it's boxed or not boxed. Any advice? // boxed? -

Why are Python's arrays slow?

大憨熊 提交于 2019-11-28 16:26:26
I expected array.array to be faster than lists, as arrays seem to be unboxed. However, I get the following result: In [1]: import array In [2]: L = list(range(100000000)) In [3]: A = array.array('l', range(100000000)) In [4]: %timeit sum(L) 1 loop, best of 3: 667 ms per loop In [5]: %timeit sum(A) 1 loop, best of 3: 1.41 s per loop In [6]: %timeit sum(L) 1 loop, best of 3: 627 ms per loop In [7]: %timeit sum(A) 1 loop, best of 3: 1.39 s per loop What could be the cause of such a difference? Tim Peters The storage is "unboxed", but every time you access an element Python has to "box" it (embed

Does passing a value type in an “out” parameter cause the variable to be boxed?

一曲冷凌霜 提交于 2019-11-28 11:59:18
I'm aware that boxing and unboxing are relatively expensive in terms of performance. What I'm wondering is: Does passing a value type to a method's out parameter cause boxing/unboxing of the variable (and thus a performance hit)? Can the compiler optimize this away? int number; bool result = Int32.TryParse(value, out number); As others have pointed out, there's no boxing here. When you pass a variable as an argument corresponding to an out or ref parameter, what you are doing is making an alias to the variable . You are not doing anything to the value of the variable. You're making two

Why Enum's HasFlag method need boxing?

岁酱吖の 提交于 2019-11-28 10:56:52
I am reading "C# via CLR" and on page 380, there's a note saying the following: Note The Enum class defines a HasFlag method defined as follows public Boolean HasFlag(Enum flag); Using this method, you could rewrite the call to Console.WriteLine like this: Console.WriteLine("Is {0} hidden? {1}", file, attributes.HasFlag(FileAttributes.Hidden)); However, I recommend that you avoid the HasFlag method for this reason: Since it takes a parameter of type Enum, any value you pass to it must be boxed, requiring a memory allocation ." I can not understand this bolded statement -- why " any value you

Does var keyword in C# cause boxing?

我是研究僧i 提交于 2019-11-28 10:39:49
My boss forbids me to use var as it would cause boxing and slowing down the app. Is that true? AakashM An approach that might work is to write these two methods: public static void WithInt() { int x = 5; Console.WriteLine(x); } public static void WithVar() { var x = 5; Console.WriteLine(x); } Compile, and use ildasm to examine the produced CIL. Show your boss. edit @ck has done all but the last step for you :) Following on from Aakash's answer, here is the IL: (thanks LINQPad ) WithInt: IL_0000: ldc.i4.5 IL_0001: stloc.0 IL_0002: ldloc.0 IL_0003: call System.Console.WriteLine IL_0008: ret

How do I get an IntStream from a List<Integer>?

纵饮孤独 提交于 2019-11-28 08:53:42
I can think of two ways: public static IntStream foo(List<Integer> list) { return list.stream().mapToInt(Integer::valueOf); } public static IntStream bar(List<Integer> list) { return list.stream().mapToInt(x -> x); } What is the idiomatic way? Maybe there is already a library function that does exactly what I want? I guess (or at least it is an alternative) this way is more performant: public static IntStream baz(List<Integer> list) { return list.stream().mapToInt(Integer::intValue); } since the function Integer::intValue is fully compatible with ToIntFunction since it takes an Integer and it

Kotlin boxed Int are not the same

元气小坏坏 提交于 2019-11-28 08:37:25
问题 Please help me understand this piece of code in the kotlin docs:- val a: Int = 10000 print(a === a) // Prints 'true' val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA === anotherBoxedA) // !!!Prints 'false'!!! Now, I understand that first int a = 10000 then in the next line it is comparing it using === . Now the question is why when it assigned boxedA=a , it checked if it is null using int? . Can it just be written like this:- val boxedA: Int=a Please if I'm understanding it the

Do generic interfaces in C# prevent boxing? (.NET vs Mono performance)

﹥>﹥吖頭↗ 提交于 2019-11-28 08:26:47
问题 I have a C# interface with certain method parameters declared as object types. However, the actual type passed around can differ depending on the class implementing the interface: public interface IMyInterface { void MyMethod(object arg); } public class MyClass1 : IMyInterface { public void MyMethod(object arg) { MyObject obj = (MyObject) arg; // do something with obj... } } public class MyClass2 : IMyInterface { public void MyMethod(object arg) { byte[] obj = (byte[]) arg; // do something

does valueType.ToString() does a cast on the valueType?

家住魔仙堡 提交于 2019-11-28 08:23:06
问题 lets say, i have the following code in c# int x = 0; x.ToString(); does this internally does a boxing of x? Is there a way to see this happening from visual studio? 回答1: In this specific case, you are using a System.Int32 (an int ). That type redefines ToString , Equals and GetHashCode , so no boxing. If you use a struct that doesn't redefine ToString what you'll have is a constrained callvirt to System.Object.ToString() . The definition of constrained: When a callvirt method instruction has