首页 新闻 赞助 找找看

如何给泛型数组添加一个RemoveAt(index)的扩展方法?

1
悬赏园豆:10 [已解决问题] 解决于 2012-04-18 09:01

请问如何给泛型数组添加一个RemoveAt(index)的扩展方法,像List<T>.RemoveAt方法

草根程序猿的主页 草根程序猿 | 初学一级 | 园豆:129
提问于:2012-04-17 23:15
< >
分享
最佳答案
1
public static T[] RemoveAt<T>(this T[] source, int index)
{
    T[] dest = new T[source.Length - 1];
    if( index > 0 )
        Array.Copy(source, 0, dest, 0, index);

    if( index < source.Length - 1 )
        Array.Copy(source, index + 1, dest, index, source.Length - index - 1);

    return dest;
}

参考:Remove element of a regular array

收获园豆:10
artwl | 专家六级 |园豆:16736 | 2012-04-17 23:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册