【问题标题】:Is it possible to return multiple values with different types ?是否可以返回具有不同类型的多个值?
【发布时间】:2013-12-16 17:07:25
【问题描述】:

在 Javascript 中,我可以创建一个返回对象的函数:

function person() {
  return { name: "John Doe", age: 20, isMarried: false };
}

console.log("Name: "+ person().name +". Age: "+ person().age +". Is married: "+ person().isMarried);

OUTPUT:
> "Name: John Doe. Age: 20. Is Married: false"

我想知道是否有可能在 C# 中做这样的事情?我一直在阅读有关委托、字典和匿名方法的信息,但我仍然对此一无所知。

【问题讨论】:

  • 当然只是实例化一个类
  • 返回字典 类似。
  • Anonymous Types 是您要找的。 JLe 的第二个 sn-p 使用了这个,但如果你想进一步调查,你应该查找 anonymous types
  • 哇,这么多好答案!但是我在 Unity 中使用 C#,显然它也不支持 System.Dynamic 或 System.Tuple。我想我留下了 Hessam 使用 Out 的答案,但我不知道如何用它返回多个值。我应该将我的问题编辑得更具体,还是应该前往 Unity 回答网站并在那里提问?

标签: c# javascript object return


【解决方案1】:

您可以返回object

object Person() {
    var p = new ExpandoObject();
    p.Name = "John Doe";
    p.Age = 20;
    p.Married = false;

    return p;
}

您还可以创建更加“动态”的内容:

object Person() {
    return new {
        Name = "John Doe",
        Age = 20,
        Married = false
    };
}

【讨论】:

  • 你如何从另一边得到值。我不认为你可以,因为 c# 使用强类型,return new {} 是一个无法强制转换的匿名类型。
【解决方案2】:

你可以用这个

public void method(out string Name, out int Age, out bool Married){
    //body of method
    }

【讨论】:

    【解决方案3】:
    static void Main(string[] args)
    {
        dynamic p = person();
        Console.WriteLine("Name: {0}, Age: {1}, Is married: {2}", p.name, p.age, p.isMarried);
    }
    static dynamic person()
    {
        return new { name = "John Doe", age = 20, isMarried = false };
    }
    

    More info about the dynamic keyword

    【讨论】:

      【解决方案4】:

      在 .NET 4.5 中有一个叫做 TUPLE 的东西。你可以用那个。 欲了解更多信息..您可以点击链接。

      http://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx

      【讨论】:

        【解决方案5】:

        您还可以返回一个包含多个值的元组。

        static void Main(string[] args)
        {
            var tuple = GetItem()
            Console.WriteLine(string.Format("Item1: {0}", tuple.Item1));
            Console.WriteLine(string.Format("Item2: {0}", tuple.Item2));
        }
        
        public static Tuple<string, int> GetItem()
        {
            return new Tuple<string, int>("Some item from database.", 4);
        }
        

        【讨论】:

          【解决方案6】:
          class Program
          {
              static void Main(string[] args)
              {
                  Tuple<int, string, double, string> emp = GetEmployeeInfo();
                  Console.WriteLine("Emplooyee Id :: " + emp.Item1);
                  Console.WriteLine("Employee Name :: " + emp.Item2);
                  Console.WriteLine("Employee Salary :: " + emp.Item3);
                  Console.WriteLine("Employee Address :: " + emp.Item4);
              }
          
              static Tuple<int, string, double, string> GetEmployeeInfo()
              {
                  Tuple<int, string, double, string> employee;
                  employee = new Tuple<int, string, double, string>(1001, "Uthaiah Bollera", 4500000.677, "Bangalore Karnataka!");
                  return employee;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2014-08-24
            • 1970-01-01
            • 1970-01-01
            • 2016-01-29
            • 2017-05-19
            • 2013-10-26
            • 1970-01-01
            • 1970-01-01
            • 2016-06-09
            相关资源
            最近更新 更多