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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#学习笔记(集合)

發布時間:2024/6/18 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#学习笔记(集合) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 System.Array類和System.collections.ArrayList類

示例:控制臺程序,新疆三個類,抽象類Animal以及兩個繼承類Cows和Chicken

Animal.cs

public abstract class Animal
{
  protected string name;

  public string Name
  {
    get { return name; }
    set { name = value; }
  }
  public Animal()
  {
    name = "The animal with no name";
  }
  public Animal(string newName)
  {
    name = newName;
  }
  public void Feed()
  {
    Console.WriteLine("{0} has been fed.", name);
  }
}

Cows.cs

public class Cow : Animal
{
  public void Milk()
  {
    Console.WriteLine("{0} has been fed.",name);
  }
  public Cow(string newName)
  : base(newName)
  {

  }
}

Chicken.cs

public class Chicken : Animal
{
  public void LayEgg()
  {
    Console.WriteLine("{0} has been fed.", name);
  }
  public Chicken(string newName)
  : base(newName)
  {

  }
}

Program.cs

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("Create an Array type collection of Animal " + "object and use it");
    Animal[] animalArray = new Animal[2];
    Cow myCow1 = new Cow("Deirdre");
    animalArray[0] = myCow1;
    animalArray[1] = new Chicken("Ken");

    foreach (Animal myAnimal in animalArray)
    {
      Console.WriteLine("New {0} object added to Array collection," + "Name = {1}", myAnimal.ToString(), myAnimal.Name);
    }

    Console.WriteLine("Array collection contatains {0} objects.",animalArray.Length);

    animalArray[0].Feed();
    ((Chicken)animalArray[1]).LayEgg();
    Console.WriteLine();

    Console.WriteLine("Create an ArrayList type collection of Animal " + "object and use it");
    ArrayList animalArrayList = new ArrayList();
    Cow myCow2 = new Cow("Hayley");
    animalArrayList.Add(myCow2);
    animalArrayList.Add(new Chicken("Roy"));
    foreach(Animal myAnimal in animalArrayList)
    {
      Console.WriteLine("New {0} object added to ArrayList collection," + "Name = {1}",myAnimal.ToString(),myAnimal.Name);
    }
    Console.WriteLine("ArrayList collection contains {0} objects.",animalArrayList.Count);
    ((Animal)animalArrayList[0]).Feed();
    ((Chicken)animalArrayList[1]).Feed();
    Console.WriteLine();

    Console.WriteLine("Additional manipulaion of ArrayList:");
    animalArrayList.RemoveAt(0);
    ((Animal)animalArrayList[0]).Feed();
    animalArrayList.AddRange(animalArray);
    ((Chicken)animalArrayList[2]).LayEgg();
    Console.WriteLine("The animal called {0} is at index {1}.",myCow1.Name,animalArrayList.IndexOf(myCow1));
    myCow1.Name = "Janice";
    Console.WriteLine("The animal is called {0}.",((Animal)animalArrayList[1]).Name);
    Console.ReadKey();
  }
}

輸出結果:

?

  這個示例創建了兩個對象集合,第一個使用System.Array類(這是一個簡單的數組),第二個集合使用了System.collections.ArrayList類。

  對于簡單的數組來說,只有用固定的大小初始化后,才能使用它。

  Animal[] animlaArray = new Animal[2];

  而ArrayList集合不需要初始化其大小,所以可以使用一下代碼創建列表animalArrayList;

  ArrayList[] myArrayList = new ArrayList();

  因為數組是引用類型,因此用一個長度初始化數組并沒有初始化他所包含的項。要使用一個指定的項,該項還需要初始化,即需要給這個項賦予初始化了的對象:

  Cow myCow1 = new Cow("Deirdre");

  animalArray[0] = myCow1;

  animalArray[1] = new Chicken("Ken");

  對于ArrayList集合,他沒有現成的項。他沒有null引用的項。這樣就不能以相同的方式給索引賦予新實例。我們是用ArrayList對象的add()方法加新項:

  Cow myCow2 = new Cow("hayley");

  animalArrayList.Add(myCow2);

  animalArrayList.Add(new Chicken("Roy"));

注意:

  數組的類型是抽象類型Animal,因此不能直接調用由派生類提供的方法,而必須使用數據類型轉換:

  ((Chicken)animalArray[1]).LayEgg();

2 定義集合,創建自己的、強化類型的集合

2.1 System.Collections.CollectionBase類

實例:

在上面的基礎之上添加一個新類Animals.cs

Animals.cs

public class Animals : CollectionBase
{
  public void Add(Animal newAnimal)
  {
    List.Add(newAnimal);
  }
  public void Remove(Animal newAnimal)
  {
  List.Remove(newAnimal);
  }
  public Animals()
  {
    Console.WriteLine("This is Animals!!!");
  }
  public Animal this[int animalIndex]
  {
    get
    {
      return (Animal)List[animalIndex];
    }
    set
    {
      List[animalIndex] = value;
    }
  }
}

Program.cs

class Program
{
  static void Main(string[] args)
  {
    Animals animalCollection = new Animals();
    animalCollection.Add(new Cow("Jack"));
    animalCollection.Add(new Chicken("Vera"));
    foreach (Animal myAnimal in animalCollection)
    {
      myAnimal.Feed();
    }
    Console.ReadKey();
  }
}

輸出結果:

這個實例使用了上一節詳細介紹的代碼,實現類Animal中強化類型的Animal對象集合。Main()中代碼僅實例化了一個Animals對象animalCollection,添加了兩個項,再使用foreach循環調用這個兩個對象繼承于基類Animal的Feed方法。

3 迭代器

  迭代器的定義是,他是一個代碼塊,按順序提供了要在foreach循環中使用的所有值。一般情況下,這個代碼塊是一個方法,但也可以使用訪問器和其他代碼塊作為迭代器。這里為了簡單起見,介紹方法:

public static IEnumerable SimpleList()

{

  yield return "string 1";

  yield return "string 2";

  yield return "string 3";

}

public static void Main(string[] args)

{

  foreach(string item in SimpleList())

  {

    Console.WriteLine(item);

  }

  Console.ReadKey;

}

  實際上,并沒有返回string類型的項,而是返回了object類型的值。因為object是所有類型的基類,也就是說,可以從yield語句中返回任意類型。

?

轉載于:https://www.cnblogs.com/DannyShi/p/4501594.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。