1

In a method

  1. count is calculated

  2. a list is also formed

I need to return this count along with the list in c#. Any suggestions on how to return this?

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74

4 Answers4

2

You should avoid using out and ref parameters.

I suggest creating a type that represents the output result.

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

FxCop rule for out parametes.

Community
  • 1
  • 1
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
0

2 options

  1. using out param for count, with return type as List
  2. using a Tuple or custom Class which will contain both of your Count and List

Sample Code:

 public List<Class1> GetData(out int Count)
 { 
   //... 
    //Your Logic with returning list
 }

Option2:

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; }
}
Raghuveer
  • 2,630
  • 3
  • 29
  • 59
0

You could simply return a List and a get a Count in caller method. You don't really require to return both.

Still interested? multiple options.

Using out parameter for count.

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

using Tuple as pointed in comments.

public Tuple<List<something>, int> DoSomething()
{
    ....
    return new Tuple<List<something>, int>(list1, count); 
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • 1
    Using Tuples is [not a good idea here](http://stackoverflow.com/questions/3017352/is-using-net-4-0-tuples-in-my-c-sharp-code-a-poor-design-decision). – Hamid Pourjam May 05 '16 at 07:30
  • That's true, but just to let OP know that there is an option :-). I don't really see any valid case he needs both the parameters in the first place. – Hari Prasad May 05 '16 at 07:35
0

You should use the out keyword in C#.

MSDN description. "Declaring an out method is useful when you want a method to return multiple values. This enables methods to return values optionally."

Here is the MSDN documentation.

Here is an example:

public List<YourType> SomeMethodName(out int count)
{
    //Your calculation here
}
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73
  • 1
    Well, I think you _can_ use out but I don't think you _should_ use it. It's certainly a matter of taste and quite opinion-based, but I think out almost always creates an awful api ;) – Jan Köhler May 05 '16 at 07:26
  • @khlr Thanks for sharing your input. If I may ask, what is your opinion on out, and why you should not use it? – monstertjie_za May 05 '16 at 07:29
  • I try to avoid it because I had to deal with a lot of legacy code which extensively used out in several inappropriate situations. In my opinion it's always a pain if you call a method and need to declare another variable beforehand for the out parameter (though C# 6 [brought improvements](http://odetocode.com/blogs/scott/archive/2014/09/15/c-6-0-features-part-3-declaration-expressions.aspx) in those cases). Furthermore I think it's _cleaner_ if a method has one defined return value and not two (or even more...). I prefer returning a custom defined type in those cases as suggested by @dotctor. – Jan Köhler May 05 '16 at 09:21
  • @khlr I totally agree with returning a custom type for these situations. I also read through the answer in the post given by dotctor, by John Skeet, and that was pretty interesting. – monstertjie_za May 05 '16 at 09:30