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 5960667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:51:29+00:00 2026-05-22T18:51:29+00:00

I’m creating a messaging system for use in an XNA game. My Message types

  • 0

I’m creating a messaging system for use in an XNA game. My Message types are structs because I want them to behave in a Value Type way.

struct MyMessageType1 : IMessage {}
struct MyMessageType2 : IMessage {}

List<IMessage> messageQueue = new List<IMessage>();

I want to be able to store Messages of different types in my message queue, but I want to do so without any of them being boxed.

If I have the structs implement an interface such as IMessage and I try to store them in a List, they get boxed.

I don’t know all the possible message types ahead of time, so I can’t just hard code one List for each type.

So the question is how can I store a list of structs of different types without them being boxed?

  • 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-22T18:51:29+00:00Added an answer on May 22, 2026 at 6:51 pm

    This cannot be done.

    Alternative 1

    However, you can emulate things, by using two Lists (List<MyMessageType1> and List<MyMessageType2>).

    You then concoct one Super Index (possibly, just another array of ints (longs?)) to make it possible to (indirectly) address an item as if it were one list.

    You might want to optimize the index (runlength encoding: store just the indexes where the backing array switches: this will also enormously help when iterating a subrange that is known to be contiguous in one of the backing arrays)

    Lists use Array storage internally, so
    – you get no boxing
    – fast random access
    – blazing iteration with list.ForEach

    Alternative 2

    Look at the StructLayout attribute and somehow emulate a Union by doing all the manipulations. If you are really prepared to get your hands dirty, throw in unsafe {} blocks (and compile with /unsafe) … however, seriously consider P/Invoke a C DLL or use C++/CLI if it matters that much

    Alternative 3 (added)

    Because I really liked the fact that Marc Gravell pointed out you can use the StructLayout that I mentioned, to pinpoint all three members of a union .NET struct at the same offset; I thought I’d go the extra step and see whether I could make that a hell of a lot more leaky tranparent still. This comes pretty close to being transparent:

    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    
    namespace LeakyAbstractions
    {
        struct TypeA {}
        struct TypeB {}
        struct TypeC {}
    
        [StructLayout(LayoutKind.Explicit)] internal struct AnyMessage {
            [FieldOffset(0)] public TypeA A;
            [FieldOffset(0)] public TypeB B;
            [FieldOffset(0)] public TypeC C;
    
            AnyMessage(TypeA a) { A = a; }
            AnyMessage(TypeB b) { B = b; }
            AnyMessage(TypeC c) { C = c; }
    
            public static implicit operator TypeA(AnyMessage msg) { return msg.A; }
            public static implicit operator TypeB(AnyMessage msg) { return msg.B; }
            public static implicit operator TypeC(AnyMessage msg) { return msg.C; }
    
            public static implicit operator AnyMessage(TypeA a) { return a; }
            public static implicit operator AnyMessage(TypeB b) { return b; }
            public static implicit operator AnyMessage(TypeC c) { return c; }
        }
    
        public class X
        {
            public static void Main(string[] s) 
            {
                var anyMessages = new List<AnyMessage> { 
                    new TypeA(),
                    new TypeB(),
                    new TypeC(),
                };
    
                TypeA a = anyMessages[0];
                TypeB b = anyMessages[1];
                TypeC c = anyMessages[2];
    
                anyMessages.Add(a);
                anyMessages.Add(b);
                anyMessages.Add(c);
            }
        }
    }
    

    I’ll leave the problem of discriminating this poor men’s variant as an exercise to you. The simplist way would be to add a field to the AnyMessage struct, but depending on the payload, other strategies might be much more (space/time) efficient.


    My $0.02

    Oh, I’d never actually do this, because it seems like overcomplicated. I’m assuming you have a valid reason to optimize this


    PS. If you are asking this after reading my answer here (yesterday: Should I use a struct or a class to represent a Lat/Lng coordinate?), I’m going to snap-judge this premature optimization

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

Sidebar

Related Questions

No related questions found

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.