Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:00:49+00:00 2026-05-24T00:00:49+00:00

In C# is there any cost of storing ints (or any value types) as

  • 0

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.");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:00:50+00:00Added an answer on May 24, 2026 at 12:00 am

    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:

    [StructLayout(LayoutKind.Explicit)]
    [CLSCompliant(false)]
    public struct NumberStackEntry
    {
        /// <summary>Type of entry</summary>
        [FieldOffset(0)]
        public EntryTypes entryType;
    
        /// <summary>Value if double</summary>
        [FieldOffset(4)]
        public double dval;
    
        /// <summary>Value if ulong</summary>
        [FieldOffset(4)]
        public ulong uval;
    
        /// <summary>Value if long</summary>
        [FieldOffset(4)]
        public long lval;
    
        /// <summary>Value if integer</summary>
        [FieldOffset(4)]
        public int ival;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any benefit (or conversely, cost) to passing a parameter by const value
Is there any overhead when we cast objects of one type to another? Or
Is there any cost/drawback (apart from typing too much) to adorning a class with
Is there by any chance a way to set shipping cost by state in
Is there any way to create Map or Set type data structures at compile
In C, suppose I need to take input from a string int num,cost; char
Is there any sensible way of transforming this: (for ([cos-t (stream-map cos t-range)] [sin-t
Is there any functional difference in Python between a try statement and an if
Is there any way in Notepad++ (or even with another tool) to change the
Is there any way I can set a formatter on models that will convert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.