Is there an interface that can be used as a parameter in a method for T[] (an array of type T) and for List<T>

huangapple go评论78阅读模式
英文:

Is there an interface that can be used as a parameter in a method for T[] (an array of type T) and for List<T>

问题

我有两种执行相同任务的方法:一个接收一个数组参数,另一个接收一个List参数,两者都是字符串类型的。有没有办法用一个单一的方法替代这两个方法?可以用什么类型的参数替代它们?

方法如下:

public static void NumberLinesInCollection(List<string> list, int startNumberingFromRowNumber = 0)
{
    int numberOfLines = list.Count;
    for (int i = startNumberingFromRowNumber; i < numberOfLines; i++)
    {
        string sourceString = (i + 1).ToString();
        string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(), 
                                                                fieldLength: numberOfLines.ToString().Length, 
                                                                position: PaddingDirection.left);
        list[i] = resultingString + ". " + list[i];
    }
}

public static void NumberLinesInCollection(string[] arrayOfStrings, int startNumberingFromRowNumber = 0)
{
    int numberOfLines = arrayOfStrings.Length;
    for (int i = startNumberingFromRowNumber; i < numberOfLines; i++)
    {
        string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(),
                                                                fieldLength: numberOfLines.ToString().Length,
                                                                position: PaddingDirection.left);
        arrayOfStrings[i] = resultingString + ". " + arrayOfStrings[i];
    }
}

谢谢您提前的帮助。

英文:

I have two methods that perform the same task: one receiving an array parameter and the a List parameter, both of type string.
Is there a way to replace these two methods by a single method? What type of parameter can replace both?
The methods are:

    public static void NumberLinesInCollection(List&lt;string&gt; list, int startNumberingFromRowNumber = 0)
    {
        int numberOfLines = list.Count;
        for (int i = startNumberingFromRowNumber; i &lt; numberOfLines; i++)
        {
            string sourceString = (i + 1).ToString();
            string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(), 
                                                                    fieldLength: numberOfLines.ToString().Length, 
                                                                    position: PaddingDirection.left);
            list[i] = resultingString + &quot;. &quot; + list[i];
        }
    }    

and

    public static void NumberLinesInCollection(string[] arrayOfStrings, int startNumberingFromRowNumber = 0)
    {
        int numberOfLines = arrayOfStrings.Length;
        for (int i = startNumberingFromRowNumber; i &lt; numberOfLines; i++)
        {
            string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(),
                                                                    fieldLength: numberOfLines.ToString().Length,
                                                                    position: PaddingDirection.left);
            arrayOfStrings[i] = resultingString + &quot;. &quot; + arrayOfStrings[i];
        }
    }    

Thank you in advance.

答案1

得分: 4

System.Array的文档中可以看到:

单维数组实现了IList<T>ICollection<T>IEnumerable<T>IReadOnlyList<T>IReadOnlyCollection<T>通用接口。这些实现在运行时提供给数组,因此在Array类的声明语法中不会出现通用接口。

List<T>的文档中可以看到:

实现了ICollection<T>IEnumerable<T>IList<T>IReadOnlyCollection<T>IReadOnlyList<T>[以及一些非通用接口]

正如我们所见,这里存在一个很好的重叠,允许您根据实际需要的数组/列表特性选择最合适的接口。

在您的特定情况下,IList<T>似乎是最适合的,因为它提供了您的代码中使用的以下两个功能:

  • 一个Item[]索引器
  • 一个从ICollection<T>继承而来的Count属性。
英文:

From the documentation of System.Array:

> Single-dimensional arrays implement the IList&lt;T&gt;, ICollection&lt;T&gt;, IEnumerable&lt;T&gt;, IReadOnlyList&lt;T&gt; and IReadOnlyCollection&lt;T&gt; generic interfaces. The implementations are provided to arrays at run time, and as a result, the generic interfaces do not appear in the declaration syntax for the Array class.

From the documentation of List&lt;T&gt;:

> Implements ICollection&lt;T&gt;, IEnumerable&lt;T&gt;, IList&lt;T&gt;, IReadOnlyCollection&lt;T&gt;, IReadOnlyList&lt;T&gt;, [and a few non-generic interfaces]

As we can see, there is a nice overlap, allowing you to choose the most fitting interface based on the actual array/list features you require.

In your particular case, IList&lt;T&gt; seems to be most suitable, since it provides both of the following features used in your code:

  • an Item[] indexer and
  • a Count property (inherited from ICollection&lt;T&gt;).

答案2

得分: 1

你可以使用IList&lt;string&gt;,这将允许您在调用NumberLinesInCollection时将List和Array都作为参数传递。数组和列表都实现了IList接口。

英文:

you can use IList&lt;string&gt;, this will allow you to pass both List and Array as the parameter while calling the NumberLinesInCollection. array and list both implement IList

答案3

得分: 1

你可以使用 IEnumerable,然后它可以是 listarray:

public static void NumberLinesInCollection(IEnumerable<string> list, int startNumberingFromRowNumber = 0)
{
}
英文:

You can use IEnumerable, then it can be either list and array:

public static void NumberLinesInCollection(IEnumerable&lt;string&gt; list, int startNumberingFromRowNumber = 0)
{
}

huangapple
  • 本文由 发表于 2023年1月9日 18:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定