unboxing

C# - Issues with boxing / unboxing / typecasting ints. I don't understand

老子叫甜甜 提交于 2019-11-27 09:29:37
I'm having a hard time understanding this. Consider the following example: protected void Page_Load(object sender, EventArgs e) { // No surprise that this works Int16 firstTest = Convert.ToInt16(0); int firstTest2 = (int)firstTest; // This also works object secondTest = 0; int secondTest2 = (int)secondTest; // But this fails! object thirdTest = Convert.ToInt16(0); int thirdtest2 = (int)thirdTest; // It blows up on this line. } The specific error that I get at runtime is Specified cast is not valid. If I QuickWatch (int)thirdTest in Visual Studio, I get a value of Cannot unbox 'thirdTest' as a

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

不羁岁月 提交于 2019-11-27 06:39:22
问题 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); 回答1: 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

Differences in auto-unboxing between Java 6 vs Java 7

♀尐吖头ヾ 提交于 2019-11-27 06:10:59
I have noted a difference in auto unboxing behavior between Java SE 6 and Java SE 7. I'm wondering why that is, because I can't find any documentation of changes in this behavior between these two versions. Here's a simple example: Object[] objs = new Object[2]; objs[0] = new Integer(5); int myInt = (int)objs[0]; This compiles fine with javac from Java SE 7. However, if I give the compiler the "-source 1.6" argument I get an error on the last line: inconvertible types found : java.lang.Object required: int I tried downloading the Java SE 6 to compile with the native version 6 compiler (without

What is the difference between boxing/unboxing and type casting?

£可爱£侵袭症+ 提交于 2019-11-27 03:02:41
What is the difference between boxing/unboxing and type casting? Often, the terms seem to be used interchangeably. Boxing refers to a conversion of a non-nullable-value type into a reference type or the conversion of a value type to some interface that it implements (say int to IComparable<int> ). Further, the conversion of an underlying value type to a nullable type is also a boxing conversion. (Caveat: Most discussions of this subject will ignore the latter two types of conversions.) For example, int i = 5; object o = i; converts i to an instance of type object . Unboxing refers to an

Method overload resolution in java

时光毁灭记忆、已成空白 提交于 2019-11-26 22:25:10
Here is what I know about overload resolution in java: The process of compiler trying to resolve the method call from given overloaded method definitions is called overload resolution. If the compiler can not find the exact match it looks for the closest match by using upcasts only (downcasts are never done). Here is a class: public class MyTest { public static void main(String[] args) { MyTest test = new MyTest(); Integer i = 9; test.TestOverLoad(i); } void TestOverLoad(int a){ System.out.println(8); } void TestOverLoad(Object a){ System.out.println(10); } } As expected the output is 10.

C# - Issues with boxing / unboxing / typecasting ints. I don't understand

*爱你&永不变心* 提交于 2019-11-26 17:50:32
问题 I'm having a hard time understanding this. Consider the following example: protected void Page_Load(object sender, EventArgs e) { // No surprise that this works Int16 firstTest = Convert.ToInt16(0); int firstTest2 = (int)firstTest; // This also works object secondTest = 0; int secondTest2 = (int)secondTest; // But this fails! object thirdTest = Convert.ToInt16(0); int thirdtest2 = (int)thirdTest; // It blows up on this line. } The specific error that I get at runtime is Specified cast is not

Casting object to int throws InvalidCastException in C#

情到浓时终转凉″ 提交于 2019-11-26 17:16:44
问题 I have this method: private static Dossier PrepareDossier(List<List<object>> rawDossier) { return new Dossier((int)rawDossier[0][0]); } When I use it I get an InvalidCastException . However, when I use Convert.ToInt32(rawDossier[0][0]) it works just fine. What is the problem? 回答1: The problem is that you don't cast an object to an int , you're attempting to unbox an int. The object really has to be an int. It cannot be just anything that can be converted to an int. So the difference is that

Differences in auto-unboxing between Java 6 vs Java 7

只谈情不闲聊 提交于 2019-11-26 11:55:45
问题 I have noted a difference in auto unboxing behavior between Java SE 6 and Java SE 7. I\'m wondering why that is, because I can\'t find any documentation of changes in this behavior between these two versions. Here\'s a simple example: Object[] objs = new Object[2]; objs[0] = new Integer(5); int myInt = (int)objs[0]; This compiles fine with javac from Java SE 7. However, if I give the compiler the \"-source 1.6\" argument I get an error on the last line: inconvertible types found : java.lang

How java auto boxing/unboxing works?

余生颓废 提交于 2019-11-26 10:34:25
Since JDK 5.0, auto boxing/unboxing was introduced in java, the trick is simple and helpful, but when i started testing different conversions between wrapper classes and primitive types, i get really confused how the concept of auto boxing works in java, for example: Boxing int intValue = 0; Integer intObject = intValue; byte byteValue = 0; intObject = byteValue; // ==> Error After trying different cases ( short , long , float , double ), the only case which is accepted by the compiler is when the type of the value on the right of affectation operator is int . When i looked inside the source

Why can&#39;t I unbox an int as a decimal?

早过忘川 提交于 2019-11-26 10:29:37
I have an IDataRecord reader that I'm retrieving a decimal from as follows: decimal d = (decimal)reader[0]; For some reason this throws an invalid cast exception saying that the "Specified cast is not valid." When I do reader[0].GetType() it tells me that it is an Int32. As far as I know, this shouldn't be a problem.... I've tested this out by this snippet which works just fine. int i = 3750; decimal d = (decimal)i; This has left me scratching my head wondering why it is failing to unbox the int contained in the reader as a decimal. Does anyone know why this might be occurring? Is there