Generics

Embed Size (px)

Citation preview

C#

GenericsSystem.Collections.Generic

object

class TestClass{
private object obj;
public void Add(object obj){
this.obj=obj;
}
public object Get(){
return obj;
}
}

string s= Hello, world;

TestClass tc = new TestClass();

tc.Add(s);

...

int i = (int)tc.Get();

System.Collections

ArrayList

ArrayList

ArrayList()

ArrayList(ICollection)

ArrayList(int capacity)

int Add(object value)

void AddRange(ICollection)

void Clear()

Remove(object)

RemoveAt(int)

ArrayList

object this[int index] { get; set; }

int Count { get; }

int Capacity { get; set; }

int IndexOf(object value)

int LastIndexOf(object value)

ArrayList

class ArrayListTest{
private ArrayList arr;
public void Add(object o){
arr.Add(o);
}
public object Get(){
return arr[0];
}
}

Generic

Generic

class GenericTest{
private T field;
private T[] arr=new T[10];

public void Add(T o){
field=o;
arr[0]=o;
}
}

Generic

string s = Hello, world;

GenericTest gt = new GenericTest();

gt.Add(s);

Cinsole.WriteLine(gt.Get());

class TestClass{
private T t;
public void M1(U u){
Console.WriteLine(typeof(U));
} }

class TestClass{
private T t;
public void M1(U u){
Console.WriteLine(typeof(U));
} }

class TestClass{
public void TestMethod(T t){
Console.WriteLine(Test value: {0},t.ToString());
}

public void TestMethod2(){
Console.WriteLine(typeof(T));
}
}

string s= Hello, world;

TestMethod(s);

TestMethod2();

string s = Hello, world;

TestMethod(s);

class Class1 where U : IComparable {
protected U u1;
}

class Class2 : Class1 where U : IComparable {
public Class2(U t)
{
base.u1 = t;
}
}

class Class3:Class1{
public Class3(string u) {
base.u1 = u;
}
}

struct Struct1 {
public T t1;
public T t2;
}

Struct1 st;
st.t1 = 10.234;
st.t2 = 12.432;

public interface Iface {
void Set(T t);
T Get();
}

class Class6 : Iface {
private T t;
public void Set(string t) {
this.t=t;
}
public string Get() {
return t;
}
}

class Class7 : Iface { private T t; public void Set(T t) { this.t=t; } public T Get() { return t; } }

default

class TestClass{
public void Method(T t){
T t1=default(T);
Console.WriteLine(Value t={0}, t1={1},t, t1);
}

}

where T: struct

where T : class

where T : new()

where T :

where T :

where T : U

.

class TestClass where T : IComparable

.

public void TestMethod(U u1, U u2) where U: IComarable

+

-

/

*

class Class1 : IEnumerable {
T[] arr;
public Class1(T a) {
arr = new T[10];
for (int i = 0; i < 10; ++i)
arr[i] = a;
}

#region IEnumerable Members
public IEnumerator GetEnumerator(){
foreach (T t in arr)
yield return t;
}
#endregion

#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return arr.GetEnumerator();
}
#endregion

Class1 coll = new Class1("Test");
foreach (string cl in coll)
Console.WriteLine(cl);
IEnumerable ien = coll;
IEnumerator ienum = ien.GetEnumerator();
while (ienum.MoveNext())
Console.WriteLine(ienum.Current);

class Class1 : IComparable where T : Iomparable {
private T t1; private T t2;
public Class1(T t1, T t2) { this.t1 = t1; this.t2 = t2; }
public T T1 { set { t1 = value; } get { return t1; } }
public T T2 { set { t2 = value; } get { return t2; } }
public int CompareTo(Class1 t1)
{
return this.t1.CompareTo(t1.t1);
}
}

static void Main(string[] args) {
Class1[] arr = new Class1[5];
Random r=new Random();
for (int i = 0; i < 5; ++i)
arr[i] = new Class1(string.Format("{0}",i), string.Format("Value{0}",100-i*10));
Array.Sort(arr, new Comparison(Comp));
}

static int Comp(T t1, T t2)where T : Icomparable {
return t1.CompareTo(t2);
}

class Program where T : IComparable {
private T[] arr;
public Program(T t1) {
arr = new T[5];
for (int i = 0; i < 5; ++i)
arr[i] = t1;
}
public void Sort() {
Array.Sort(arr, new Comparison(Comp));
}
public int Comp(T t1, T t2) {
return t1.CompareTo(t2);
}
}

System.Collections.Generic

Collections Collections.Generic

ICollection

IComparer

IDictionary

IEnumerable

IEnumerator

IList

ICollection

interface ICollection : IEnumerable, IEnumerable

int Count { get; }

bool IsReadOnly { get; }

void Add(T item)

void Clear()

bool Contains(T item)

void CopyTo(T[] array, int arrayIndex)

bool Remove(T item)

IList

T this[int index] { get; set; }

int IndexOf(T item)

void Insert(int index, T item)

void RemoveAt(int index)

List

LinkedList

Stack

Queue

List

List()

List(IEnumerable collection)

List(int capacity)

int Capacity { get; set; }

int Count { get; }

T this[int index] { get; set; }

List

void Add(T item)

void AddRange(IEnumerable collection)

void Insert(int index, T item)

void InsertRange(int index, IEnumerable collection)

List

void Clear()

bool Remove(T item)

int RemoveAll(Predicate match)

void RemoveAt(int index)

void RemoveRange(int index, int count)

List

void Sort()

void Sort(Comparison comparison)

int Comparison(T x, T y)

void Sort(IComparer comparer)

void Sort(int index, int count, IComparer comparer)

List

List FindAll(Predicate match)

int FindIndex(Predicate match)

delegate bool Predicate(T obj)

int FindIndex(int startIndex, Predicate match)

T FindLast(Predicate match)

int FindLastIndex(Predicate match)

List

int FindLastIndex(int startIndex, Predicate match)

int IndexOf(T item)

int IndexOf(T item, int index)

int LastIndexOf(T item)

int LastIndexOf(T item, int index)

List

void ForEach(Action action)

delegate void Action(T obj)

LinkedList

LinkedList()

LinkedList(IEnumerable collection)

int Count { get; }

LinkedListNode First { get; }

LinkedListNode Last { get; }

LinkedList

void AddAfter(LinkedListNode node, LinkedListNode newNode)

LinkedListNode AddAfter(LinkedListNode node, T value)

void AddBefore(LinkedListNode node, LinkedListNode newNode)

LinkedListNode AddBefore(LinkedListNode node, T value)

LinkedList

void AddFirst(LinkedListNode node)

LinkedListNode AddFirst(T value)

void AddLast(LinkedListNode node)

LinkedListNode AddLast(T value)

LinkedList

void Remove(LinkedListNode node)

bool Remove(T value)

void RemoveFirst()

void RemoveLast()

LinkedList

LinkedListNode Find(T value)

LinkedListNode FindLast(T value)

LinkedListNode

public LinkedListNode(T value)

public LinkedList List { get; }

public LinkedListNode Next { get; }

public LinkedListNode Previous { get; }

public T Value { get; set; }

Stack

Stack()

Stack(IEnumerable collection)

Stack(int capacity)

int Count { get; }

Stack

T Peek()

T Pop()

void Push(T item)

Queue

Queue()

Queue(IEnumerable collection)

Queue(int capacity)

int Count { get; }

Queue

T Dequeue()

void Enqueue(T item)

T Peek()

IDictionary

ICollection Keys { get; }

ICollection Values { get; }

TValue this[TKey key] { get; set; }

void Add(TKey key, TValue value)

bool ContainsKey(TKey key)

bool Remove(TKey key)

bool TryGetValue(TKey key, out TValue value)

Dictionary

Dictionary()

Dictionary(IDictionary dictionary)

Dictionary(IEqualityComparer comparer)

Dictionary(int capacity)

Dictionary

int Count { get; }

Dictionary.KeyCollection Keys { get; }

Dictionary.ValueCollection Values { get; }

TValue this[TKey key] { get; set; }

Dictionary

void Add(TKey key, TValue value)

bool ContainsKey(TKey key)

bool ContainsValue(TValue value)

bool Remove(TKey key)

bool TryGetValue(TKey key, out TValue value)

IEnumerator

IEnumerator ienum= idic.GetEnumerator();

while(ienum.MoveNext())

Console.WriteLine(ienum.Current.Key);

foreach

foreach(KeyValuePair keyp in idic)

Console.WriteLine(ienum.Current.Key);

e-mail: [email protected]

CollectionCollectionBase

ComparerComparer

DictionaryHashTable

ListArrayList

QueueQueue

SortedDictionarySortedList

StackStack

LinkedListReadOnlyCollectionReadOnlyCollection

??? ??? (???)11.03.2009, 01:05:04 /