Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回一个对象和一个列表_C#_.net_Json_List_Return - Fatal编程技术网

C# 返回一个对象和一个列表

C# 返回一个对象和一个列表,c#,.net,json,list,return,C#,.net,Json,List,Return,以某种方式 计数是计算出来的 还形成了一份清单 我需要将此计数与c#中的列表一起返回。关于如何返回此文件有什么建议吗?2选项 使用out参数进行计数,返回类型为List 使用一个元组或自定义类,该类将同时包含计数和列表 示例代码: public List<Class1> GetData(out int Count) { //... //Your Logic with returning list } public CustomClass DoSomethin

以某种方式

  • 计数是计算出来的

  • 还形成了一份清单

  • 我需要将此计数与c#中的列表一起返回。关于如何返回此文件有什么建议吗?

    2选项

  • 使用out参数进行计数,返回类型为List
  • 使用一个元组或自定义类,该类将同时包含
    计数
    列表
  • 示例代码:

     public List<Class1> GetData(out int Count)
     { 
       //... 
        //Your Logic with returning list
     }
    
    public CustomClass DoSomething()
    {
        var data = new CustomClass();
        //....
        data.Data = list;
        data.Count = list.Count();
        return data;
    }
    public class CustomClass
    {
        public List<Class1> Data { get; set; }
        public int Count { get; set; }
    }
    
    public List GetData(out int Count)
    { 
    //... 
    //您的逻辑与返回列表
    }
    
    选项2:

     public List<Class1> GetData(out int Count)
     { 
       //... 
        //Your Logic with returning list
     }
    
    public CustomClass DoSomething()
    {
        var data = new CustomClass();
        //....
        data.Data = list;
        data.Count = list.Count();
        return data;
    }
    public class CustomClass
    {
        public List<Class1> Data { get; set; }
        public int Count { get; set; }
    }
    
    public CustomClass DoSomething()
    {
    var data=new CustomClass();
    //....
    数据。数据=列表;
    data.Count=list.Count();
    返回数据;
    }
    公共类CustomClass
    {
    公共列表数据{get;set;}
    公共整数计数{get;set;}
    }
    
    您只需在调用者方法中返回一个
    列表
    和一个get a
    计数
    。你不需要两个都返回

    还有兴趣吗?多种选择

    使用
    out
    参数进行计数

    public List<something> DoSomething(out int count)
    {
        ....
        count = list.Count();
        return list; 
    }
    

    您应该在C#中使用out关键字

    MSDN说明。“当您希望一个方法返回多个值时,声明out方法非常有用。这使方法可以选择性地返回值。”

    是MSDN文档

    以下是一个例子:

    public List<YourType> SomeMethodName(out int count)
    {
        //Your calculation here
    }
    
    public List SomeMethodName(out int count)
    {
    //你的计算在这里
    }
    
    你应该这样做。

    我建议创建一个表示输出结果的类型

    public DoSomethingResult DoSomething()
    {
        var result = new DoSomethingResult();
        //....
        result.Data = GenerateList();
        result.Count = CalculateCount();
        return result;
    }
    
    public class DoSomethingResult
    {
        public List<YourType> Data { get; set; }
        public int Count { get; set; }
    }
    
    public DoSomething结果DoSomething()
    {
    var结果=新的DoSomethingResult();
    //....
    result.Data=GenerateList();
    result.Count=CalculateCount();
    返回结果;
    }
    公共类DoSomethingResult
    {
    公共列表数据{get;set;}
    公共整数计数{get;set;}
    }
    

    如果count是list的一部分,则只需返回list并在caller中获取count即可。如果count不属于list,那么您可以将list作为方法返回类型返回,并将count作为out参数。您还可以返回
    元组
    。您还可以返回匿名类型
    返回新的{count=countVar,list=listVar}
    返回列表并使用list.Count属性检查列表项的计数。这是一个粗体声明:)我阅读了链接,我们通常不需要使用它们,但不太确定必须避免使用它们。必须避免使用
    out
    ref
    ,直到没有其他方法或性能问题为止@HariPrasadWell,我想你可以用它,但我不认为你应该用它。这当然是一个品味问题,也是一个基于观点的问题,但我认为out几乎总是会创建一个糟糕的api;)@khlr感谢分享您的意见。如果我可以问一下,您对out的看法是什么,为什么不应该使用它?我尽量避免使用它,因为我必须处理大量在几种不合适的情况下广泛使用的遗留代码。在我看来,如果调用一个方法并且需要事先为out参数声明另一个变量(尽管在这些情况下是C#6),那么这总是一件痛苦的事情。此外,我认为如果一个方法有一个定义的返回值而不是两个(甚至更多…),那么它会更干净。我更喜欢在@dotctor建议的情况下返回自定义类型。@khlr我完全同意在这些情况下返回自定义类型。我还通读了Doctor和John Skeet在帖子中给出的答案,这非常有趣。使用元组是正确的。这是为了让OP知道有一个选项:-)。我真的看不到任何有效的例子,他首先需要这两个参数。