tags:

views:

90

answers:

5

Hi,

I have two objects and I dont want to create a wrapper class to have data of this two object, no 'Out' parameter is there any way that I can return more than one object of different type?

Will really appreciate it!!

Thanks

+5  A: 

No. You can't.

You can use an array or a map to return them, though.

Pablo Santa Cruz
+1 - the most sane solution is ruled out in the question!
Sohnee
+3  A: 
public class VPair<TFirst, TSecond>
    {
        public TFirst First { get; set; }
        public TSecond Second { get; set; }

        public VPair(TFirst first, TSecond second)
        {
            First = first;
            Second = second;
        }
    }

or Tuple class in c#4.0

bonus:

public class VTriple<TFirst, TSecond, TThird> : VPair<TFirst, TSecond>
    {
        public TThird Third { get; set; }

        public VTriple(TFirst first, TSecond second, TThird third)
            : base(first, second)
        {
            Third = third;
        }
    }
igor
A: 

You can return a List< Object >.

Carra
+1  A: 

In c++ this can be done with something called std::pair. There is a StackOverflow post on how to do it in C#: What is C# analog of C++ std::pair?

Anders Abel
A: 
return new object[] { object1, object2 }

Alternatively, you can use an anonymous type. See this article for more information.

Ilia Jerebtsov