boxing

How is the boxing/unboxing behavior of Nullable<T> possible?

前提是你 提交于 2019-12-17 06:14:14
问题 Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable<T> can be assigned to null . For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T> : public static implicit operator Nullable<T>(object box); But the above operator clearly does not exist, as if it did then the following would also have to be legal, at least at compile-time (which it

How is the boxing/unboxing behavior of Nullable<T> possible?

浪子不回头ぞ 提交于 2019-12-17 06:14:13
问题 Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable<T> can be assigned to null . For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an implicit conversion from object to Nullable<T> : public static implicit operator Nullable<T>(object box); But the above operator clearly does not exist, as if it did then the following would also have to be legal, at least at compile-time (which it

.NET: Strange behaviour of double.Equals() when boxing

倖福魔咒の 提交于 2019-12-13 13:10:15
问题 What's going on here? int zero = 0; double x = 0; object y = x; Console.WriteLine(x.Equals(zero)); // True Console.WriteLine(y.Equals(zero)); // False 回答1: Here, you're calling two different methods - Double.Equals(double) and Object.Equals(object) . For the first call, int is implicitly convertable to double , so the input to the method is a double and it does an equality check between the two double s. However, for the second call, the int is not being cast to a double , it's only being

Auto Boxing vs static numbers

我怕爱的太早我们不能终老 提交于 2019-12-12 12:14:05
问题 Is it valuable for using Integer i = NumberUtils.INTEGER_ONE instead of Integer i = 1 ? I don't know what happen behind auto boxing. Thanks 回答1: Basically it will be compiled into: Integer i = Integer.valueOf(NumberUtils.INTEGER_ONE); assuming INTEGER_ONE is declared as an int . At execution time, assuming INTEGER_ONE has the value 1, that will actually return a reference to the same object each time, guaranteed by the Java Language Specification, because it's in the range -128 to 127. Values

Creating (boxed) primitive instance when the class is known

走远了吗. 提交于 2019-12-12 07:26:39
问题 I need a method that returns an instance of the supplied class type. Let's assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying String.class would return an empty String, supplying an Integer.class would return an Integer whose initial value is zero, and so on. But how do I create (boxed) primitive types on the fly? Like this? public Object newInstance(Class<?> type) { if (!type.isPrimitive()) { return type.newInstance();

Boxing the string in c# [closed]

社会主义新天地 提交于 2019-12-12 06:49:45
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Whenever I Search for Boxing in c#, I come across a cliche example like following: string s = "something"; Object o = s; This is very simple to understand that a value type is cast into a very generic reference type at run time. Well and good. I want to talk about little more specific kind of

How to compare two numeric values (SByte, Double) stored as Objects in .NET 2.0?

落爺英雄遲暮 提交于 2019-12-12 06:04:56
问题 In my custom sorting algorithm, I need to compare numeric types stored as Objects in an array. We can have SByte, Double, Int32, etc values mixed in one array. How can I compare two values of that array in my IComparer implementation? An example. Let's say we have two numeric values: object var1 = (sbyte)-8; object var2 = (double)123.456; It would be nice if code like the following one worked, but it fails: IComparable cmp1 = var1 as IComparable; IComparable cmp2 = var2 as IComparable;

How Upcasting preserve derived classes's Properties? [closed]

╄→尐↘猪︶ㄣ 提交于 2019-12-12 04:57:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . static void Main(string[] args) { Student student = new Student() { ID = 12, Name = "Manu", LastName = "Shekar" }; Iregister x = student; Student newstudent = x as Student; //Console.WriteLine(x.LastName); //Uncommenting this shows compilation error Console.WriteLine(newstudent

Boxing..do I get it right? [duplicate]

社会主义新天地 提交于 2019-12-11 03:09:37
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What is boxing and unboxing and what are the trade offs? Hi, From my understanding: When I assign data of value type to (reference) type object variable, it gets boxed and the result is not actual reference as it points to copy of the value stored on the heap. Is that right? Thanks 回答1: Well, not quite . (I misread your post to start with.) The result is a real reference - but it doesn't refer to your original

Hidden Boxing in the BCL?

老子叫甜甜 提交于 2019-12-11 02:28:07
问题 Recently I became aware that there are some parts in the BCL that still use some "legacy" code that was probably written before generics were introduced in v2.0 of the framework. Apparently, parts of that "legacy" code may cause the CLR to perform numerous boxing/unboxing operations. Since excessive usage of boxing is never a good thing, I wanted to know whether there are some other, critical places in the BCL that you've noticed that boxing occur? Thanks 回答1: Note that, for the specific