【问题标题】:How to return Multiple List<> values from a single function?如何从单个函数返回多个 List<> 值?
【发布时间】:2016-03-31 20:05:32
【问题描述】:

试图从单个函数返回 2 个列表值

我正在使用此代码:-

public KeyValuePair<int, int> encrypt(string password)
    {
        List<int> key = new List<int>();
        List<int> code = new List<int>();
        /*
           do stuff, do some more stuff and go!
        */

        return new KeyValuePair<List<int>,List<int>>(key,code);
    }

在这里我试图返回 2 个List&lt;int&gt; 值,但发生错误。如何从单个函数返回 2 个列表值

更新

找到了答案,我们得到了 2 个正确答案,这就是为什么我不只选择一个,因为两者都很好用

HadiRj的回答

谜题

回答

如果你想使用我的代码,那么这是它的正确版本:-

public KeyValuePair<List<int>, List<int>> encrypt(string password)
    {
        List<int> key = new List<int>();
        List<int> code = new List<int>();
        /*
           do stuff, do some more stuff and go!
        */

        return new KeyValuePair<List<int>,List<int>>(key,code);
    }

【问题讨论】:

  • 创建一个类,添加任何你想返回的属性,然后返回一个类的实例
  • 这是个好主意,但还有其他方法
  • 是要返回列表(合并)还是返回列表(单独)
  • 也许您可以使用 one List&lt;&gt; 对,而不是像您拥有的一对列表?如果是这样,可以使用List&lt;KeyValuePair&lt;int, int&gt;&gt; 或者甚至简单地使用Dictionary&lt;int, int&gt; 或SortedDictionary&lt;int, int&gt;。这取决于您要允许的数据。在您的解决方案中,键 List&lt;&gt; 的计数可能大于或小于值 List&lt;&gt; 的计数。

标签: c# list function keyvaluepair


【解决方案1】:

将你的函数减速改为

public KeyValuePair<List<int>, List<int>> encrypt(string password)

P.S:我不推荐这个!创建新课程是处理您的问题的更好主意

【讨论】:

  • 您的意思是将 KeyValuePair 更改为您的公共 KeyValuePair, List> ?
【解决方案2】:

方式一:元组:

       public Tuple<List<int>, List<int>> func()
    {
        List<int> key = new List<int>() { 2,34,5};
        List<int> code = new List<int>() { 345,67,7};
        return Tuple.Create<List<int>,List<int>>(key, code);

    }

方式2:

视图模型

    public class retViewModel
{
    public List<int> key { get; set; }
    public List<int> code { get; set; }
}



public retViewModel func()
        {
            List<int> key = new List<int>() { 2,34,5};
            List<int> code = new List<int>() { 345,67,7};
            retViewModel obj = new retViewModel() { 
            code=code,
            key=key
            };
            return obj;
        }

【讨论】:

    【解决方案3】:

    在这种情况下,一个相当简洁的方法是使用out 参数。

    public void encrypt(string password, out List<int> key, out List<int> code)
    {
        key = new List<int>();
        code = new List<int>();
        /*
           do stuff, do some more stuff and go!
        */
    }
    

    【讨论】:

    • Note: 避免使用 out 或 reference 参数。使用定义输出或引用参数的成员需要开发人员了解指针、值类型和引用类型之间的细微差别,以及输出和引用参数之间的初始化差异。
    • @HadiRj - 我也更喜欢避免使用它们,但有时它们是合适的 - 我使用“在这种情况下”这个词来帮助提供这种含义。
    【解决方案4】:
    List<int> key;
    List<int> code;
    static void Main(string[] args)
            {
                key = new List<int>();
                code = new List<int>();
                encrypt("",ref key,ref code);
            }
      public void encrypt(string password, ref List<int> key, ref List<int> code)
            {
    
                /*
                   do stuff, do some more stuff and go!
                */
    
    
            }
    

    【讨论】:

      【解决方案5】:

      您始终可以返回 List&lt;List&lt;int&gt;&gt;。从您的代码中我可以看出,您使用 KVP 的唯一原因是因为您知道您将返回两个列表。然后我会说创建另一个对象,您可以在其中拥有密钥和代码:

       public class EncryptionResult
       {
           public IList<int> Key {get; set;}
           public IList<int> Code {get; set;}
       }
      

      我不建议您使用其他一些 cmets 建议的 out/ref 解决方案。使用它们返回多个参数不是一个好习惯,应该避免使用它们。此外,如果您因为需要更多不同的数据而在任何时间点扩展/修改该对象,则无需更改接口的签名,但是如果您更改所需的参数,则需要修改每个方法和调用者(包括你所有的测试)。

      【讨论】:

        【解决方案6】:

        非常简单

        [WebMethod]
        public static List<Teacher>[] BindData()
        {
           List<Teacher> list1 = new List<Teacher>();
           List<Teacher> list2 = new List<Teacher>();
           return new List<Teacher>[] { list1, list2 };
        }
        

        注意:两个列表都将使用与我在两个列表中使用的教师类相同的类。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-01-01
          • 2021-05-13
          • 1970-01-01
          • 1970-01-01
          • 2022-04-29
          • 1970-01-01
          • 2021-12-21
          相关资源
          最近更新 更多