【005】◀▶ C#学习笔记(四)(集合)
《C#入門經典(中文第四版)》第11章 - 第x章學習筆記
---------------------------------------------------------------------------------------------------------
●·● 目錄:
A1 ………… CollectionBase 類
A2 ………… DictionaryBase 類
A3 ………… List<T> 類
A4 ………… Dictionary<TKey, TValue> 類
A5 ………… ArrayList 類
A6 ………… Hashtable 類
A7 ………… 比較
A8 …………?IFields 接口
A9 …………?IQueryFilter 接口
---------------------------------------------------------------------------------------------------------
???????? ?? ╔════════╗
╠════╣??? 第A1個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝
●·● CollectionBase 類:
1. 為強類型集合提供 abstract 基類。
- 我們可以從一個類中派生自己的集合,例如System.Collection.CollecionBase類,這個抽象類提供了集合類的許多實現方式。
- CollectionBase類有借口IEnumerable、ICollection和IList,但只提供了一些要求的執行代碼,特別是 IList的Clear()和RemoveAt()方法,以及ICollection和Count屬性。如果要使用提供的功能,就需要自己執行其他代碼。
- View Code - CollectionBase
2. CollectionBase 屬性:
- Count: 獲取包含在 CollectionBase 實例中的元素數。 不能重寫此屬性。
- List:獲取一個 IList,它包含 CollectionBase 實例中元素的列表。
3. CollectionBase 方法:
- Clear: 從 CollectionBase 實例移除所有對象。 不能重寫此方法。
- RemoveAt: 移除 CollectionBase 實例的指定索引處的元素。 此方法不可重寫。
---------------------------------------------------------------------------------------------------------
???????? ?? ╔════════╗
╠════╣??? 第A2個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝
●·● DictionaryBase 類:
1. 為鍵/值對的強類型集合提供 abstract 基類。
2. DictionaryBase 方法:
- Clear:清除 DictionaryBase 實例的內容。
3. DictionaryBase 屬性:
- Count:獲取包含在 DictionaryBase 實例中的元素數。
- Dictionary:獲取包含在 DictionaryBase 實例中的元素的列表。
- InnerHashtable:獲取包含在 DictionaryBase 實例中的元素的列表。
其他:
<1> 迭代器
- 如果要迭代一個類,可以使用方法GetEnumerator(),其返回類型是IEnumerable。
- 如果要迭代一個類成員,例如一個方法,則使用IEnumerable。
- 在迭代器塊中,使用yield關鍵字選擇要在foreach循環中使用的值。其語法如下:
- yield return value;
- 對于迭代器,可以使用下面的語句中斷信息返回foreach循環的過程:
- yield break;
- View Code - 迭代器舉例 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Even e = new Even(0,20);
foreach (int a in e)
{
Console.WriteLine(a);
}
}
}
public class Even : DictionaryBase
{
private int min;
private int max;
public Even(int minValue, int maxValue)
{
min = minValue;
max = maxValue;
}
public Even():this(1,10)
{
}
public IEnumerator GetEnumerator()
{
for (int i = min; i <= max;i++ )
{
if (i % 2 == 0)
if(i<=10)
yield return i; //將偶數返回foreach
else
yield break; //如果i大于10,則推出foreach
}
}
}
//0
//2
//4
//6
//8
//10
//請按任意鍵繼續. . .
}
<2> 淺度復制(11.1.7)
- 使用受保護的方法System.Object.MemberwiseClone()進行淺度復制(shallow copy)。使用了一個GetCopy()方法,如下所示:
- View Code - 淺度復制 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Cloner mySource = new Cloner(5);
Cloner myTarget = (Cloner)mySource.GetCopy();
Console.WriteLine("{0}:mySource,{1}", mySource.ToString(), mySource.MyContent.Val);
Console.WriteLine("{0}:myTarget,{1}", myTarget.ToString(), myTarget.MyContent.Val);
mySource.Val = 10;
mySource.MyContent.Val = 2;
Console.WriteLine(mySource.Val);
Console.WriteLine(myTarget.Val);
Console.WriteLine("{0}:mySource,{1}", mySource.ToString(), mySource.MyContent.Val);
Console.WriteLine("{0}:myTarget,{1}", myTarget.ToString(), myTarget.MyContent.Val);
}
}
public class Content
{
public int Val;
}
public class Cloner
{
public int Val;
public Content MyContent = new Content();
public Cloner(int newVal)
{
Val = newVal;
}
public object GetCopy()
{
return MemberwiseClone();
}
}
//ConsoleApplication3.Cloner:mySource,0
//ConsoleApplication3.Cloner:myTarget,0
//10
//5
//ConsoleApplication3.Cloner:mySource,2
//ConsoleApplication3.Cloner:myTarget,2
//請按任意鍵繼續. . .
} - 類中的值實現深度復制,但是類中的類實現淺度復制,只是復制引用。
<3> 深度復制
- 為此,實現ICloneable 接口,該接口有一個方法Clone(),這個方法不帶參數,返回一個對象類型,其簽名和上面使用的GetCopy()方法相同。修改上面的類,可以使用下面的深度復制代碼:?
- View Code - 深度復制 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Cloner mySource = new Cloner(5);
Cloner myTarget = (Cloner)mySource.Clone();
Console.WriteLine("{0}:mySource,{1}", mySource.ToString(), mySource.MyContent.Val);
Console.WriteLine("{0}:myTarget,{1}", myTarget.ToString(), myTarget.MyContent.Val);
mySource.MyContent.Val = 2;
Console.WriteLine("{0}:mySource,{1}", mySource.ToString(), mySource.MyContent.Val);
Console.WriteLine("{0}:myTarget,{1}", myTarget.ToString(), myTarget.MyContent.Val);
}
}
public class Content
{
public int Val;
}
public class Cloner : ICloneable
{
public Content MyContent = new Content();
public Cloner(int newVal)
{
MyContent.Val = newVal;
}
public object Clone()
{
Cloner clonedCloner = new Cloner(MyContent.Val);
return clonedCloner;
}
}
//ConsoleApplication3.Cloner:mySource,5
//ConsoleApplication3.Cloner:myTarget,5
//ConsoleApplication3.Cloner:mySource,2
//ConsoleApplication3.Cloner:myTarget,5
//請按任意鍵繼續. . .
} - 類中的對象也實現了深度復制。
---------------------------------------------------------------------------------------------------------
???????? ?? ╔════════╗
╠════╣??? 第A3個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝
●·● List<T> 類:
1. 表示可通過索引訪問的對象的強類型列表。 提供用于對列表進行搜索、排序和操作的方法。
2. List<T> 構造函數:
3. List<T> 方法:
- Add:將對象添加到 List<T> 的結尾處。
- AddRange:將指定集合的元素添加到 List<T> 的末尾。
- Clear:從 List<T> 中移除所有元素。
- Contains:確定某元素是否在 List<T> 中。
- Exists:確定 List<T> 是否包含與指定謂詞所定義的條件相匹配的元素。
- Find:搜索與指定謂詞所定義的條件相匹配的元素,并返回整個 List<T> 中的第一個匹配元素。
- FindAll:檢索與指定謂詞定義的條件匹配的所有元素。
- FindLast:搜索與指定謂詞所定義的條件相匹配的元素,并返回整個 List<T> 中的最后一個匹配元素。
- ForEach:對 List<T> 的每個元素執行指定操作。
- IndexOf:
- Insert:
- Remove:
- RemoveAll:
- RemoveAt:
- Sort:
- ToArray:將 List<T> 的元素復制到新數組中。
4. List<T> 屬性:
- Item:獲取或設置指定索引處的元素。
- Count:獲取 List<T> 中實際包含的元素數。
- Capacity:獲取或設置該內部數據結構在不調整大小的情況下能夠容納的元素總數。
※ 參考:http://hi.baidu.com/coldwindsnow/blog/item/bfb5270025a13f1d728b6513.html
---------------------------------------------------------------------------------------------------------
???????? ?? ╔════════╗
╠════╣??? 第A4個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝
●·● Dictionary<TKey, TValue> 類:
1. 表示鍵和值的集合,通過引用鍵來調出值。
2. Dictionary類的構造函數:
- Dictionary<string, string> d = new Dictionary<string, string>();
- Dictionary<int, string> exer = new Dictionary<int, string>();
3. Dictionary類的屬性:
- Count:鍵/值對的個數。
- Keys:鍵的集合。
- Values:值的集合。
- 使用索引,例如:exer[111] = "Alex"??? 類名[鍵] = 值
4. Dictionary類的方法:
- Add:將指定的鍵和值加入到字典中。
- Clear:清除所有鍵和值。
- ContainsKey:判斷是否包含某鍵。
- ContainsValue:判斷是否包含某值。
- Remove:移除指定鍵的值,同時將鍵也移除,其他自動上移。
參考1:http://www.cnblogs.com/linzheng/archive/2010/12/13/1904709.html
參考2:http://www.2cto.com/kf/201007/52242.html
---------------------------------------------------------------------------------------------------------
???????? ?? ╔════════╗
╠════╣??? 第A5個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝
●·● ArrayList 類:
>>內部鏈接<<
---------------------------------------------------------------------------------------------------------
???????? ?? ╔════════╗
╠════╣??? 第A6個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝
●·● Hashtable 類:
>>內部鏈接<<
---------------------------------------------------------------------------------------------------------
???????? ?? ╔════════╗
╠════╣??? 第A7個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝
●·● 比較:
1. 運算符重載
- 一元運算符: +,-, !, ~, ++,-- , true, false
- 二元運算符: +,-, *, /, %, &, |, ^, <<, >>
- 比較運算符: ==, !=, <, >, <=, >=
- View Code - 運算符重載舉例 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
AddClass1 op1 = new AddClass1();
op1.val = 5;
AddClass1 op2 = new AddClass1();
op2.val = 5;
AddClass1 op3 = op1 + op2;
Console.WriteLine(op3.val);
AddClass1 op4 = -op3;
Console.WriteLine(op4.val);
Console.WriteLine(op3 > op4);
}
}
public class AddClass1
{
public int val;
public static AddClass1 operator +(AddClass1 op1, AddClass1 op2)
{
AddClass1 returnVal = new AddClass1();
returnVal.val = op1.val + op2.val;
return returnVal;
}
public static AddClass1 operator -(AddClass1 op1)
{
AddClass1 returnVal = new AddClass1();
returnVal.val = -op1.val;
return returnVal;
}
public static bool operator >(AddClass1 op1,AddClass1 op2)
{
return (op1.val > op2.val);
}
public static bool operator <(AddClass1 op1,AddClass1 op2)
{
return (op1.val < op2.val);
}
//10
//-10
//True
//請按任意鍵繼續. . .
}
}
總結
以上是生活随笔為你收集整理的【005】◀▶ C#学习笔记(四)(集合)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux ssh密钥认证
- 下一篇: [已经验证通过]xp sp2 不支持WP