What is the best way to return two lists in C#?

前端 未结 5 2350
情话喂你
情话喂你 2020-12-31 05:12

I am almost embarrassed to ask this question, but as a long time C programmer I feel that perhaps I am not aware of the best way to do this in C#.

I have a member fu

5条回答
  •  自闭症患者
    2020-12-31 05:55

    First of all, that should probably be out, not ref.

    Second, you can declare and return a type containing the two lists.

    Third, you can declare a generic Tuple and return an instance of that:

    class Tuple {
       public Tuple(T first, U second) { 
           First = first;
           Second = second;
       }
       public T First { get; private set; }
       public U Second { get; private set; }
    }
    
    static class Tuple {
       // The following method is declared to take advantage of
       // compiler type inference features and let us not specify
       // the type parameters manually.
       public static Tuple Create(T first, U second) {
            return new Tuple(first, second);
       }
    }
    
    return Tuple.Create(firstList, secondList);
    

    You can extend this idea for different number of items.

提交回复
热议问题