boxing

Boxing and Unboxing in String.Format(…) … is the following rationalized?

孤街浪徒 提交于 2019-12-10 01:04:54
问题 I was doing some reading regarding boxing/unboxing, and it turns out that if you do an ordinary String.Format() where you have a value type in your list of object[] arguments, it will cause a boxing operation. For instance, if you're trying to print out the value of an integer and do string.Format("My value is {0}",myVal) , it will stick your myVal int in a box and run the ToString function on it. Browsing around, I found this article. It appears you can avoid the boxing penalty simply by

How to unbox elements contained in polymorphic vectors?

邮差的信 提交于 2019-12-09 18:47:49
问题 After reading this answer to "Vector of objects belonging to a trait", it looks like Rust does automatic unboxing. Is this the case? My code doesn't compile and I don't understand how that answer's code could compile. What is the correct way to unbox the elements of a polymorphic vector, one containing boxed traits? I've read Rust by Example and the Box documentation and I can't see any method that looks like unbox() . My code is: trait HasArea { fn area(&self) -> f64; } struct Circle { x:

Is boxing involved when calling ToString for integer types?

杀马特。学长 韩版系。学妹 提交于 2019-12-09 14:53:22
问题 Very simple question: int a = 5; string str = a.ToString(); Since ToString is a virtual method of System.Object, does it mean that everytime I call this method for integer types, a boxing occurs? 回答1: You've already got answers telling you that when ToString() is overridden for a value type, there will be no boxing when you call it, but it's nice to have some way of actually seeing that. Take the type int? ( Nullable<int> ). This is a useful type because it is a value type, yet boxing may

Boxed Value Type comparisons

吃可爱长大的小学妹 提交于 2019-12-09 14:49:49
问题 What i'm trying to achieve here is a straight value comparison of boxed primitive types. ((object)12).Equals((object)12); // Type match will result in a value comparison, ((object)12).Equals((object)12d); // but a type mismatch will not. (false) object.Equals((object)12,(object)12d); // Same here. (false) I understand the 'why'. I just don't see a 'how'. The types are unknown until runtime, where they could be any primitive type from a datasource. That includes strings, datetimes, bools, etc.

Why does ((object)(int)1).Equals(((object)(ushort)1)) yield false?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 15:07:19
问题 I have the Situation that I have an object which I want to check for equality with another object . public static bool Equals(object a, object b) { return a.Equals(b); } A Problem occurs when a = 1 (integer) and b = 1 (ushort (or basically not integer)) . I wondered whether this shouldn't yield true, but it does return false... Edit What makes it even worse is this: Hashtable ht = new Hashtable(); ht.Add((int)1, "SOME STRING"); ht.Add((short)1, "SOME STRING"); ht.Add((long)1, "SOME STRING");

Compare two integer objects for equality regardless of type

假如想象 提交于 2019-12-08 14:28:16
问题 I'm wondering how you could compare two boxed integers (either can be signed or unsigned) to each other for equality. For instance, take a look at this scenario: // case #1 object int1 = (int)50505; object int2 = (int)50505; bool success12 = int1.Equals(int2); // this is true. (pass) // case #2 int int3 = (int)50505; ushort int4 = (ushort)50505; bool success34 = int3.Equals(int4); // this is also true. (pass) // case #3 object int5 = (int)50505; object int6 = (ushort)50505; bool success56 =

Curiosity: Converting a C# struct into an object still copies it

会有一股神秘感。 提交于 2019-12-08 14:14:24
This question is more out of curiosity than a real problem. Consider the following code (C# 4.0, if it matters): class Program { static Point myPoint = new Point(3, 5); static void Main(string[] args) { Console.WriteLine("Point Struct Before: " + myPoint); object point = GetPoint(); Console.WriteLine("Point Object Before: " + point); myPoint.X = 10; Console.WriteLine("Point Struct After: " + myPoint); Console.WriteLine("Point Object After: " + point); } static object GetPoint() { return myPoint; } } This outputs the following: Point Struct Before: 3;5 Point Object Before: 3;5 Point Struct

Checking Null Wrappers against primitive values

耗尽温柔 提交于 2019-12-08 02:12:00
问题 Integer i = null; if (i == 3) Why the second line above throws a NullPointerException , IMHO, this has only one meaning which is Wrapper Object i is to be unboxed which yields the Exception such as: ArrayList<Integer> list = new ArrayList<Integer>(); list.add(null); int x = list.get(0); EDIT: Can you supply me with some format doc? 回答1: It throws NPE because compiler does the following "magic" for you: Integer i = null; if (i.intValue() == 3) Obviously i.intValue() throws NPE when i is null .

Is converting this ArrayList to a Generic List efficient?

最后都变了- 提交于 2019-12-07 06:02:59
问题 The code I'm writing receives an ArrayList from unmanaged code, and this ArrayList will always contain one or more objects of type Grid_Heading_Blk. I've considered changing this ArrayList to a generic List, but I'm unsure if the conversion operation will be so expensive as to nullify the benefits of working with the generic list. Currently, I'm just running a foreach (Grid_Heading_Blk in myArrayList) operation to work with the ArrayList contents after passing the ArrayList to the class that

Are value types boxed when passed as generic parameters with an interface constraint?

若如初见. 提交于 2019-12-07 04:59:15
问题 (As a result of doing the research to answer this question, I (think I have!) determined that the answer is "no." However, I had to look in several different places to figure this out, so I think there is still value to the question. But I won't be devastated if the community votes to close.) For example: void f<T>(T val) where T : IComparable { val.CompareTo(null); } void g() { f(4); } Is 4 boxed? I know that explicitly casting a value type to an interface that it implements triggers boxing: