【问题标题】:How can I return more than one object of different type in C# .net?如何在 C# .net 中返回多个不同类型的对象?
【发布时间】:2011-01-22 16:13:31
【问题描述】:

我有两个对象,我不想创建一个包装类来保存这两个对象的数据,没有“Out”参数,有什么方法可以返回多个不同类型的对象?

真的很感激!!

谢谢

【问题讨论】:

    标签: .net c#-3.0


    【解决方案1】:
    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;
            }
        }
    

    Tuple c#4.0 中的类

    奖金:

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

    【讨论】:

      【解决方案2】:
      return new object[] { object1, object2 }
      

      或者,您可以使用匿名类型。 See this article for more information.

      【讨论】:

        【解决方案3】:

        在 c++ 中,这可以通过称为 std::pair 的东西来完成。 StackOverflow 上有一篇关于如何在 C# 中执行此操作的帖子:What is C# analog of C++ std::pair?

        【讨论】:

          【解决方案4】:

          你可以返回一个列表

          【讨论】:

            【解决方案5】:

            没有。你不能。

            不过,您可以使用数组或映射来返回它们。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-05-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-07-09
              • 2019-01-29
              相关资源
              最近更新 更多