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

歡迎訪問 生活随笔!

生活随笔

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

C#

C#4.0 Collections【集合】

發布時間:2023/12/20 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#4.0 Collections【集合】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

URI:http://www.albahari.com/nutshell/cs4ch07.aspx

集合:

Implementing IEnumerable<T> with an iterator(實現IEnumerable<T>的迭代器):

public class MyGenCollection : IEnumerable<int>
{
int[] data = {1, 2, 3};

public IEnumerator<int> GetEnumerator()
{
foreach (int i in data)
yield return i;
}

IEnumerator IEnumerable.GetEnumerator() // Explicit implementation(顯示實現IEnumerable<T>迭代器的接口)
{ // keeps it hidden.
return GetEnumerator();
}
}

Implementing IEnumerable<T> directly(直接顯示實現IEnumerable<T>接口的好處):

class MyIntList : IEnumerable<int>
{
int[] data = { 1, 2, 3 };

// The generic enumerator is compatible with both IEnumerable and
// IEnumerable<T>. We implement the nongeneric GetEnumerator method
// explicitly to avoid a naming conflict.
/*

一個通用的枚舉迭代器是需要兼容IEnumerable和IEnumerable<T>接口的,我們通過顯示的方式實現GetEnumerator接口來避免接口命名(同名)沖突
*/

public IEnumerator<int> GetEnumerator() { return new Enumerator(this); }
IEnumerator IEnumerable.GetEnumerator() { return new Enumerator(this); }

class Enumerator : IEnumerator<int>
{
int currentIndex = -1;
MyIntList collection;

internal Enumerator (MyIntList collection)
{
this.collection = collection;
}

public int Current { get { return collection.data [currentIndex]; } }
object IEnumerator.Current { get { return Current; } }

public bool MoveNext()
{
return ++currentIndex < collection.data.Length;
}

public void Reset() { currentIndex = -1; }

// Given we don’t need a Dispose method, it’s good practice to
// implement it explicitly, so it’s hidden from the public interface.(由于我們不需要對外提供Dispose()方法,最好的方式就是通過顯示實現接口的Dispose()方法。)

void IDisposable.Dispose() {}
}
}

Using List<T>(List<T>用例):

List<string> words = new List<string>(); // New string-typed list

words.Add ("melon");
words.Add ("avocado");
words.AddRange (new string[] { "banana", "plum" } );
words.Insert (0, "lemon"); // insert at
words.InsertRange (0, new string[] { "peach", "nashi" }); // start

words.Remove ("melon");
words.RemoveAt (3); // Remove the 4th element
words.RemoveRange (0, 2); // Remove first 2 elements

// Remove all strings starting in 'n':
words.RemoveAll (delegate (string s) { return s.StartsWith ("n"); });

Console.WriteLine (words [0]); // first word
Console.WriteLine (words [words.Count - 1]); // last word
foreach (string s in words) Console.WriteLine (s); // all words
List<string> subset = words.GetRange (1, 2); // 2nd->3rd words

string[] wordsArray = words.ToArray(); // Creates a new typed array

// Copy first two elements to the end of an existing array:
string[] existing = new string [1000];
words.CopyTo (0, existing, 998, 2);

List<string> bigWords = words.ConvertAll <string> // Converts to
(delegate (string s) { return s.ToUpper(); } ); // uppercase

List<int> lengths = words.ConvertAll <int>
(delegate (string s) { return s.Length; } );

Using LinkedList<T>:

LinkedList<string> tune = new LinkedList<string>();
tune.AddFirst ("do"); // do
tune.AddLast ("so"); // do - so

tune.AddAfter (tune.First, "re"); // do - re - so
tune.AddAfter (tune.First.Next, "mi"); // do - re - mi - so
tune.AddBefore (tune.Last, "fa"); // do - re - mi - fa - so

tune.RemoveFirst(); // re - mi - fa - so
tune.RemoveLast(); // re - mi - fa

LinkedListNode<string> miNode = tune.Find ("mi");
tune.Remove (miNode); // re - fa
tune.AddFirst (miNode); // mi - re - fa

foreach (string s in tune) Console.WriteLine (s);

Using Queue<T>:

Queue<int> q = new Queue<int>();
q.Enqueue (10);
q.Enqueue (20);
int[] data = q.ToArray(); // Exports to an array
Console.WriteLine (q.Count); // "2"
Console.WriteLine (q.Peek()); // "10"
Console.WriteLine (q.Dequeue()); // "10"
Console.WriteLine (q.Dequeue()); // "20"
Console.WriteLine (q.Dequeue()); // throws an exception (queue empty)

Using Stack<T>:

Stack<int> s = new Stack<int>();
s.Push (1); // Stack = 1
s.Push (2); // Stack = 1,2
s.Push (3); // Stack = 1,2,3
Console.WriteLine (s.Count); // Prints 3
Console.WriteLine (s.Peek()); // Prints 3, Stack = 1,2,3
Console.WriteLine (s.Pop()); // Prints 3, Stack = 1,2
Console.WriteLine (s.Pop()); // Prints 2, Stack = 1
Console.WriteLine (s.Pop()); // Prints 1, Stack = <empty>
Console.WriteLine (s.Pop()); // throws exception

Using HashSet<T>:

HashSet<char> letters = new HashSet<char> ("the quick brown fox");

Console.WriteLine (letters.Contains ('t')); // true
Console.WriteLine (letters.Contains ('j')); // false

foreach (char c in letters) Console.Write (c); // the quickbrownfx HashSet<char> letters = new HashSet<char> ("the quick brown fox");
letters.IntersectWith ("aeiou");
foreach (char c in letters) Console.Write (c); // euio HashSet<char> letters = new HashSet<char> ("the quick brown fox");
letters.ExceptWith ("aeiou");
foreach (char c in letters) Console.Write (c); // th qckbrwnfx HashSet<char> letters = new HashSet<char> ("the quick brown fox");
letters.SymmetricExceptWith ("the lazy brown fox");
foreach (char c in letters) Console.Write (c); // quicklazy

Using Dictionary<TKey,TValue>:

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

d.Add("One", 1);
d["Two"] = 2; // adds to dictionary because "two" not already present
d["Two"] = 22; // updates dictionary because "two" is now present
d["Three"] = 3;

Console.WriteLine (d["Two"]); // Prints "22"
Console.WriteLine (d.ContainsKey ("One")); // true (fast operation)
Console.WriteLine (d.ContainsValue (3)); // true (slow operation)
int val = 0;
if (!d.TryGetValue ("onE", out val))
Console.WriteLine ("No val"); // "No val" (case sensitive)

// Three different ways to enumerate the dictionary:

foreach (KeyValuePair<string, int> kv in d) // One ; 1
Console.WriteLine (kv.Key + "; " + kv.Value); // Two ; 22
// Three ; 3

foreach (string s in d.Keys) Console.Write (s); // OneTwoThree
Console.WriteLine();
foreach (int i in d.Values) Console.Write (i); // 1223

Using SortedDictionary<TKey,TValue>:

// MethodInfo is in the System.Reflection namespace
//該方法位于System.Reflection命名空間中

var sorted = new SortedList <string, MethodInfo>();

foreach (MethodInfo m in typeof (object).GetMethods())
sorted [m.Name] = m;

foreach (string name in sorted.Keys)
Console.WriteLine (name);

foreach (MethodInfo m in sorted.Values)
Console.WriteLine (m.Name + " returns a " + m.ReturnType);

Console.WriteLine (sorted ["GetHashCode"]); // Int32 GetHashCode()

Console.WriteLine (sorted.Keys [sorted.Count - 1]); // ToString
Console.WriteLine (sorted.Values[sorted.Count - 1].IsVirtual); // True

Extending Collection<T>(擴展Collection<T>):

public class Animal
{
public string Name;
public int Popularity;
public Zoo Zoo { get; internal set; }

public Animal(string name, int popularity)
{
Name = name; Popularity = popularity;
}
}

public class AnimalCollection : Collection <Animal>
{
Zoo zoo;
public AnimalCollection (Zoo zoo) { this.zoo = zoo; }

protected override void InsertItem (int index, Animal item)
{
base.InsertItem (index, item);
item.Zoo = zoo;
}
protected override void SetItem (int index, Animal item)
{
base.SetItem (index, item);
item.Zoo = zoo;
}
protected override void RemoveItem (int index)
{
this [index].Zoo = null;
base.RemoveItem (index);
}
protected override void ClearItems()
{
foreach (Animal a in this) a.Zoo = null;
base.ClearItems();
}
}

public class Zoo
{
public readonly AnimalCollection Animals;
public Zoo() { Animals = new AnimalCollection (this); }
}

Extending KeyedCollection<,>(擴展KeyedCollection<,>):

public class Animal
{
string name;
public string Name
{
get { return name; }
set {
if (Zoo != null) Zoo.NotifyNameChange (this, value);
name = value;
}
}
public int Popularity;
public Zoo Zoo { get; internal set; }

public Animal (string name, int popularity)
{
Name = name; Popularity = popularity;
}
}

public class AnimalCollection : KeyedCollection <string, Animal>
{
Zoo zoo;
public AnimalCollection (Zoo zoo) { this.zoo = zoo; }

internal void NotifyNameChange (Animal a, string newName)
{
this.ChangeItemKey (a, newName);
}

protected override string GetKeyForItem (Animal item)
{
return item.Name;
}

// The following methods would be implemented as in the previous example
protected override void InsertItem (int index, Animal item)...
protected override void SetItem (int index, Animal item)...
protected override void RemoveItem (int index)...
protected override void ClearItems()...
}

public class Zoo
{
public readonly AnimalCollection Animals;
public Zoo() { Animals = new AnimalCollection (this); }
}

class Program
{
static void Main()
{
Zoo zoo = new Zoo();
zoo.Animals.Add (new Animal ("Kangaroo", 10));
zoo.Animals.Add (new Animal ("Mr Sea Lion", 20));
Console.WriteLine (zoo.Animals [0].Popularity); // 10
Console.WriteLine (zoo.Animals ["Mr Sea Lion"].Popularity); // 20
zoo.Animals ["Kangaroo"].Name = "Mr Roo";
Console.WriteLine (zoo.Animals ["Mr Roo"].Popularity); // 10
}
}

Using EqualityComparer(比較相等的用例):

public class Customer
{
public string LastName;
public string FirstName;

public Customer (string last, string first)
{
LastName = last;
FirstName = first;
}
}

public class LastFirstEqComparer : EqualityComparer <Customer>
{
public override bool Equals (Customer x, Customer y)
{
return x.LastName == y.LastName && x.FirstName == y.FirstName;
}

public override int GetHashCode (Customer obj)
{
return (obj.LastName + ";" + obj.FirstName).GetHashCode();
}
}

static void Main()
{
Customer c1 = new Customer ("Bloggs", "Joe");
Customer c2 = new Customer ("Bloggs", "Joe");

// Because we’ve not overridden object.Equals, normal reference
// type equality semantics apply:

Console.WriteLine (c1 == c2); // false
Console.WriteLine (c1.Equals (c2)); // false

Dictionary<Customer, string> d = new Dictionary<Customer, string>();
d [c1] = "Joe";
Console.WriteLine (d.ContainsKey (c2)); // false

// Now with the custom equality comparer:

LastFirstEqComparer eq = new LastFirstEqComparer();
d = new Dictionary<Customer, string> (eq);
d [c1] = "Joe";
Console.WriteLine (d.ContainsKey (c2)); // true
}

Using the IComparer interfaces(使用IComparer接口):

class Wish
{
public string Name;
public int Priority;

public Wish (string name, int priority)
{
Name = name;
Priority = priority;
}
}

class PriorityComparer : Comparer <Wish>
{
public override int Compare (Wish x, Wish y)
{
if (object.Equals (x, y)) return 0; // Fail-safe check
return x.Priority.CompareTo (y.Priority);
}
} static void Main()
{
List<Wish> wishList = new List<Wish>();
wishList.Add (new Wish ("Peace", 2));
wishList.Add (new Wish ("Wealth", 3));
wishList.Add (new Wish ("Love", 2));
wishList.Add (new Wish ("3 more wishes", 1));

wishList.Sort (new PriorityComparer());
foreach (Wish w in wishList) Console.Write (w.Name + " | ");
}

SurnameComparer(起始名稱的比較):

class SurnameComparer : Comparer <string>
{
StringComparer strCmp;

public SurnameComparer (CultureInfo ci)
{
// Create a case-sensitive, culture-sensitive string comparer
strCmp = StringComparer.Create (ci, false);
}

string Normalize (string s)
{
s = s.Trim();
if (s.ToUpper().StartsWith ("MC")) s = "MAC" + s.Substring (2);
return s;
}

public override int Compare (string x, string y)
{
// Directly call Compare on our culture-aware StringComparer
return strCmp.Compare (Normalize (x), Normalize (y));
}
}

?

轉載于:https://www.cnblogs.com/magic_evan/archive/2011/12/20/2294737.html

總結

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

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