C#数组复制操作

这玩意真是难受,随便搞一下就云里雾里的……

本来系统里是有个Array.CopyTo方法的,但我不喜欢用,不合我的要求。

于是我就自己写,写着写着就绕里面了,这会儿刚绕出来,赶紧记一下。

(很杀笔的注释往往有奇效偷笑

    public static class HenShaBiDeArrayCopyTool
    {
        /// <summary>
        /// 小数组放进大数组里,从大数组的指定位置开始放小数组,小数组是从第0个开始取的。
        /// 如果小数组没取完大数组到头了就不要再放了
        /// </summary>
        /// <param name="small">小数组</param>
        /// <param name="large">大数组</param>
        /// <param name="index">从大数组的指定位置开始放小数组的那个指定位置</param>
        public static void WriteTo(this byte[] small, byte[] large, long index)
        {
            if (index >= large.LongLength)
            {
                throw new ArgumentOutOfRangeException("index out of large range");
            }
            long slen = small.LongLength;
            long llen = large.LongLength;
            for (long i = index, j = 0; i < llen && j < slen; i++, j++)
            {
                large[i] = small[j];
            }
        }

        /// <summary>
        /// 大数组放进小数组里,从大数组的指定位置开始取数据放到小数组里,小数组是从第0个开始放大数组的。
        /// 如果大数组到头了就不要再取了,如果小数组到头了就不要再放了
        /// </summary>
        /// <param name="large">大数组</param>
        /// <param name="small">小数组</param>
        /// <param name="index">从大数组的指定位置开始取数据放到小数组里的那个指定位置</param>
        public static void CopyTo(this byte[] large, byte[] small, long index)
        {
            if (index >= large.LongLength)
            {
                throw new ArgumentOutOfRangeException("index out of large range");
            }
            long slen = small.LongLength;
            long llen = large.LongLength;
            for (long i = index, j = 0; i < llen && j < slen; i++, j++)
            {
                small[j] = large[i];
            }
        }
    }


我觉得应该是原创,毕竟这么简单的东西……


第二个拓展方法和.Net里的重复了,改成这样:

        /// <summary>
        /// 大数组放进小数组里,从大数组的指定位置开始取数据放到小数组里,小数组是从第0个开始放大数组的。
        /// 如果大数组到头了就不要再取了,如果小数组到头了就不要再放了
        /// </summary>
        /// <param name="small">小数组</param>
        /// <param name="large">大数组</param>
        /// <param name="index">从大数组的指定位置开始取数据放到小数组里的那个指定位置</param>
        public static void CopyFrom(this Array small, Array large, long index)
        {
            if (index >= large.LongLength)
            {
                throw new ArgumentOutOfRangeException("index out of large range");
            }
            long slen = small.LongLength;
            long llen = large.LongLength;
            for (long i = index, j = 0; i < llen && j < slen; i++, j++)
            {
                object largedata = large.GetValue(i);
                small.SetValue(largedata, j);
            }
        }


last edited at 2016/12/02 15:40

如今再看,发现自己已经不是过度封装这么简单了。。。

//
// Summary:
//     Copies all the elements of the current one-dimensional array to the specified
//     one-dimensional array starting at the specified destination array index.
//     The index is specified as a 32-bit integer.
//
// Parameters:
//   array:
//     The one-dimensional array that is the destination of the elements copied
//     from the current array.
//
//   index:
//     A 32-bit integer that represents the index in array at which copying begins.
//
public void CopyTo(Array array, int index);

//
// Summary:
//     Copies a range of elements from an System.Array starting at the first element
//     and pastes them into another System.Array starting at the first element.
//     The length is specified as a 32-bit integer.
//
// Parameters:
//   sourceArray:
//     The System.Array that contains the data to copy.
//
//   destinationArray:
//     The System.Array that receives the data.
//
//   length:
//     A 32-bit integer that represents the number of elements to copy.
//
public static void Copy(Array sourceArray, Array destinationArray, int length);

//
// Summary:
//     Copies a range of elements from an System.Array starting at the specified
//     source index and pastes them to another System.Array starting at the specified
//     destination index. The length and the indexes are specified as 32-bit integers.
//
// Parameters:
//   sourceArray:
//     The System.Array that contains the data to copy.
//
//   sourceIndex:
//     A 32-bit integer that represents the index in the sourceArray at which copying
//     begins.
//
//   destinationArray:
//     The System.Array that receives the data.
//
//   destinationIndex:
//     A 32-bit integer that represents the index in the destinationArray at which
//     storing begins.
//
//   length:
//     A 32-bit integer that represents the number of elements to copy.
//
public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);

作为一个新手一定要多看官方文档,对提高很有帮助,而且如果是国外做出的东西就更要强迫英语水平不太好的自己去看英文文档。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值