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)?
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.
So my question is which approach will perform better or will both be the same?
Or should i stick with specific “columns” for all value types and store all user defined reference types as object?
Thanks – Example code below – Static Test method shows usage.
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.");
}
}
}
This is called boxing and the cost is actually huge, both in performance and in memory use. If you want to avoid boxing and you want to cause a few simple types like int and double to share memory, use the LayoutKind struct layout attribute to force them to share memory, then set your list to be of that struct type. For example:
Edit: You can make an array of the above, without boxing to fill members of the array with these values. You cannot add an array to the struct, though, and make it share memory with non-CLI reference types.