CS3505/5020 Software Practice II
A few C# goodies Homework Help Demos (vector math, tools for coding, etc)
CS 3505 L05 - 1
CS3505/5020 Software Practice II A few C# goodies Homework Help - - PowerPoint PPT Presentation
CS3505/5020 Software Practice II A few C# goodies Homework Help Demos (vector math, tools for coding, etc) CS 3505 L05 - 1 Generics public class List< < T T > > public class List { { private T private T [] elements; []
CS 3505 L05 - 1
CS 3505
public class List public class List< <T T> > { { private private T T[] elements; [] elements; private private int int count; count; public void public void Add( Add(T T element) { element) { if (count == if (count == elements.Length elements.Length) ) Resize(count Resize(count * 2); * 2); elements[count elements[count++] = element; ++] = element; } } public public T T this[int this[int index] { index] { get { return get { return elements[index elements[index]; } ]; } set { set { elements[index elements[index] = value; } ] = value; } } } public public int int Count { Count { get { return count; } get { return count; } } } } } List List< <int int> > intList intList = new List = new List< <int int> >(); (); intList.Add(1); intList.Add(1); // No boxing // No boxing intList.Add(2); intList.Add(2); // No boxing // No boxing intList.Add("Three intList.Add("Three"); "); // Compile // Compile-
time error int int i = intList[0]; i = intList[0]; // No cast required // No cast required L05 - 2
CS 3505
L05 - 3
CS 3505
class Dictionary<K,V>: class Dictionary<K,V>: IDictionary IDictionary<K,V> <K,V> where K: where K: IComparable IComparable<K> <K> where V: where V: IKeyProvider IKeyProvider<K> <K> { { public void public void Add(K Add(K key, V value) { key, V value) { ... ... } } } L05 - 4
CS 3505
string[] names = string[] names = Utils.CreateArray Utils.CreateArray<string>("", 10); <string>("", 10); int int[] numbers = [] numbers = Utils.CreateArray Utils.CreateArray< <int int>( >(-
1, 100); string[] names = string[] names = Utils.CreateArray Utils.CreateArray("", 10); ("", 10); int int[] numbers = Utils.CreateArray( [] numbers = Utils.CreateArray(-
1, 100); L05 - 5
CS 3505
L05 - 6
CS 3505
L05 - 7
CS 3505
L05 - 8
CS 3505
publ i c publ i c par t i al par t i al cl ass Cust om er cl ass Cust om er { { pr i vat e pr i vat e i nt i nt i d; i d; pr i vat e st r i ng nam e; pr i vat e st r i ng nam e; pr i vat e st r i ng addr ess; pr i vat e st r i ng addr ess; pr i vat e Li st <O r der s> pr i vat e Li st <O r der s> or der s
; } } publ i c publ i c par t i al par t i al cl ass Cust om er cl ass Cust om er { { publ i c voi d publ i c voi d Subm i t O r der ( O r der Subm i t O r der ( O r der
) ; } } publ i c publ i c bool bool HasO ut st andi ngO r der s HasO ut st andi ngO r der s( ) { ( ) { r et ur n r et ur n or der s. Count
> 0; > 0; } } } } publ i c cl ass Cust om er publ i c cl ass Cust om er { { pr i vat e pr i vat e i nt i nt i d; i d; pr i vat e st r i ng nam e; pr i vat e st r i ng nam e; pr i vat e st r i ng addr ess; pr i vat e st r i ng addr ess; pr i vat e Li st <O r der s> pr i vat e Li st <O r der s> or der s
; publ i c voi d publ i c voi d Subm i t O r der ( O r der Subm i t O r der ( O r der
) ; } } publ i c publ i c bool bool HasO ut st andi ngO r der s HasO ut st andi ngO r der s( ) { ( ) { r et ur n r et ur n or der s. Count
> 0; > 0; } } } }
L05 - 9
CS 3505 L05 - 11
CS 3505
L05 - 12
CS 3505
publ i c cl ass Li st publ i c cl ass Li st { { i nt er nal obj ect [ ] el em ent s; i nt er nal obj ect [ ] el em ent s; i nt er nal i nt er nal i nt i nt count ; count ; publ i c publ i c I Enum er at or I Enum er at or G et Enum er at or G et Enum er at or ( ) { ( ) { r et ur n new r et ur n new Li st Enum er at or ( t hi s Li st Enum er at or ( t hi s) ; ) ; } } } }
L05 - 14
CS 3505
publ i c cl ass publ i c cl ass Li st Enum er at or Li st Enum er at or : : I Enum er at or I Enum er at or { { Li st Li st l i st l i st ; ; i nt i nt i ndex; i ndex; i nt er nal i nt er nal Li st Enum er at or ( Li st Li st Enum er at or ( Li st l i st ) { l i st ) { t hi s. l i st t hi s. l i st = l i st ; = l i st ; i ndex = i ndex = -
1; } } publ i c publ i c bool bool M
M
( ) { i nt i nt i = i ndex + 1; i = i ndex + 1; i f ( i >= i f ( i >= l i st . count l i st . count ) r et ur n f al se; ) r et ur n f al se; i ndex = i ; i ndex = i ; r et ur n t r ue; r et ur n t r ue; } } publ i c obj ect Cur r ent { publ i c obj ect Cur r ent { get { r et ur n get { r et ur n l i st . el em ent s[ i ndex l i st . el em ent s[ i ndex] ; } ] ; } } } } }
L05- 15
CS 3505
publ i c cl ass Li st publ i c cl ass Li st { { i nt er nal obj ect [ ] el em ent s; i nt er nal obj ect [ ] el em ent s; i nt er nal i nt er nal i nt i nt count ; count ; publ i c publ i c I Enum er at or I Enum er at or G et Enum er at or G et Enum er at or ( ) { ( ) { f or ( f or ( i nt i nt i = 0; i < count ; i ++) { i = 0; i < count ; i ++) { yi el d r et ur n yi el d r et ur n el em ent s[ i el em ent s[ i ] ; ] ; } } } } } }
L05 - 16
CS 3505
L05 - 17
CS 3505
– But once you get it, it’s very, very useful.
– Well, what happens is that the compiler makes an internal class for you that does the right thing. – For now, just accept the magic and be happy.
– The class generated implements the IEnumerator<T> interface. – But, if you take a liberal view of what IEnumerator and GetNext is for, you can do some pretty creative stuff.
publ i c cl ass Test publ i c cl ass Test { { publ i c publ i c I Enum er at or I Enum er at or G et Enum er at or G et Enum er at or ( ) { ( ) { yi el d r et ur n yi el d r et ur n “ “ hel l o hel l o” ” ; ; yi el d r et ur n yi el d r et ur n “ “ wor l d wor l d” ” ; ; } } } }
Check this out!! L05 - 18
CS 3505 L05 - 19
Note: my code makes some assumptions that may be incorrect,
but you’ll rarely see them.