日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

【005】◀▶ C#学习笔记(四)(集合)

發(fā)布時間:2025/7/14 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【005】◀▶ C#学习笔记(四)(集合) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

《C#入門經(jīng)典(中文第四版)》第11章 - 第x章學(xué)習(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類,這個抽象類提供了集合類的許多實現(xiàn)方式。
  • CollectionBase類有借口IEnumerable、ICollection和IList,但只提供了一些要求的執(zhí)行代碼,特別是 IList的Clear()和RemoveAt()方法,以及ICollection和Count屬性。如果要使用提供的功能,就需要自己執(zhí)行其他代碼。
  • View Code - CollectionBase

2. CollectionBase 屬性:

  • Count: 獲取包含在 CollectionBase 實例中的元素數(shù)。 不能重寫此屬性。
  • List:獲取一個 IList,它包含 CollectionBase 實例中元素的列表。

3. CollectionBase 方法:

  • Clear: CollectionBase 實例移除所有對象。 不能重寫此方法。
  • RemoveAt: 移除 CollectionBase 實例的指定索引處的元素。 此方法不可重寫。

---------------------------------------------------------------------------------------------------------

???????? ?? ╔════════╗
╠════╣??? 第A2個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝

●·● DictionaryBase 類

1. 為鍵/值對的強類型集合提供 abstract 基類。

2. DictionaryBase 方法:

  • Clear:清除 DictionaryBase 實例的內(nèi)容。

3. DictionaryBase 屬性:

  • Count:獲取包含在 DictionaryBase 實例中的元素數(shù)。
  • Dictionary:獲取包含在 DictionaryBase 實例中的元素的列表。
  • InnerHashtable:獲取包含在 DictionaryBase 實例中的元素的列表。

其他:

<1> 迭代器

  • 如果要迭代一個類,可以使用方法GetEnumerator(),其返回類型是IEnumerable。
  • 如果要迭代一個類成員,例如一個方法,則使用IEnumerable。
  • 在迭代器塊中,使用yield關(guān)鍵字選擇要在foreach循環(huán)中使用的值。其語法如下:
  • yield return value;
  • 對于迭代器,可以使用下面的語句中斷信息返回foreach循環(huán)的過程:
  • 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; //將偶數(shù)返回foreach
    else
    yield break; //如果i大于10,則推出foreach
    }
    }
    }
    //0
    //2
    //4
    //6
    //8
    //10
    //請按任意鍵繼續(xù). . .

    }

<2> 淺度復(fù)制(11.1.7)

  • 使用受保護的方法System.Object.MemberwiseClone()進行淺度復(fù)制(shallow copy)。使用了一個GetCopy()方法,如下所示:
  • View Code - 淺度復(fù)制 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
    //請按任意鍵繼續(xù). . .

    }
  • 類中的值實現(xiàn)深度復(fù)制,但是類中的類實現(xiàn)淺度復(fù)制,只是復(fù)制引用。

<3> 深度復(fù)制

  • 為此,實現(xiàn)ICloneable 接口,該接口有一個方法Clone(),這個方法不帶參數(shù),返回一個對象類型,其簽名和上面使用的GetCopy()方法相同。修改上面的類,可以使用下面的深度復(fù)制代碼:?
  • View Code - 深度復(fù)制 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
    //請按任意鍵繼續(xù). . .

    }
  • 類中的對象也實現(xiàn)了深度復(fù)制。

---------------------------------------------------------------------------------------------------------

???????? ?? ╔════════╗
╠════╣??? 第A3個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝

●·● List<T> 類

1. 表示可通過索引訪問的對象的強類型列表。 提供用于對列表進行搜索、排序和操作的方法。

2. List<T> 構(gòu)造函數(shù):

List<string> dinosaurs = new List<string>();

3. List<T> 方法:

  • Add:將對象添加到 List<T> 的結(jié)尾處。
  • AddRange:將指定集合的元素添加到 List<T> 的末尾。
  • Clear:從 List<T> 中移除所有元素。
  • Contains:確定某元素是否在 List<T> 中。
  • Exists:確定 List<T> 是否包含與指定謂詞所定義的條件相匹配的元素。
  • Find:搜索與指定謂詞所定義的條件相匹配的元素,并返回整個 List<T> 中的第一個匹配元素。
  • FindAll:檢索與指定謂詞定義的條件匹配的所有元素。
  • FindLast:搜索與指定謂詞所定義的條件相匹配的元素,并返回整個 List<T> 中的最后一個匹配元素。
  • ForEach:對 List<T> 的每個元素執(zhí)行指定操作。
  • IndexOf:
  • Insert:
  • Remove:
  • RemoveAll:
  • RemoveAt:
  • Sort:
  • ToArray:將 List<T> 的元素復(fù)制到新數(shù)組中。

4. List<T> 屬性:

  • Item:獲取或設(shè)置指定索引處的元素。
  • Count:獲取 List<T> 中實際包含的元素數(shù)。
  • Capacity:獲取或設(shè)置該內(nèi)部數(shù)據(jù)結(jié)構(gòu)在不調(diào)整大小的情況下能夠容納的元素總數(shù)。

※ 參考:http://hi.baidu.com/coldwindsnow/blog/item/bfb5270025a13f1d728b6513.html

---------------------------------------------------------------------------------------------------------

???????? ?? ╔════════╗
╠════╣??? 第A4個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝

●·● Dictionary<TKey, TValue> 類

1. 表示鍵和值的集合,通過引用鍵來調(diào)出值。

2. Dictionary類的構(gòu)造函數(shù):

  • Dictionary<string, string> d = new Dictionary<string, string>();
  • Dictionary<int, string> exer = new Dictionary<int, string>();

3. Dictionary類的屬性:

  • Count:鍵/值對的個數(shù)。
  • 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 類

>>內(nèi)部鏈接<<

---------------------------------------------------------------------------------------------------------

???????? ?? ╔════════╗
╠════╣??? 第A6個??? ╠══════════════════════════════════════════════════╣
??????????? ╚════════╝

●·● Hashtable 類

>>內(nèi)部鏈接<<

---------------------------------------------------------------------------------------------------------

???????? ?? ╔════════╗
╠════╣??? 第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
    //請按任意鍵繼續(xù). . .
    }
    }

總結(jié)

以上是生活随笔為你收集整理的【005】◀▶ C#学习笔记(四)(集合)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。