


1 indexer
[]The declared variable must be of fixed length, that is, the length is static; object[] objectArray = new object[10] ;
objectArray is shallow copy, that is, only assign an address value to it in memory. At this time, each item is a null reference;
Application examples
AdjustablePanel[] adjustPanelArrays = new AdjustablePanel[12]; foreach (Control ultraControl in this.Controls) { if (ultraControl.GetType() == typeof(UltraGrid) || ultraControl.GetType() == typeof(UltraChart) || ultraControl.GetType() == typeof(Panel)) { //adjustPanelArrays[index]此时为null,因此会出现null引用bug adjustPanelArrays[index].Controls.Add(ultraControl); } }
2 Array
Provides Create, Operation, Search and Sort methods of arrays and thus serves as the base class for all arrays in the common language runtime. The length is fixed and cannot be dynamically increased on demand; Array is an abstract class and cannot be created using new Array; GetValue returns the object type.
Array myArray = Array.CreateInstance(typeof(int),3); myArray.SetValue(1,0); myArray.SetValue(2,1); myArray.SetValue(3,2); //GetValue返回的是object类型,需要进行类型提升为int int val2 = (int)myArray.GetValue(2);
3 ArrayList
Implement the IList interface using an array whose size can be dynamically increased on demand, and is for any type.
ArrayList al = new ArrayList(); ArrayList arrayList = new ArrayList(); al.Add("qaz"); al.Add(1); al.Add(new List<object>()); string str = (string)al[0]; int intval = (int)al[1]; List<object> objs = (List<object>)al[2];
Summary
[], The length of Array needs to be known before compilation, it is static, and the type needs to be uniquely determined. Array is an abstract class, and Array is required to create it. CreateInstance();
The length of ArrayList is unknown at compile time, it is dynamic, and the added elements can be of different types.
4 List-APIs
4-1 Introduction
List is a generic class that implements the interface IList, The external interface is displayed by using a dynamically adjusted array internally.
4-2 Add elements
Implement adding an element
Add(obj)
Add elements to the list in batches:
AddRange(objList)
Example:
private List<int> intList = new List<int>(); public void AddApi() { intList.Add(10); //添加1个元素 intList.AddRange(new List<int>() { 5, 1, 1, 2, 2, 3 }); //批量添加元素 }
Insert an element in the collection at the specified index
void Insert(int index, T item);
void InsertRange(int index, IEnumerable《T》 collection)
4-3 Remove the element
Assume that intList is a List type, and the initial value is {10,5,1,1, 2,2,3}. Execution:
intList.Remove(1);
Remove the first occurrence of a specific object from the intList. After removing element 1, intList = {10,5,1,2,2,3};
Remove a certain range of elements:
intList.RemoveRange(0, 2);
intList = {2,2,3 };
After removing all duplicate elements: intList = {3};
intList.RemoveAll(removeDuplicateElements); intList.RemoveAll(i => { List<int> elementList = intList.FindAll(r => r.Equals(i)); if (elementList != null && elementList.Count > 1) return true; return false; });
When judging whether an element exists above, such as when removing an element, equality needs to be used Comparators. If type T implements the IEquatableLet’s look at an example of implementing an interface that is not the default comparator: public class MyObject
{ public int Value { get; set; }
public MyObject(int value)
{ this.Value = value;
}
} //实现接口IEquatable<MyObject>
public class MyObjectCollection : IEquatable<MyObject>
{ private List<MyObject> _myObjects = new List<MyObject>()
{ new MyObject(3),
new MyObject(4),
new MyObject(3),
new MyObject(2),
new MyObject(3)
}; //删除所有重复的元素
public void RemoveDuplicates()
{
_myObjects.RemoveAll(Equals);
} public List<MyObject> MyObjects
{ get
{ return _myObjects;
}
}
public bool Equals(MyObject other)
{
MyObject duplicate = _myObjects.Find(r => r.Value == other.Value);
if (duplicate != null && duplicate!=other)
return true;
return false;
}
}
Equals(object) is implemented here, but Remove(test) fails temporarily, and we will find out the reason later.
4-4 Find elements
Determine whether an element is in the List.
bool Contains(obj)
Determines whether it contains elements that match the conditions defined by the specified predicate.
bool Exists(Predicate<T> match)
Search for elements that match the conditions defined by the specified predicate and return the first matching element.
T Find(Predicate<T> match)
Retrieves all elements that match the conditions defined by the specified predicate.
List<T> FindAll(Predicate<T> match)
Search for elements that match the conditions defined by the specified predicate and return the zero-based index of the first matching element
int FindIndex(Predicate<T> match)
Search for elements that match the conditions defined by the specified predicate of elements and returns the zero-based index of the first occurrence in the range of elements from the specified index to the last element.
int FindIndex(int startIndex, Predicate<T> match)
Search for elements that match the conditions defined by the specified predicate and return the zero-based index of the first occurrence in a range of elements starting at the specified index and containing the specified number of elements
int FindIndex(int startIndex, int count, Predicate<T> match)
T FindLast(Predicate<T> match)
int FindLastIndex(Predicate<T> match)
int FindLastIndex(int startIndex, Predicate<T> match)
int FindLastIndex(int startIndex, int count, Predicate<T> match)
Searches the specified object and returns the zero-based index of the first match
int IndexOf(T item)
Searches the specified object and returns the first match in the range of elements from the specified index to the last element The zero-based index of the item
int IndexOf(T item, int index)
int IndexOf(T item, int index, int count)
Searches the specified object and returns the zero-based index of the last matching item.
int LastIndexOf(T item)
int LastIndexOf(T item, int index)
int LastIndexOf(T item, int index, int count)
4-5 Binary Search
Use the default comparator to search for an element in the entire
sortedList and return the zero-based index of the element.
int BinarySearch(T item);
Searches the entire sorted list for an element using the specified comparator and returns the zero-based index of the element.
int BinarySearch(T item, IComparer<T> comparer)
int BinarySearch(int index, int count, T item, IComparer<T> comparer)
4-6 SortingUse the default comparator to sort the elements in the entire List.
void Sort()
Use the specified System.Comparison to sort the elements in the entire List.
void Sort(Comparison<T> comparison)
Use the specified comparator to sort the elements in the List.
void Sort(IComparer<T> comparer)
void Sort(int index, int count, IComparer<T> comparer)
4-7 Performance analysis
Time complexity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Insert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Remove | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GetAnItem | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sort | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Find | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4-8 附使用陷阱点:1 list.Min() 和 list.Max() 和 Average()等Linq方法,当list元素个数为0,则会出现“序列不包含任何元素”的异常。 2 object.ToString() 使用前要检测object是否为null。 3 Foreach遍历时,迭代器是不允许增加或删除的。例如: public List<MDevice> GetNormalDevices(List<MDevice> devices) { rtnDevices = devices; foreach (var device in devices) { var tmpdevices = bslMDevice.GetMDeviceByDeviceCode(device.DeviceCode); if (!devices[0].IsNormal) { //这是非法的,因为移除rtnDevices列表的一个元素,等价于移除devices列表。 rtnDevices.Remove(device); } } } 5 SortedList5-1 SortedList简介Sorted表明了它内部实现自动排序,List表明了它有点像List,可以通过index访问集合中的元素。 5-2 内部实现机理一个SortedList对象内部维护了2个数组,以此来存储元素,其中一个数组用来存放键(keys),另一个存放键关联的值(values)。每一个元素都是键值对(key/value pair)。key不能是null,value可以。 5-3 总结API5-3-1 Capacity一个SortedList对象的容量是SortedList能容纳的元素数,这个值是动态变化,自动调整的。如下所示: SortedList mySL = new SortedList(); mySL.Add("Third", "!"); mySL.Add("Second", "World"); mySL.Add("First", "Hello"); Console.WriteLine( "mySL" ); Console.WriteLine( " Capacity: {0}", mySL.Capacity ); 此时Capacity: 16 如果添加到mySL中的元素增多,相应的Capacity会相应的自动变大。 5-3-2 访问元素通过index访问SortedList对象要想通过index访问,需要使用构造函数SortedList() 或 SortedList(IComparer icompared)。 SortedList sortedList = new SortedList(); sortedList.Add(3,"gz"); sortedList.Add(9, "lhx"); sortedList.Add(3, "gz");object getByIndex = sortedList.GetByIndex(2); 通过key访问SortedList对象要想通过key访问,需要使用带有TKey,TValue的泛型构造函数。 SortedList<int,string> sortedList = new SortedList<int,string>(); sortedList.Add(3,"gz"); sortedList.Add(9, "lhx");object getByIndex = sortedList[3]; 5-3-3排序SortedList有一种默认的比较顺序,比如下面的代码: SortedList<int,string> sortedList = new SortedList<int,string>(); sortedList.Add(9,"gz"); sortedList.Add(3, "lhx"); 结果是 sortedList中第一个对是3,”lhx” 如果不想按照默认的排序顺序,需要自己在构造时定制一种排序顺序,如下面的代码: 实现排序接口新建一个私有排序类,实现接口IComparer private class ImplementICompare: IComparer<int> { public int Compare(int x, int y) { return x < y ? 1 : -1; } } 构造SortedListImplementICompare impleCompare = new ImplementICompare(); SortedList<int, string> sortedList = new SortedList<int, string>(impleCompare); sortedList.Add(9,"gz"); sortedList.Add(3, "lhx"); 按照键从大到小的顺序排序,结果是 sortedList中第一个对是9,”gz” 5-3-4 添加元素用add接口实现添加某个元素到集合中,不允许重复添加相同键。 SortedList<int,string> sortedList = new SortedList<int,string>(); sortedList.Add(9,"gz"); sortedList.Add(3, "lhx"); 5-3-5 移除元素移除集合中指定元素Remove(object removedElement);指定index处移除元素RemoveAt(int index)。 Remove(object)SortedList mySL = new SortedList(); mySL.Add( "3c", "dog" ); mySL.Add( "2c", "over" ); mySL.Add( "3a", "the" ); mySL.Add( "3b", "lazy" ); mySL.Remove( "3b" ); //sucessful to remove SortedList<int, string> sortedList = new SortedList<int, string>(); sortedList.Add(9,"gz"); sortedList.Add(3, "lhx");bool removedFlag = sortedList.Remove(3); //true ImplementICompare impleCompare = new ImplementICompare(); SortedList<int, string> sortedList = new SortedList<int, string>(impleCompare); sortedList.Add(9,"gz"); sortedList.Add(3, "lhx");bool removedFlag = sortedList.Remove(3); //false 这是需要注意的一个地方,构造器带有impleCompare实现了排序接口时,好像不能移除某个元素,需要待确认。 RemoveAt(int index)SortedList sorted = new SortedList(); sorted.Add(9, "gz"); sorted.Add(3, "lhx"); sortedList.RemoveAt(1); //在排序后的位置移除,sortedList的一个对的键 为3,第二个对的键为9,因此移除了9这个键值对 5-4 性能一个SortedList的操作相比Hashtable对象是要慢些的,由于它实现了排序功能。但是,SortedList提供了访问的方便性,由于既可以通过index,也可以通过key去访问元素。 6 .net容器相关接口
7 Interface UML![]() 8 Time complexity of each container
|

如何使用C#编写时间序列预测算法时间序列预测是一种通过分析过去的数据来预测未来数据趋势的方法。它在很多领域,如金融、销售和天气预报中有广泛的应用。在本文中,我们将介绍如何使用C#编写时间序列预测算法,并附上具体的代码示例。数据准备在进行时间序列预测之前,首先需要准备好数据。一般来说,时间序列数据应该具有足够的长度,并且是按照时间顺序排列的。你可以从数据库或者

如何使用Redis和C#开发分布式事务功能引言分布式系统的开发中,事务处理是一项非常重要的功能。事务处理能够保证在分布式系统中的一系列操作要么全部成功,要么全部回滚。Redis是一种高性能的键值存储数据库,而C#是一种广泛应用于开发分布式系统的编程语言。本文将介绍如何使用Redis和C#来实现分布式事务功能,并提供具体代码示例。I.Redis事务Redis

如何实现C#中的人脸识别算法人脸识别算法是计算机视觉领域中的一个重要研究方向,它可以用于识别和验证人脸,广泛应用于安全监控、人脸支付、人脸解锁等领域。在本文中,我们将介绍如何使用C#来实现人脸识别算法,并提供具体的代码示例。实现人脸识别算法的第一步是获取图像数据。在C#中,我们可以使用EmguCV库(OpenCV的C#封装)来处理图像。首先,我们需要在项目

如何使用C#编写动态规划算法摘要:动态规划是求解最优化问题的一种常用算法,适用于多种场景。本文将介绍如何使用C#编写动态规划算法,并提供具体的代码示例。一、什么是动态规划算法动态规划(DynamicProgramming,简称DP)是一种用来求解具有重叠子问题和最优子结构性质的问题的算法思想。动态规划将问题分解成若干个子问题来求解,通过记录每个子问题的解,

Redis在C#开发中的应用:如何实现高效的缓存更新引言:在Web开发中,缓存是提高系统性能的常用手段之一。而Redis作为一款高性能的Key-Value存储系统,能够提供快速的缓存操作,为我们的应用带来了不少便利。本文将介绍如何在C#开发中使用Redis,实现高效的缓存更新。Redis的安装与配置在开始之前,我们需要先安装Redis并进行相应的配置。你可以

C#开发中如何处理跨域请求和安全性问题在现代的网络应用开发中,跨域请求和安全性问题是开发人员经常面临的挑战。为了提供更好的用户体验和功能,应用程序经常需要与其他域或服务器进行交互。然而,浏览器的同源策略导致了这些跨域请求被阻止,因此需要采取一些措施来处理跨域请求。同时,为了保证数据的安全性,开发人员还需要考虑一些安全性问题。本文将探讨C#开发中如何处理跨域请

如何实现C#中的图像压缩算法摘要:图像压缩是图像处理领域中的一个重要研究方向,本文将介绍在C#中实现图像压缩的算法,并给出相应的代码示例。引言:随着数字图像的广泛应用,图像压缩成为了图像处理中的重要环节。压缩能够减小存储空间和传输带宽,并能提高图像处理的效率。在C#语言中,我们可以通过使用各种图像压缩算法来实现对图像的压缩。本文将介绍两种常见的图像压缩算法:

如何在C#中实现遗传算法引言:遗传算法是一种模拟自然选择和基因遗传机制的优化算法,其主要思想是通过模拟生物进化的过程来搜索最优解。在计算机科学领域,遗传算法被广泛应用于优化问题的解决,例如机器学习、参数优化、组合优化等。本文将介绍如何在C#中实现遗传算法,并提供具体的代码示例。一、遗传算法的基本原理遗传算法通过使用编码表示解空间中的候选解,并利用选择、交叉和


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
