214创建 Cards集 ■新类 Cards是Gard对象的一个定制集合。 cc⊥ ass ec private Cards cards new Cards public Deck o for (int suitVal =0: suitval 4: suitval++) for (int rankVal =1: rankVal< 14: rank Val++) cards. Add(new Card((Suit)suitVal, (RankrankVal) public Card GetCard(int cardNum f( cardIum≥0&& return cards [cardNum throw (new System. ArgumentOutOfRangeException("cardNum", cardNum Value must be between 0 and 51. " )
2.1.4 创建Cards集合 新类Cards是Card对象的一个定制集合
214创建 Cards集 public void Shuffle Cards newDeck new Cards bool[ assigned new bool [52] Random sourceGen new Random for (int i=0: i< 52. i++) int sourceCard =0 bool soundCard false while (foundCard = false) sourceCard= sourceGen Next(52) if (assi gned[sourceCard] = false) soundCard = true assigned[sourceCard] = true newDeck. Add(cards [sourceCard] newDeck CopyTo(cards)
2.1.4 创建Cards集合
214创建 Cards集 ■应用程序代码不需要修改,即可得到同样结果。 static void Main(string[] args) Deck my Deck new Deck O my Deck. Shuffle for (int i=0:i< 52. i++) Card temp Card my Deck. GetCard (i) Write(tempCard ToString O) if(i!=51) Write(, ) WriteLineO he Three of Clubs, The Nine of Clubs, The Jack of Diamonds, The Five of Diamond The Six of diamonds. The Seven of Hearts. The Jack of Clubs. The six of Heart ReadEy) The Nine of Spades, The Deuce of Hearts, The Nine of Hearts, The Ace of Clubs he Ace of Hearts, The Seven of Clubs, The King of Clubs, The Five of Hearts Ten of Clubs, The Ace of Diamonds, The Six of Spades, The Four Queen of Diamonds, The queen of Clubs, The Deuce of Diamonds, The Four of D nds, The Jack of Hearts, The Five of Spades. The Eight of Clubs, The Ten of D onds, The King of Diamonds, The Jack of Spades, The King of Spades, The Eight Spades, The Six of Clubs, The Seven of Spades, The Eight of Diamonds, The Four Clubs, The Three of Spades, The Ten of Hearts, The Ace of Spades, The Eight f Hearts, The Seven of Diamonds, The Nine of Diamonds, The Ten of Spades, The euce of Spades, The Five of Clubs, The King of Hearts, The Three of Hearts,The euce of Clubs, The Queen of Spades, The Three of Diamonds, The Four of Spades
2.1.4 创建Cards集合 应用程序代码不需要修改,即可得到同样结果
21.5键控集合和 FIctional Dictionary是C#中的一种字典集合类 ●要使用 Dict i y集合,需要导入C排#泛型命名空间 System. Col lections. Gener ic Dictionary的描述 1、从一组键(Key)到一组值( Value)的映射,每一个添 加项都是由一个值及其相关连的键组成; >2、任何键都必须是唯一的; >3、键不能为空,若值为引用类型,则可以为空值 >4、Key和 lValue可以是任何类型( string,int,自定义类型 等)
2.1.5 键控集合和IDictionary Dictionary是C#中的一种字典集合类。 要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic; Dictionary的描述 1、从一组键(Key)到一组值(Value)的映射,每一个添 加项都是由一个值及其相关连的键组成; 2、任何键都必须是唯一的; 3、键不能为空,若值为引用类型,则可以为空值 4、Key和Value可以是任何类型(string,int,自定义类型 等)
21.5键控集合和 IDictionar ● Dictionary?常用用法:以key的类型为int, valuel的类型为 str ing为例 创建及初始化 Dictionary<int, str ing> my Dict ionar y=new dictionary<int, str ing>o 添加元素 my Dictionary. Add(1, "C#) my Dictionary. Add(2, C++) my Dictionary. Add(3, "ASP. NET") my Dictionary. Add (4, "MVC")
2.1.5 键控集合和IDictionary Dictionary常用用法:以 key 的类型为 int , value的类型为 string 为例 创建及初始化 Dictionary<int,string> myDictionary=new Dictionary<int,string>(); 添加元素 myDictionary.Add(1,"C#"); myDictionary.Add(2,"C++"); myDictionary.Add(3,"ASP.NET"); myDictionary.Add(4,"MVC");