How can I return more than one object of different type in C# .net?

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

Answers


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;
        }
    }

No. You can't.

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


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?


You can return a List< Object >.


return new object[] { object1, object2 }

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


Need Your Help

View code generated by deriveJSON in Aeson

json haskell

I'm using Aeson to parse a JSON file in Haskell, and I'm letting Aeson auto-generate the parser by calling deriveJSON. Is there any way to view the code that Aeson generates? I tried calling derive...

UIView animation issue

ios objective-c uiview uipickerview

I am very new to both Objective-c and Xcode. I am trying to implement a simple UIPickerView + Toolbar setup that shows when a user taps on the Label element on the right and then when a user select...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.