Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any cost of storing ints (or any value types) as objects and casting them back to int(or the value type)?
    text
    copied!<p>In C# is there any cost of storing ints (or any value types) as objects and casting them back to int(or the value type)?</p> <p>Basically I need to create an in-memory table. I can create "columns" specific for each possible type (so any primitive value type such as int, double, string etc and also user defined reference types i want store in the table such as Order) or I could simply store EVERYTHING as an object and cast them back to the correct type when accessing the table.</p> <p>So my question is which approach will perform better or will both be the same?</p> <p>Or should i stick with specific "columns" for all value types and store all user defined reference types as object? </p> <p>Thanks - Example code below - Static Test method shows usage.</p> <pre><code>public sealed class Columns { public static void Test() { Columns cols = new Columns(100); cols.SetInt(0,Int_Column,12345); int value = cols.GetInt(0,Int_Column); cols.SetObject(1,Object_Column,12345); int value2 = (int)cols.GetObject(1,Object_Column); } private const int Int_Column = 0; private const int String_Column = 1; private const int Object_Column = 2; private int[] _intCol; private string[] _stringCol; private object[] _objCol; public Columns(int rowCount) { _intCol = new int[rowCount]; _stringCol = new string[rowCount]; _objCol = new object[rowCount]; } public void SetInt(int rowIndex, int colIndex, int value) { switch(colIndex) { case Int_Column: _intCol[rowIndex] = value; break; default: throw new Exception("Incorrect column index specified."); } } public int GetInt(int rowIndex, int colIndex) { switch(colIndex) { case Int_Column: return _intCol[rowIndex]; default: throw new Exception("Incorrect column index specified."); } } public void SetString(int rowIndex, int colIndex, string value) { switch(colIndex) { case String_Column: _stringCol[rowIndex] = value; break; default: throw new Exception("Incorrect column index specified."); } } public string GetString(int rowIndex, int colIndex) { switch(colIndex) { case String_Column: return _stringCol[rowIndex]; default: throw new Exception("Incorrect column index specified."); } } public void SetObject(int rowIndex, int colIndex, object value) { switch(colIndex) { case Object_Column: _objCol[rowIndex] = value; break; default: throw new Exception("Incorrect column index specified."); } } public object GetObject(int rowIndex, int colIndex) { switch(colIndex) { case Object_Column: return _objCol[rowIndex]; default: throw new Exception("Incorrect column index specified."); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload