我打算编写一种C#扩展方法,使其仅加入特定范围的字符串数组元素。例如,如果我有这个数组:
+-----+ +-----+ +-------+ +------+ +------+ +-----+
| one | | two | | three | | four | | five | | six |
+-----+ +-----+ +-------+ +------+ +------+ +-----+
0 1 2 3 4 5
而且我只想使用,索引2到索引4加入它们three,four,five
。如果用户不提供开始索引和结束索引,那么我的Join
方法将连接所有数组元素。下面是我的方法签名。
public static class StringSplitterJoinner
{
public static string Join(this string[] me, string separator, int start_index = 0, int end_index = me.Length - 1) {
}
}
问题在于该参数end_index
无法引用第一个参数,me
并且会生成错误。我不希望用户总是提供start_index
,end_index
我希望我的方法具有一些有意义的默认值。在这种情况下,如何解决这个问题?
这个怎么样:
public static class StringSplitterJoinner
{
public static string Join(this string[] me, string separator, int start_index = 0, int? end_index = null)
{
if (!end_index.HasValue) end_index = me.Length - 1;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句