What is boxing and unboxing and what are the trade offs?
I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome. ...
I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome. ...
First, two examples: // This works int foo = 43; long lFoo = foo; // This doesn't object foo = (int)43; long? nullFoo = foo as long?; // returns null long lFoo = (long)foo; // throws InvalidCastException if (foo.GetType() == typeof(int)) Console.WriteLine("But foo is an int..."); // This gets written out Now, my guess as to why t...
I've recently started working with JSON and the ExtJs framework and I've come across the following code in an example. we retrieve the information from the frontend using this: object updatedConfig = JavaScriptConvert.DeserializeObject(Request["dataForm"]); Then in the example they do the following: JavaScriptObject jsObj = update...
Hello, Another recent C# interview question I had was if I knew what Boxing and Unboxing is. I explained that value types are on Stack and reference types on Heap. When a value is cast to a reference type, we call it boxing and vice versa. Then he asked me to calculate this: int i = 20; object j = i; j = 50; What is i? I messed it ...
Hello guys, I'm trying to write some code to convert data from a object type field (come from a DataSet) into it's destination (typed) fields. I'm doing (trying at least) it using dynamic conversion. It seems to work fine for strings, int, DateTime. But it doesn't work for unsigned types (ulong, uint). Below there's a simple code that ...
Hi, This is not a question of what is boxing and unboxing, it is rather why do languages like Java and C# need that ? I am greatly familiar wtih C++, STL and Boost. In C++ I could write something like this very easily, std::vector<double> dummy; I have some experience with Java, but I was really surprised because I had to write som...
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 ...
What is the difference between boxing/unboxing and type casting? Often, the terms seem to be used interchangeably. ...
I just converted a code snippet from VB.NET to C# and stumbled over this issue. Consider this code: Dim x As Integer = 5 Dim y As Object = x Dim z As Decimal = CType(y, Decimal) No error from compiler or at runtime. z is five. Now let's translate this code to C# int x = 5; object y = x; decimal z = (decima...
I created the following property, which threw an InvalidCastException if the getter was accessed when ViewState[TOTAL_RECORD_COUNT] was null. public long TotalRecordCount { get { return (long)(ViewState[TOTAL_RECORD_COUNT] ?? -1); } set { ViewState[TOTAL_RECORD_COUNT] = value; } } My thought is that it incorrectly attempted to...
I'm just revising chapter 4 of C# in Depth which deals with nullable types, and I'm adding a section about using the "as" operator, which allows you to write: object o = ...; int? x = o as int?; if (x.HasValue) { ... // Use x.Value in here } I thought this was really neat, and that it could improve performance over the C# 1 equiva...
Let's examine the MSIL code generated for the following generic method: public static U BoxValue<T, U>(T value) where T : struct, U where U : class { return value; } Look: .method public hidebysig static !!U BoxValue<valuetype .ctor ([mscorlib]System.ValueType, !!U) T,class U>(!!T 'value') cil managed { .maxstack 8 IL_00...
Guys, I'm writing a class to represent a row from a SQL query. I want the field data to be accessed via the indexer property of the class. This is straightforward enough if I load the data into an internal List of Object. I've already tried this and am not happy with the boxing for the primitives. Boxing increases the memory requirement...
I am trying to do some work with HopenGL and I need a Ptr that points to a array of floats. From what I have read uarray and storableArray seem to be the way to go, in some combination some way. ...
Does .net CLR typecast the objects to the ones mentioned in the collection declaration? If i declare a List<string> lststrs= new List<string>(); lststrs.add("ssdfsf"); does .net typecasts this object while adding and retriving????? Well i think the question itself was not clearly understood by everyone.Let me elaborate. In java ther...
I got some weird exception when trying to compile this: Byte b = 2; if (b < new Integer(5)) { ... } Is it a valid check (unboxing-implicit cast - unboxing)? ...
I'm working with an older Oracle database, and I feel there's likely a better way to go about unboxing the values I retrieve from the database. Currently, I have a static class full of different type-specific methods: public static int? Int(object o) { try { return (int?)Convert.ToInt32(o); } catch (Exception) ...
I am a big fan of auto-boxing in Java as it saves a lot of ugly boiler plate code. However I have found auto-unboxing to be confusing in some circumstances where the Number object may be null. Is there any way to detect where auto-unboxing is occurring in a codebase with a javac warning? Any other solution to detect occurrences of unboxi...
I have a Dictionary(TKey, TValue) like Dictionary<int, ArrayList> Deduction_Employees = new Dictionary<int, ArrayList>(); and later I add to that array list an anonymous type like this var day_and_type = new { TheDay = myDay, EntranceOrExit = isEntranceDelay }; Deduction_Employees[Employee_ID].Add(day_and_type); Now h...
Is there any difference between the 2 methods below for calculating c ... specifically boxing/unboxing issues? Dim a As Integer? = 10 Dim b As Integer? = Nothing Dim c As Integer ' Method 1 c = If(a, 0) + If(b, 0) ' Method 2 c = a.GetValueOrDefault(0) + b.GetValueOrDefault(0) ...