精华内容
下载资源
问答
  • C#源码集合

    2013-05-30 10:55:09
    C#源码集合,你懂的!包你满意。 控件操作, 文件管理, 程序设置, 系统维护, 图像处理 ,数据库开发, 网络应用, Web应用,窗体开发,网络开发,线程知识。适合一切c#初学者!!
  • C#项目源码集合

    2017-11-11 14:28:53
    C#项目源码集合C#项目源码集合C#项目源码集合C#项目源码集合
  • CSC440FinalPortfolio 中级C#类别和实践的C#程序集合我曾经通过C#MCP考试。 项目分为两个部分: 管理程序流程 创建和使用类型
  • C#硬件开发源码集合

    2009-09-19 09:00:38
    C#硬件开发源码 包括: 语音卡、IC卡、神龙卡的使用  利用语音卡拨打电话  客户来电查询系统  向IC卡中写入数据  从IC卡中读取数据  利用IC卡制作考勤程序  利用神龙卡制作练歌房点歌程序  串口技术与加密狗...
  • C#项目源码集合系列一

    热门讨论 2016-04-07 23:13:42
    源码04 C#点名程序 源码05 象棋游戏 源码06 变色球游戏 源码07 多功能计算器 源码08 记事本 源码09 简易画图程序 源码10 成绩管理系统 源码11 BBS论坛系统 源码12 电话客服系统 源码13 仿QQ聊天系统 源码14 客房管理...
  • C#项目源码集合.zip

    2019-06-03 16:19:31
    资源非常大,分系列,其他系列详见我的资料 源码一 销售管理系统 源码二 彩票分析系统 源码三 餐饮管理系统 源码C#点名程序 源码五 象棋游戏 源码六 变色球游戏 源码七 多功能计算器 源码八 记事本 源码九 简易...
  • 源码04 C#点名程序 源码05 象棋游戏 源码06 变色球游戏 源码07 多功能计算器 源码08 记事本 源码09 简易画图程序 源码10 成绩管理系统 源码11 BBS论坛系统 源码12 电话客服系统 源码13 仿QQ聊天系统 源码14 客房管理...
  • 源码04 C#点名程序 源码05 象棋游戏 源码06 变色球游戏 源码07 多功能计算器 源码08 记事本 源码09 简易画图程序 源码10 成绩管理系统 源码11 BBS论坛系统 源码12 电话客服系统 源码13 仿QQ聊天系统 源码14 客房管理...
  • 把代码过程中经常用的内容做个珍藏,如下资料是关于C#转换集合类型为html输出的代码。public static string ToHTML(Hashtable hashtable) {string tmp = "";IDictionaryEnumerator myEnumerator = hashtable....

    把代码过程中经常用的内容做个珍藏,如下资料是关于C#转换集合类型为html输出的代码。
    public static string ToHTML(Hashtable hashtable) {
    string tmp = "";
    IDictionaryEnumerator myEnumerator = hashtable.GetEnumerator();
    while ( myEnumerator.MoveNext() ) {
    tmp += "<br />" + myEnumerator.Key+", "+ myEnumerator.Value;
    }
    return tmp;
    }
    public static string ToHTML(ArrayList arrayList) {
    foreach ( Object o in arrayList ) {
    tmp.Append("<br />" + o);
    }
    return tmp.ToString();
    }

    转载于:https://blog.51cto.com/14152780/2342463

    展开全文
  • c#官方线程安全集合源码,concurrentBag concurrentqueue,concurrentset,concurrentDictionary,concurrentSet等
  • C#程序集合

    2019-03-25 20:15:36
    里面有视频监控(客户端、服务器端)、广播通信、屏幕捕获、信息传输加密、音频、远程屏控等多个程序源码,比较基础,可作为进一步研究的参考。
  • 高质量C#源码.rar

    2020-02-28 10:48:55
    该资源是多达150个实例源码集合涵盖资源管理,序列化、线程并发、架构设计、安全性设计等等既有小型项目又有大型项目
  • C#泛型集合

    2019-10-09 10:00:50
    ***数组元素个数是固定的,数组一但定义,就无法改变元素总数,如果需求变化,就要修改源码。 ***如果初始化元素总数非常大,则会造成空间浪费。 集合的特点: ***根据需求动态增加元素个数,没有限制。 List<...

    数组的局限性:

    ***数组元素个数是固定的,数组一但定义,就无法改变元素总数,如果需求变化,就要修改源码。

    ***如果初始化元素总数非常大,则会造成空间浪费。

    集合的特点:

    ***根据需求动态增加元素个数,没有限制。

    List<T>泛型集合的特点:元素可以摆放无序

    ***<T>表示泛型,T是Type的缩写,表示当前不确定具体类型。

    ***可以根据用户的实际需要,确立当前集合需要存放的数据类型,一但确定不可改变。

    List<T>泛型集合的存储结构:

    泛型集合要求:

    ***使用泛型集合只能添加一种类型的数据,数据取出后无需强制转换。

    List<T>使用前的准备工作:

    ***引入命名空间using System.Collections.Generic;

    ***确定存储类型:list<Student> student = new List<Student>();

    常用方法:

    ***添加元素:Add(<T>);

    ***删除元素:RemoveAt(索引)

    常用属性:

    ***元素个数:Count

    遍历集合:

    foreach(Student stu in students)
    {
        //代码段
        Console.WriteLine(stu.StudentName);
    }

    例子:

    class Program
        {
            static void Main(string[] args)
            {
                //创建对象
                Student objStudent1 = new Student(1001,"joday");
                Student objStudent2 = new Student(1002, "lucy");
                Student objStudent3 = new Student(1003, "marry");
                Student objStudent4 = new Student(1004, "Alice");
                stuList.Add(new Student() 
                { 
                    StudentId = 10006,
                    StudentName = "xiaojing"
                    
                });//初始化器
    
                //集合初始化器            
                List<Student> nameList = new List<Student>(){objStu1,objStu2,objStu3};
    
                //创建集合对象
                List<Student> stuList = new List<Student>();
                stuList.Add(objStudent1);//一个一个往集合中添加元素
                stuList.Add(objStudent2);
                stuList.Add(objStudent3);
                stuList.Add(objStudent4);
                //以数组的形式往集合中添加元素,不用一个一个添加
                //stuList.AddRange()
    
                //获取元素的个数
                Console.WriteLine("元素总数:{0}",stuList.Count);
    
                //删除元素
                stuList.Remove(objStudent2);//按元素删除
                stuList.RemoveAt(0);//按元素的索引删除
    
                //插入一个对象
                stuList.Insert(0,new Student(1006,"but"));//在所引处添加一个元素
                Console.WriteLine(objStudent1.StudentName);//get设置
    
                //遍历
                foreach(Student i in stuList)
                {
                    Console.WriteLine(i.StudentName+"\t"+i.StudentId);
                }
                Console.ReadLine();
                
            
            
    
            }

    泛型集合Dictionary<K,V>:(存储基于哈希表)元素摆放有序,通过序号K找到对象V

    ***Dictionary<K,V>通常称为字典,<K,V>约束集合中元素的类型。

    ***编译时检查类型约束,无需装箱拆箱操作,与哈希表操作类似

    Dictionary<K,V>的存储结构:

    例子:

                Dictionary<string,Student> stus = new Dictionary<string,Student>();
                stus.Add("joday", objStudent1);
                stus.Add("lucy", objStudent2);
                //取出元素
                Console.WriteLine(stus["joday"].StudentId);
    
                foreach (Student value in stus.Values) //对值遍历,键、值一次遍历只能遍历一个
                {
                    Console.WriteLine(value.StudentName +"\t"+value.StudentId);
                }
    
                foreach(string key in stus.Keys)//对键进行遍历
                {
                    Console.WriteLine(key);
                }

     

    展开全文
  • 1、在C#中,不用去面对完成端口的操作系统内核对象,Microsoft已经为我们提供了SocketAsyncEventArgs类,它封装了IOCP的使用。请参考:...
  • List字典Dictionary泛型集合实例,源码
  • C#源码分析—ArrayList

    2019-11-11 15:09:53
    ArrayList的源码和List的源码基本相似,只不过List是泛型集合而ArrayList是Object,关于ArrayList和List的优缺点这里不再叙述,请自行百度。需要说明的一点List实现了IList<T>、IList和IReadOnlyList<T>...

    ArrayList的源码和List的源码基本相似,只不过List是泛型集合而ArrayList是Object,关于ArrayList和List的优缺点这里不再叙述,请自行百度。需要说明的一点List实现了IList<T>、IList和IReadOnlyList<T>接口,而ArrayList实现了IList和ICloneable接口。

    字段:

            private Object[] _items;                    //内部数组
            [ContractPublicPropertyName("Count")]
            private int _size;                          //存储的元素数量
            private int _version;                       //版本号
            [NonSerialized]
            private Object _syncRoot;                   //Lock锁相关
            
            private const int _defaultCapacity = 4;     //默认容量
            private static readonly Object[] emptyArray = EmptyArray<Object>.Value;     //默认空数组

    其中的emptyArray和List的稍微有些不同,指向了EmptyArray<Object>.Value,其实现也是一个长度为0的数组:

    // Useful in number of places that return an empty byte array to avoid unnecessary memory allocation.
    internal static class EmptyArray<T>
    {
        public static readonly T[] Value = new T[0];
    }

    构造函数:

            // Note: this constructor is a bogus constructor that does nothing
            // and is for use only with SyncArrayList.
            internal ArrayList( bool trash )
            {
            }
    
            // Constructs a ArrayList. The list is initially empty and has a capacity
            // of zero. Upon adding the first element to the list the capacity is
            // increased to _defaultCapacity, and then increased in multiples of two as required.
    #if !FEATURE_CORECLR
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    #endif
            public ArrayList() {
                _items = emptyArray;  
            }
        
            // Constructs a ArrayList with a given initial capacity. The list is
            // initially empty, but will have room for the given number of elements
            // before any reallocations are required.
            // 
             public ArrayList(int capacity) {
                 //容量不能小于0
                 if (capacity < 0) throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_MustBeNonNegNum", "capacity"));
                 Contract.EndContractBlock();
    
                 //给定的容量为0则指向空数组,否则new新数组
                 if (capacity == 0)
                     _items = emptyArray;
                 else
                     _items = new Object[capacity];
            }
        
            // Constructs a ArrayList, copying the contents of the given collection. The
            // size and capacity of the new list will both be equal to the size of the
            // given collection.
            // 
            public ArrayList(ICollection c) {
                if (c==null)
                    throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection"));
                Contract.EndContractBlock();
    
                int count = c.Count;
                if (count == 0)
                {
                    _items = emptyArray;
                }
                else {
                    _items = new Object[count];
                    AddRange(c);
                }
            }

    容量相关的函数:

            // Gets and sets the capacity of this list.  The capacity is the size of
            // the internal array used to hold items.  When set, the internal 
            // array of the list is reallocated to the given capacity.
            // 
             public virtual int Capacity {
                get {
                    Contract.Ensures(Contract.Result<int>() >= Count);
                    return _items.Length;
                }
                set {
                    //若设置的容量小于当前数量
                    if (value < _size) {
                        throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity"));
                    }
                    Contract.Ensures(Capacity >= 0);
                    Contract.EndContractBlock();
                    // We don't want to update the version number when we change the capacity.
                    // Some existing applications have dependency on this.
                    //设置的容量不等于内部数组大小时才扩容
                    if (value != _items.Length) {
                        //设置的容量只能大于等于存储的元素数量且不能等于内部数组的大小,其取值范围为[_size,_items.Length)和(_items.Length,∞]
                        //_size的取值范围[0,_items.Length]
                        if (value > 0) {
                            Object[] newItems = new Object[value];
                            if (_size > 0) { 
                                Array.Copy(_items, 0, newItems, 0, _size);
                            }
                            _items = newItems;
                        }
                        else {
                            _items = new Object[_defaultCapacity];
                        }
                    }            
                }
            }        
    
            // Ensures that the capacity of this list is at least the given minimum
            // value. If the currect capacity of the list is less than min, the
            // capacity is increased to twice the current capacity or to min,
            // whichever is larger.
            private void EnsureCapacity(int min) {
                if (_items.Length < min) {
                    //除第一次扩容外,其他扩容时会扩容为原来的2倍
                    int newCapacity = _items.Length == 0? _defaultCapacity: _items.Length * 2;
                    // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
                    // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
                    //可以看到List的大小是有极限的
                    if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
                    //判断计算出的容量是否使用
                    if (newCapacity < min) newCapacity = min;
                    Capacity = newCapacity;
                }
            }
    
            // Sets the capacity of this list to the size of the list. This method can
            // be used to minimize a list's memory overhead once it is known that no
            // new elements will be added to the list. To completely clear a list and
            // release all memory referenced by the list, execute the following
            // statements:
            // 
            // list.Clear();
            // list.TrimToSize();
            // 
            public virtual void TrimToSize() {
                Capacity = _size;
            }

    和List一样的实现。

    Add函数:

            // Adds the given object to the end of this list. The size of the list is
            // increased by one. If required, the capacity of the list is doubled
            // before adding the new element.
            //
            public virtual int Add(Object value) {
                Contract.Ensures(Contract.Result<int>() >= 0);
                if (_size == _items.Length) EnsureCapacity(_size + 1);
                _items[_size] = value;
                _version++;
                return _size++;
            }

    Clone函数:

            // Clones this ArrayList, doing a shallow copy.  (A copy is made of all
            // Object references in the ArrayList, but the Objects pointed to 
            // are not cloned).
            public virtual Object Clone()
            {
                Contract.Ensures(Contract.Result<Object>() != null);
                ArrayList la = new ArrayList(_size);
                la._size = _size;
                la._version = _version;
                Array.Copy(_items, 0, la._items, 0, _size);
                return la;
            }

    Clone是实现ICloneable的函数,函数内部通过new出一个新的ArrayList,同时把所有的数据复制了一份,并更新了存储的元素数量和版本号。

    Clear函数:

            // Clears the contents of ArrayList.
            public virtual void Clear() {
                if (_size > 0)
                {
                Array.Clear(_items, 0, _size); // Don't need to doc this but we clear the elements so that the gc can reclaim the references.
                _size = 0;
                }
                _version++;
            }

    如果存储的元素数量大于0,调用Array.Clear()清空数据,同时归零_size。

    Contains函数:

            // Contains returns true if the specified element is in the ArrayList.
            // It does a linear, O(n) search.  Equality is determined by calling
            // item.Equals().
            //
            public virtual bool Contains(Object item) {
                if (item==null) {
                    for(int i=0; i<_size; i++)
                        if (_items[i]==null)
                            return true;
                    return false;
                }
                else {
                    for(int i=0; i<_size; i++)
                        if ( (_items[i] != null) && (_items[i].Equals(item)) )
                            return true;
                    return false;
                }
            }

    Contains函数和List的实现一样,通过for循环判断,同样会判断null,说明ArraList同样可以存入null。

    CopyTo函数:

            // Copies this ArrayList into array, which must be of a 
            // compatible array type.  
            //
            public virtual void CopyTo(Array array) {
                CopyTo(array, 0);
            }
    
            // Copies this ArrayList into array, which must be of a 
            // compatible array type.  
            //
            public virtual void CopyTo(Array array, int arrayIndex) {
                if ((array != null) && (array.Rank != 1))
                    throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
                Contract.EndContractBlock();
                // Delegate rest of error checking to Array.Copy.
                Array.Copy(_items, 0, array, arrayIndex, _size);
            }
        
            // Copies a section of this list to the given array at the given index.
            // 
            // The method uses the Array.Copy method to copy the elements.
            // 
            public virtual void CopyTo(int index, Array array, int arrayIndex, int count) {
                if (_size - index < count)
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
                if ((array != null) && (array.Rank != 1))
                    throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
                Contract.EndContractBlock();
                // Delegate rest of error checking to Array.Copy.
                Array.Copy(_items, index, array, arrayIndex, count);
            }

    和Listyi一样最后调用Array.Copy函数进行实现。

    IndexOf函数:

            // Returns the index of the first occurrence of a given value in a range of
            // this list. The list is searched forwards from beginning to end.
            // The elements of the list are compared to the given value using the
            // Object.Equals method.
            // 
            // This method uses the Array.IndexOf method to perform the
            // search.
            // 
    #if !FEATURE_CORECLR
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    #endif
            public virtual int IndexOf(Object value) {
                Contract.Ensures(Contract.Result<int>() < Count);
                return Array.IndexOf((Array)_items, value, 0, _size);
            }
        
            // Returns the index of the first occurrence of a given value in a range of
            // this list. The list is searched forwards, starting at index
            // startIndex and ending at count number of elements. The
            // elements of the list are compared to the given value using the
            // Object.Equals method.
            // 
            // This method uses the Array.IndexOf method to perform the
            // search.
            // 
            public virtual int IndexOf(Object value, int startIndex) {
                if (startIndex > _size)
                    throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
                Contract.Ensures(Contract.Result<int>() < Count);
                Contract.EndContractBlock();
                return Array.IndexOf((Array)_items, value, startIndex, _size - startIndex);
            }
    
            // Returns the index of the first occurrence of a given value in a range of
            // this list. The list is searched forwards, starting at index
            // startIndex and upto count number of elements. The
            // elements of the list are compared to the given value using the
            // Object.Equals method.
            // 
            // This method uses the Array.IndexOf method to perform the
            // search.
            // 
            public virtual int IndexOf(Object value, int startIndex, int count) {
                if (startIndex > _size)
                    throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
                if (count <0 || startIndex > _size - count) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
                Contract.Ensures(Contract.Result<int>() < Count);
                Contract.EndContractBlock();
                return Array.IndexOf((Array)_items, value, startIndex, count);
            }

    最终调用Array.IndexOf函数,其函数实现如下:

            // Returns the index of the first occurrence of a given value in a range of
            // an array. The array is searched forwards, starting at index
            // startIndex and upto count elements. The
            // elements of the array are compared to the given value using the
            // Object.Equals method.
            // 
            [System.Security.SecuritySafeCritical]  // auto-generated
            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
            public static int IndexOf(Array array, Object value, int startIndex, int count) {
                if (array==null)
                    throw new ArgumentNullException("array");
                if (array.Rank != 1)
                    throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
                Contract.Ensures(Contract.Result<int>() < array.GetLowerBound(0) + array.Length);
                Contract.EndContractBlock();
    
                int lb = array.GetLowerBound(0);
                if (startIndex < lb || startIndex > array.Length + lb)
                    throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
                if (count < 0 || count > array.Length - startIndex + lb)
                    throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
    
                // Try calling a quick native method to handle primitive types.
                int retVal;
                bool r = TrySZIndexOf(array, startIndex, count, value, out retVal);
                if (r)
                    return retVal;
    
                Object[] objArray = array as Object[];
                int endIndex = startIndex + count;
                
                if (objArray != null) {
                    if (value == null) {
                        for (int i = startIndex; i < endIndex; i++) {
                            if (objArray[i] == null) return i;
                        }
                    }
                    else {
                        for (int i = startIndex; i < endIndex; i++) {
                            Object obj = objArray[i];
                            if (obj != null && obj.Equals(value)) return i;
                        }
                    }
                }
                else {                
                    for (int i = startIndex; i < endIndex; i++) {
                        Object obj = array.GetValue(i);
                        if( obj == null) {
                            if(value == null) return i;
                        }
                        else {
                            if( obj.Equals(value)) return i;
                        }
                    }
                }
                // Return one less than the lower bound of the array.  This way,
                // for arrays with a lower bound of -1 we will not return -1 when the
                // item was not found.  And for SZArrays (the vast majority), -1 still
                // works for them.
                return lb-1;
            }

    Array.IndexOf函数会尝试将参数array转换为Object[],无论成功与否,都会进行参数value是否为null的判断,然后for循环判断出结果。

    Insert函数:

            // Inserts an element into this list at a given index. The size of the list
            // is increased by one. If required, the capacity of the list is doubled
            // before inserting the new element.
            // 
            public virtual void Insert(int index, Object value) {
                // Note that insertions at the end are legal.
                if (index < 0 || index > _size) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_ArrayListInsert"));
                //Contract.Ensures(Count == Contract.OldValue(Count) + 1);
                Contract.EndContractBlock();
    
                if (_size == _items.Length) EnsureCapacity(_size + 1);
                //插入到内部数组头部或中间,尾部是合法的
                if (index < _size) {
                    Array.Copy(_items, index, _items, index + 1, _size - index);
                }
                _items[index] = value;
                _size++;
                _version++;
            }
        
            // Inserts the elements of the given collection at a given index. If
            // required, the capacity of the list is increased to twice the previous
            // capacity or the new size, whichever is larger.  Ranges may be added
            // to the end of the list by setting index to the ArrayList's size.
            //
            public virtual void InsertRange(int index, ICollection c) {
                if (c==null)
                    throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection"));
                if (index < 0 || index > _size) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
                //Contract.Ensures(Count == Contract.OldValue(Count) + c.Count);
                Contract.EndContractBlock();
    
                int count = c.Count;
                if (count > 0) {
                    //进行扩容,接收数据
                    EnsureCapacity(_size + count);
                    // shift existing items
                    //插入到内部数组头部或中间,尾部是合法的
                    if (index < _size) {
                        Array.Copy(_items, index, _items, index + count, _size - index);
                    }
    
                    Object[] itemsToInsert = new Object[count];
                    c.CopyTo(itemsToInsert, 0);
                    itemsToInsert.CopyTo(_items, index);
                    _size += count;
                    _version++;
                }
            }

    LastIndexOf函数:

            // Returns the index of the last occurrence of a given value in a range of
            // this list. The list is searched backwards, starting at the end 
            // and ending at the first element in the list. The elements of the list 
            // are compared to the given value using the Object.Equals method.
            // 
            // This method uses the Array.LastIndexOf method to perform the
            // search.
            // 
            public virtual int LastIndexOf(Object value)
            {
                Contract.Ensures(Contract.Result<int>() < _size);
                return LastIndexOf(value, _size - 1, _size);
            }
    
            // Returns the index of the last occurrence of a given value in a range of
            // this list. The list is searched backwards, starting at index
            // startIndex and ending at the first element in the list. The 
            // elements of the list are compared to the given value using the 
            // Object.Equals method.
            // 
            // This method uses the Array.LastIndexOf method to perform the
            // search.
            // 
            public virtual int LastIndexOf(Object value, int startIndex)
            {
                if (startIndex >= _size)
                    throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
                Contract.Ensures(Contract.Result<int>() < Count);
                Contract.EndContractBlock();
                return LastIndexOf(value, startIndex, startIndex + 1);
            }
    
            // Returns the index of the last occurrence of a given value in a range of
            // this list. The list is searched backwards, starting at index
            // startIndex and upto count elements. The elements of
            // the list are compared to the given value using the Object.Equals
            // method.
            // 
            // This method uses the Array.LastIndexOf method to perform the
            // search.
            // 
            public virtual int LastIndexOf(Object value, int startIndex, int count) {
                if (Count != 0 && (startIndex < 0 || count < 0))
                    throw new ArgumentOutOfRangeException((startIndex<0 ? "startIndex" : "count"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                Contract.Ensures(Contract.Result<int>() < Count);
                Contract.EndContractBlock();
    
                if (_size == 0)  // Special case for an empty list
                    return -1;
    
                if (startIndex >= _size || count > startIndex + 1) 
                    throw new ArgumentOutOfRangeException((startIndex>=_size ? "startIndex" : "count"), Environment.GetResourceString("ArgumentOutOfRange_BiggerThanCollection"));
    
                return Array.LastIndexOf((Array)_items, value, startIndex, count);
            }

    和List一样最终调用Array.LastIndexOf函数,其实现如下:

            // Returns the index of the last occurrence of a given value in a range of
            // an array. The array is searched backwards, starting at index
            // startIndex and counting uptocount elements. The elements of
            // the array are compared to the given value using the Object.Equals
            // method.
            // 
            [System.Security.SecuritySafeCritical]  // auto-generated
            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
            public static int LastIndexOf(Array array, Object value, int startIndex, int count) {
                if (array==null)
                    throw new ArgumentNullException("array");
                Contract.Ensures(Contract.Result<int>() < array.GetLowerBound(0) + array.Length);
                Contract.EndContractBlock();
                int lb = array.GetLowerBound(0);
                if (array.Length == 0) {
                    return lb-1;
                }
    
                if (startIndex < lb || startIndex >= array.Length + lb)
                    throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
                if (count < 0)
                    throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
                if (count > startIndex - lb + 1)
                    throw new ArgumentOutOfRangeException("endIndex", Environment.GetResourceString("ArgumentOutOfRange_EndIndexStartIndex"));
                if (array.Rank != 1)
                    throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
    
                // Try calling a quick native method to handle primitive types.
                int retVal;
                bool r = TrySZLastIndexOf(array, startIndex, count, value, out retVal);
                if (r)
                    return retVal;
    
                Object[] objArray = array as Object[];
                int endIndex = startIndex - count + 1;
                if (objArray!=null) {
                    if (value == null) {
                        for (int i = startIndex; i >= endIndex; i--) {
                            if (objArray[i] == null) return i;
                        }
                    }
                    else {
                        for (int i = startIndex; i >= endIndex; i--) {
                            Object obj = objArray[i];
                            if (obj != null && obj.Equals(value)) return i;
                        }
                    }
                }
                else {
                    for (int i = startIndex; i >= endIndex; i--) {
                        Object obj = array.GetValue(i);
                        if( obj == null) {
                            if(value == null) return i;
                        }
                        else {
                            if( obj.Equals(value)) return i;
                        }
                    }
                }
                return lb-1;  // Return lb-1 for arrays with negative lower bounds.
            }

    和Array.IndexOf实现相同,只不过否循环是从后向前。

    Remove函数:

    // Removes the element at the given index. The size of the list is
            // decreased by one.
            // 
    #if !FEATURE_CORECLR
            [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    #endif
            //内部调用IndexOf查找索引位置,通过RemoveAt移除,所以在知道索引的清空下,尽量使用RemoveAt
            public virtual void Remove(Object obj) {
                Contract.Ensures(Count >= 0);
    
                int index = IndexOf(obj);
                BCLDebug.Correctness(index >= 0 || !(obj is Int32), "You passed an Int32 to Remove that wasn't in the ArrayList." + Environment.NewLine + "Did you mean RemoveAt?  int: "+obj+"  Count: "+Count);
                if (index >=0) 
                    RemoveAt(index);
            }
        
            // Removes the element at the given index. The size of the list is
            // decreased by one.
            // 
            public virtual void RemoveAt(int index) {
                if (index < 0 || index >= _size) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
                Contract.Ensures(Count >= 0);
                //Contract.Ensures(Count == Contract.OldValue(Count) - 1);
                Contract.EndContractBlock();
    
                _size--;
                if (index < _size) {
                    //将不需要移除的元素向上移动,注意此时的_size已经更新过
                    Array.Copy(_items, index + 1, _items, index, _size - index);
                }
                _items[_size] = null;
                _version++;
            }
        
            // Removes a range of elements from this list.
            // 
            public virtual void RemoveRange(int index, int count) {
                if (index < 0)
                    throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                if (count < 0)
                    throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                if (_size - index < count)
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
                Contract.Ensures(Count >= 0);
                //Contract.Ensures(Count == Contract.OldValue(Count) - count);
                Contract.EndContractBlock();
        
                if (count > 0) {
                    int i = _size;
                    _size -= count;
                    if (index < _size) {
                        //将不需要移除的元素向上移动,注意此时的_size已经更新过
                        Array.Copy(_items, index + count, _items, index, _size - index);
                    }
                    while (i > _size) _items[--i] = null;
                    _version++;
                }
            }

    Reverse函数:

            // Reverses the elements in this list.
            public virtual void Reverse() {
                Reverse(0, Count);
            }
        
            // Reverses the elements in a range of this list. Following a call to this
            // method, an element in the range given by index and count
            // which was previously located at index i will now be located at
            // index index + (index + count - i - 1).
            // 
            // This method uses the Array.Reverse method to reverse the
            // elements.
            // 
            public virtual void Reverse(int index, int count) {
                if (index < 0)
                    throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                if (count < 0)
                    throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                if (_size - index < count)
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
                Contract.EndContractBlock();
                Array.Reverse(_items, index, count);
                _version++;
            }

    调用Array.Reverse函数,其实现如下:

            // Reverses the elements in a range of an array. Following a call to this
            // method, an element in the range given by index and count
            // which was previously located at index i will now be located at
            // index index + (index + count - i - 1).
            // Reliability note: This may fail because it may have to box objects.
            // 
            [System.Security.SecuritySafeCritical]  // auto-generated
            [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
            public static void Reverse(Array array, int index, int length) {
                if (array==null) 
                    throw new ArgumentNullException("array");
                if (index < array.GetLowerBound(0) || length < 0)
                    throw new ArgumentOutOfRangeException((index<0 ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                if (array.Length - (index - array.GetLowerBound(0)) < length)
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
                if (array.Rank != 1)
                    throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
                Contract.EndContractBlock();
    
                bool r = TrySZReverse(array, index, length);
                if (r)
                    return;
    
                int i = index;
                //注意length为反转的数量
                int j = index + length - 1;
                Object[] objArray = array as Object[];
                if (objArray!=null) {
                    while (i < j) {
                        Object temp = objArray[i];
                        objArray[i] = objArray[j];
                        objArray[j] = temp;
                        i++;
                        j--;
                    }
                }
                else {
                    while (i < j) {
                        Object temp = array.GetValue(i);
                        array.SetValue(array.GetValue(j), i);
                        array.SetValue(temp, j);
                        i++;
                        j--;
                    }
                }
            }

    Sort函数:

            // Sorts the elements in this list.  Uses the default comparer and 
            // Array.Sort.
            public virtual void Sort()
            {
                Sort(0, Count, Comparer.Default);
            }
    
            // Sorts the elements in this list.  Uses Array.Sort with the
            // provided comparer.
            public virtual void Sort(IComparer comparer)
            {
                Sort(0, Count, comparer);
            }
    
            // Sorts the elements in a section of this list. The sort compares the
            // elements to each other using the given IComparer interface. If
            // comparer is null, the elements are compared to each other using
            // the IComparable interface, which in that case must be implemented by all
            // elements of the list.
            // 
            // This method uses the Array.Sort method to sort the elements.
            // 
            public virtual void Sort(int index, int count, IComparer comparer) {
                if (index < 0)
                    throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                if (count < 0)
                    throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
                if (_size - index < count)
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
                Contract.EndContractBlock();
                
                Array.Sort(_items, index, count, comparer);
                _version++;
            }

    调用Array.Sort函数。

     

    源码为.net 4.5.1;

    .net源码网址:https://referencesource.microsoft.com/

    注:本人水平有限,如果有错误,欢迎指正……

    展开全文
  • C# 字形轮廓提取--亲测可用,winform 项目,输入文本内容可直接转换为字体轮廓(图形集合及预览图)
  • 超强c#常用类库源码 各种工具函数集合 可直接调用开发软件网站 资源包括: 1、sql日常操作类库 access操作类库 传递sql语句即可调用 2、文件处理函数 增删减移动文件修改 3、Ftp常用函数 4、网络操作函数 html辅助...
  • C#集合

    2014-12-27 20:21:11
    欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频、U3D教程、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌  集合  集合,表示可以通过遍历...

    欢迎来到unity学习unity培训、unity企业培训教育专区,这里有很多U3D资源U3D培训视频U3D教程U3D常见问题U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌   

    集合

      集合,表示可以通过遍历每个元素来访问的一组对象(特别是可使用foreach循环访问)
    一个集合包括多个元素,即有一个集合类对象和N个元素对象

           因为任何集合类都实现了IEnumerable接口,所以任何集合类对象都有一个GetEnumerator()方法,该方法可以返回一个实现了 IEnumerator接口的对象,这个返回的IEnumerator对象既不是集合类对象,也不是集合的元素类对象,它是一个独立的类对象。通过这个对象,可以遍历访问集合类对象中的每一个元素对象

          如果集合类是用户自定义的集合类,则用户必须实现它的GetEnumerator()方法,否则不能使用循环。当然,与这个自定义集合类对应的IEnumerator类(实现了该接口的类),也要自定义一个才行

    比如,ArrayList集合类对应的IEnumerator是 ArrayListEnumeratorSimple
                Array集合类对应的IEnumerator是 SZArrayEnumerator
          (这两个类在.net framework类库文档(msdn)中都没有介绍)

    1.System.Colloctions中表示集合的行为的接口有:
    1)ICollection
         定义所有集合的大小、枚举数和同步方法。派生于IEnumerable
         它定义了集合类最基本的行为,所有的集合类都实现了这个接口(基接口)
         但是它的行为太过基本:主要就是一个Count属性,单独实现它没有太大意义

    2)IEnumerable
         公开枚举数,该枚举数支持在集合上进行简单迭代
         它只有一个方法 GetEnumerator(),该方法可以返回一个IEnumerator接口,通过它可以遍历集合
        基本上所有的集合类都实现了这个接口

    3)IList
          IList实现是可排序且可按照索引访问其成员的值的集合,它本身实现了ICollection和IEnumerable接口
          是所有列表的抽象基类。IList 实现有三种类别:只读、固定大小、可变大小。

    4)IDictionary
          IDictionary实现是键/值对的集合,它本身实现了ICollection和IEnumerable接口
          是键/值对的集合的基接口。IDictionary 实现有三种类别:只读、固定大小、可变大小。
    IDictionary可称为字典、映射或散列表,它根据键(任意类型)来访问值

    2.System.Collections中可以直接使用的集合类有:
    1)ArrayList
          实现了接口:IList、ICollection、IEnumerable
          只要集合未被修改,ArrayList 就可安全地同时支持多个读取器
          随着向 ArrayList 中添加元素,容量通过重新分配按需自动增加(2倍增加)
          如果需要建立一个对象数组,但不能预先知道数组的大小,就可以使用ArrayList
          ArrayList把所有元素都当作object对象引用,因而在访问ArrayList的元素时要进行类型转换
          优点:动态改变大小、灵活方便的插入和删除元素、可排序
          缺点:插入时性能不如数组、不是强类型的

    2)BitArray
          实现了接口:ICollection、IEnumerable
          管理位值的压缩数组。

    3)Hashtable
          实现了接口:IDictionary、ICollection、IEnumerable
          可以向Hashtable中自由添加和删除元素,有些像ArrayList,但没有那么大的性能开销

    4)SortedList
          实现了接口:IDictionary、ICollection、IEnumerable
          SortedLIst兼顾了ArrayList和Hashtable的优点,可按键值来排序

    5)Queue
          实现了接口:ICollection、IEnumerable
          Queque是队列,先进先出的访问各个元素
          可以调用Queque对象的GetEnumerator()方法,得到IEnumerator对象,来遍历队列中的各个元素

    6)Stack
           实现了接口:ICollection、IEnumerable
           Stack是堆栈,后进先出的访问各个元素
          可以调用Stack对象的GetEnumerator()方法,得到IEnumerator对象,来遍历堆栈中的各个元素

    3.上面提到的几种集合类,他们都是通用的集合类,他们所接受的元素大都是Object类型,当对象放入
           了集合之后,都失去了原有的类型信息-即这些通用集合类都不是强类型的
           解决办法是使用强类型的集合类
           System.Collections命名空间下的CollectionBase,DictionaryBase,ReadOnlyCollectionBase 类
           System.Collections.Specialized命名空间下的一些类可以满足要求,可以直接使用也可以继承

    想了解更多到http://www.gopedu.com

    展开全文
  • 最小二乘法C#源码

    千次阅读 2016-06-01 09:09:51
    #region 最小二乘法拟合//////用最小二乘法拟合二元多次曲线///例如y=ax+b///其中MultiLine将返回a,b两个参数。... name="arrX">已知点的x坐标集合/// name="arrY">已知点的y坐标集合/// name="leng
  • 实用程序C#扩展方法的集合。 主要: 开发人员: 主要: 开发人员: nuget: Install-Package rm.Extensions 字符串扩展名: var s = " " ; if ( s . IsNullOrEmpty ()) { /**/ } if ( s . IsNullOrWhiteSpace ...
  • C#计算器源码

    2016-05-04 17:15:30
    最近完成C#计算器编程,模仿WIN7...程序运用了C#的基本功能,包括接口、绑定、面向对象、事件、字段封装、多种循环、集合操作、操作符重载、string的多种操作等,同时实现先乘除后加减的功能,但还没实现带括号的计算。
  • # 前言此些天来,武疫兴然,吾甚惧焉,居家不出,项目做完,游戏玩腻,百无聊赖,猛然惊醒,吾当面试,基础犹菜,从何下手,唯有C#C#之何,集合为多,多而不晓,遂有此文。# 环境.Net Framework 4.8# 前置知识## ...
  • Windows应用开发最佳实践:Windows App和C#开发人员的最佳实践集合
  • 使用C#LINQ样式查询和Java 8闭包轻松操作集合。 描述 Android LINQ是受Microsoft C#LINQ库启发的集合操作实用程序的一小部分,面向希望使用新Java 8 Stream()API的Android开发人员。 通过将 ,开发人员可以...
  • C# ArrayList源码剖析

    千次阅读 2016-04-19 15:29:38
    数组是C#中最基础的一种数据类型,一旦初始化之后,容量便已经确定。若想要动态扩充容量,那么集合可以满足这点需求。ArrayList是C#最常用也是最基础的一个动态数组。ArrayList在System.Collections中,实现了IList...
  • bccrypto加解密算法库C#版本源码,包含IDEA算法集合 适合在C#中用来替代openssl
  • c# 集合相关的东西

    2017-12-25 14:32:00
    c# ArryList 源码分析 c# 实现 单链表(Node) 最近研究集合相关的东西; 1.List 和Dictionry 遍历的比较; 同样是集合,为什么性能会有这样的差距。我们要从存储结构和操作系统的原理谈起。 首先我们清楚List&...

空空如也

空空如也

1 2 3 4 5 ... 15
收藏数 285
精华内容 114
关键字:

c#源码集合

c# 订阅