boxing

Details on what happens when a struct implements an interface

天涯浪子 提交于 2019-11-30 22:37:18
问题 I recently came across this Stackoverflow question: When to use struct? In it, it had an answer that said something a bit profound: In addition, realize that when a struct implements an interface - as Enumerator does - and is cast to that implemented type, the struct becomes a reference type and is moved to the heap. Internal to the Dictionary class, Enumerator is still a value type. However, as soon as a method calls GetEnumerator(), a reference-type IEnumerator is returned. Exactly what

Is there Boxing/Unboxing when casting a struct into a generic interface? [duplicate]

若如初见. 提交于 2019-11-30 17:27:54
Possible Duplicate: Structs, Interfaces and Boxing From the MSDN: http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. But what about generic interfaces? For example, int derives from both IComparable and IComparable<int> . Let's say I have the following code: void foo(IComparable value) { /* etc. */ } void bar(IComparable<T> value) { /* etc. */ } void gizmo() { int i = 42; bar(i); // is `i` boxed? I'd say YES foo(i); // is `i` boxed? I fear it is (but I hope for NO) }

Object type boxing with a reference type variable

最后都变了- 提交于 2019-11-30 15:06:08
问题 Boxing is when a value type is assigned to an object type. Is it the same when a reference type is assigned to an object? When a type (which isn't object) is assigned, what happens? Is that boxing too? int num=5; object obj = num; //boxing ////////////////////// MyClass my = new MyClass(); object obj = my; //what is name this convert (whethere is boxing?) 回答1: I assume you mean something like string s = "hello"; object x = s; // no boxing, just implict conversion to base-type. This works

Does a matter whether a value is primitive or boxed

亡梦爱人 提交于 2019-11-30 14:41:34
问题 One can use typeof to determine whether a value is primitive or boxed. Consider: typeof "foo"; // "string" typeof new String("foo"); // "object" In combination with Object.prototype.toString we could define the following two functions var toString = Object.prototype.toString; var is_primitive_string = function(s) { return toString.call(s) === "[object String]" && typeof s === "string"; }; var is_boxed_string = function(s) { return toString.call(s) === "[object String]" && typeof s === "object

Comparing primitive to wrapper object with == behaviour unexplained

懵懂的女人 提交于 2019-11-30 07:59:43
I have a piece of code which I need to understand: public static void main(String[] args) { Character c = new Character('a'); Character cy = new Character('a'); char cx = 'a'; System.out.println(c == cx); System.out.println(cx == cy); System.out.println(c == cy); } Output: true true false I am unable to understand why only the third statement is failing. EDIT: This question is different to the .equals vs == question as this about primitive versus object comparison. c and cy refer to different instances of the Character class (each time you invoke a constructor, you create a new instance), so

Why do structs need to be boxed?

ぐ巨炮叔叔 提交于 2019-11-30 05:51:59
问题 In C#, any user-defined struct is automatically a subclass of System.Struct System.ValueType and System.Struct System.ValueType is a subclass of System.Object . But when we assign some struct to object-type reference it gets boxed. For example: struct A { public int i; } A a; object obj = a; // boxing takes place here So my question is: if A is an descendant of System.Object , can't the compiler up-cast it to object type instead of boxing? 回答1: A struct is a value type. System.Object is a

Why does calling an explicit interface implementation on a value type cause it to be boxed?

笑着哭i 提交于 2019-11-30 03:29:43
My question is somewhat related to this one: How does a generic constraint prevent boxing of a value type with an implicitly implemented interface? , but different because it shouldn't need a constraint to do this because it's not generic at all. I have the code interface I { void F(); } struct C : I { void I.F() {} } static class P { static void Main() { C x; ((I)x).F(); } } The main method compiles to this: IL_0000: ldloc.0 IL_0001: box C IL_0006: callvirt instance void I::F() IL_000b: ret Why doesn't it compile to this? IL_0000: ldloca.s V_0 IL_0002: call instance void C::I.F() IL_0007: ret

Kotlin boxed Int are not the same

血红的双手。 提交于 2019-11-29 14:42:09
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 wrong way, someone guide to check the right place or explain it a bit for me. when it assigned boxedA =

Does the VB.NET “If” operator cause boxing?

谁都会走 提交于 2019-11-29 13:24:16
Those of us who've worked in VB/VB.NET have seen code similar to this abomination: Dim name As String = IIf(obj Is Nothing, "", obj.Name) I say "abomination" for three simple reasons: IIf is a function , all of whose parameters are evaluated; hence if obj is nothing in the above call then a NullReferenceException will be thrown. This is unexpected behavior for someone who's accustomed to short-circuited ternary operators in languages like C#. Because IIf is a function, it thus incurs the overhead of a function call. Again, though this isn't a big deal, it just doesn't feel right to someone who

Can I set a value on a struct through reflection without boxing?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 13:22:27
Actually, I should've asked: how can I do this and remain CLS Compliant? Because the only way I can think of doing this is as follows, but using either __makeref , FieldInfo.SetValueDirect or just System.TypedReference in general invalidates CLS Compliance. // code illustrating the issue: TestFields fields = new TestFields { MaxValue = 1234 }; // test struct with one field FieldInfo info = fields.GetType().GetField("MaxValue"); // get the FieldInfo // actual magic, no boxing, not CLS compliant: TypedReference reference = __makeref(fields); info.SetValueDirect(reference, 4096); The compliant