create a list Using combination of two types

I need to create a list in which each member is a combination of two types A and B. Is it possible? For example, assuming A and B:

class A
{
    int a1;
    string a2;
}

class B
{
    int b1;
    string b2;
}

I want to create:

List<A&B> SampleList;

in a way that A&B contains following properties:

    int a1;
    string a2;
    int b1;
    string b2;
Jon Skeet
people
quotationmark

Three options:

  • Create a new class or struct composing both:

    public class AAndB
    {
        private readonly A a;
        private readonly B b;
    
        public AAndB(A a, B b)
        {
            this.a = a;
            this.b = b;
        }
    
        // etc
    }
    
  • Use Tuple<A, B> to do the same kind of thing, but without creating a new type.

  • If you only need this in a single method, use an anonymous type, e.g.

    var mixed = as.Zip(bs, (a, b) => new { A = a, B = b });
    

people

See more on this question at Stackoverflow