C#學習筆記(八)——集合、比較和轉換

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

一、集合

** System.Collections名稱空間中的幾個介面提供了基本的集合功能

Ps:這裡看成一個動態鏈表,但是已經完美的封裝好了。

一、使用集合

1、程式碼範例

(1)Animal.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Exercise{    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);        }    }}

(2)Cow.cs

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

(3)Chicken.cs

namespace Exercise{    public class Chicken:Animal    {        public void LayEgg()        {            Console.WriteLine("{0} has laid an egg.", name);        }        public Chicken(string newName):base(newName)        {        }    }}
(4)Program.cs

namespace Exercise{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("Create an Array type collection of Animal " + "objects and use it:");            Animal[] animalAraay = new Animal[2];            Cow myCowl = new Cow("Deirdre");            animalAraay[0] = myCowl;            animalAraay[1] = new Chicken("Ken");            foreach(Animal myAnimal in animalAraay)            {                Console.WriteLine("New {0} object added to Array collection, " + "Name = {1}", myAnimal.ToString(), myAnimal.Name);            }            Console.WriteLine("Array collection contains {0} objects.", animalAraay.Length);            animalAraay[0].Feed();            ((Chicken)animalAraay[1]).LayEgg();            Console.WriteLine();            Console.ReadKey();            Console.WriteLine("Create an ArrayList type collection of Animal " + "object and use it ");            ArrayList animalArrayList = new ArrayList();            Cow mycow2 = new Cow("Heylay");            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]).LayEgg();            Console.WriteLine();            Console.WriteLine("Additional manipulation of ArrayList:");            animalArrayList.RemoveAt(0);            ((Animal)animalArrayList[0]).Feed();            animalArrayList.AddRange(animalAraay);            ((Chicken)animalArrayList[2]).LayEgg();            Console.WriteLine("The animal called {0} is at index {1}.", myCowl.Name, animalArrayList.IndexOf(myCowl));            myCowl.Name = "Janice";            Console.WriteLine("The animal is now called {0}.", ((Animal)animalArrayList[1]).Name);            Console.ReadKey();        }    }}

2、運行結果

3、注意點:

(1)ArrayList建立時不需要指定初始長度值。但是Array是需要的。

(2)對於ArrayList是不強調類型的一個集合,所以再採用所屬對象的方法之類的時候,必須進行強制類型轉換,而對於Array來說,他是強調物件類型的集合,所以可以直接調用其方法,但是對於其衍生類別來說,還是需要進行強制類型轉換。

(3)還有些刪除(at)和擴充方法(AddRange)之類的擴充方法

(4)ArrayList需要加上using System.Collections;名字空間的引用,切記切記。

C#學習筆記(八)——集合、比較和轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.