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
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
No. You can't.
You can use an array or a map to return them, though.
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;
}
}
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?
return new object[] { object1, object2 }
Alternatively, you can use an anonymous type. See this article for more information.