C# 如何返回多个列表<&燃气轮机;来自单个函数的值?

C# 如何返回多个列表<&燃气轮机;来自单个函数的值?,c#,list,function,keyvaluepair,C#,List,Function,Keyvaluepair,尝试从单个函数返回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 stuf

尝试从单个函数返回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);
    }
公钥值对加密(字符串密码)
{
列表键=新列表();
列表代码=新列表();
/*
做点什么,再做点什么,走吧!
*/
返回新的KeyValuePair(密钥、代码);
}
在这里,我试图返回2个
列表
值,但出现了错误。如何从单个函数返回2个列表值

更新

答案被找到了,我们得到了两个正确的答案,这就是为什么我没有选择一个,因为两个都很好

由哈迪尔回答

通过“神秘性”回答

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

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);
    }
公钥值对加密(字符串密码)
{
列表键=新列表();
列表代码=新列表();
/*
做点什么,再做点什么,走吧!
*/
返回新的KeyValuePair(密钥、代码);
}

将功能更改为减速

public KeyValuePair<List<int>, List<int>> encrypt(string password)
公钥值对加密(字符串密码)
我不推荐这个!创建新类是处理问题的更好方法

方法1:元组:

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

    }
公共元组func()
{
List key=new List(){2,34,5};
列表代码=新列表(){345,67,7};
返回Tuple.Create(键、代码);
}
方式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;
        }
公共类retViewModel
{
公共列表键{get;set;}
公共列表代码{get;set;}
}
public retViewModel func()
{
List key=new List(){2,34,5};
列表代码=新列表(){345,67,7};
retViewModel obj=新的retViewModel(){
代码=代码,
钥匙
};
返回obj;
}

在这种情况下,使用
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!
    */
}
public void encrypt(字符串密码、输出列表密钥、输出列表代码)
{
key=新列表();
代码=新列表();
/*
做点什么,再做点什么,走吧!
*/
}
列表键;
列表代码;
静态void Main(字符串[]参数)
{
key=新列表();
代码=新列表();
加密(“”,引用密钥,引用代码);
}
公共无效加密(字符串密码、参考列表密钥、参考列表代码)
{
/*
做点什么,再做点什么,走吧!
*/
}

您始终可以返回
列表。从代码中我可以看到,使用KVP的唯一原因是因为您知道将返回两个列表。然后我会说创建另一个对象,其中可以包含密钥和代码:

 public class EncryptionResult
 {
     public IList<int> Key {get; set;}
     public IList<int> Code {get; set;}
 }
公共类加密结果
{
公共IList密钥{get;set;}
公共IList代码{get;set;}
}

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

非常简单

[WebMethod]
public static List<Teacher>[] BindData()
{
   List<Teacher> list1 = new List<Teacher>();
   List<Teacher> list2 = new List<Teacher>();
   return new List<Teacher>[] { list1, list2 };
}
[WebMethod]
公共静态列表[]BindData()
{
List list1=新列表();
List list2=新列表();
返回新列表[]{list1,list2};
}

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

创建一个类,添加任何要作为属性返回的内容,并返回该类的实例,这是一个好主意,但得到的任何其他方法是您要返回列表(组合)或作为列表返回(单独)也许你可以使用一个
列表
对,而不是像你这样的一对列表?如果是这样,可以使用
列表
,甚至可以简单地使用
词典
分类词典
。这取决于您希望允许哪些数据。在您的解决方案中,密钥
列表
的计数可能大于或小于值
列表
的计数。可能的重复是指仅将KeyValuePair更改为您的public KeyValuePair?:避免使用out或reference参数。使用定义out或reference参数的成员需要开发人员理解指针、值类型和引用类型之间的细微差异以及out和reference参数之间的初始化差异。@HadiRj-我也喜欢避免它们,但有时它们是合适的-我使用了“在这种情况下”这个词帮助提供这一意义。
[WebMethod]
public static List<Teacher>[] BindData()
{
   List<Teacher> list1 = new List<Teacher>();
   List<Teacher> list2 = new List<Teacher>();
   return new List<Teacher>[] { list1, list2 };
}