CS3505/5020 Software Practice II A few C# goodies Homework Help - - PowerPoint PPT Presentation

cs3505 5020 software practice ii
SMART_READER_LITE
LIVE PREVIEW

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; []


slide-1
SLIDE 1

CS3505/5020 Software Practice II

A few C# goodies Homework Help Demos (vector math, tools for coding, etc)

CS 3505 L05 - 1

slide-2
SLIDE 2

CS 3505

Generics

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

time error int int i = intList[0]; i = intList[0]; // No cast required // No cast required L05 - 2

slide-3
SLIDE 3

CS 3505

Generics

Note, similar in syntax to C++

– But much, much different in implementation

Generics allow us to make classes that use

parameterized types

– Instead of using object (everything is an object) – Better type checking (adding a string to a List<int> doesn’t work) – No need to box and unbox for primitive types – Compile time type checking removes the need for run-time checks » This leads to better performance.

Generics apply to classes, interfaces,

structs, delegates and methods

– You can have generic methods without needing a generic class.

L05 - 3

slide-4
SLIDE 4

CS 3505

Generics

Types can be constrained The where keyword allows us to state that

the type that is bound to the type parameter must have the type of the where statement

– So, if we need to say that a type has to implement an interface, or extend a class, we can enforce that at compile time

We can also constrain a type parameter to

be a class

– Or interface, struct, etc.

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

slide-5
SLIDE 5

CS 3505

Generics

The most obvious use of generics is in

container or collection classes

– But there are plenty of other uses (IComparable<T> is a good example)

Generics are not quite the same as template

classes in C++

– Generics are only types. C++ can have primitive types as template parameters. » (template class C<typename T, int n>) – Rules for generics are simpler than C++ – C# can try to infer the type of a parameterized type (C++ can not).

string[] names = string[] names = Utils.CreateArray Utils.CreateArray<string>("", 10); <string>("", 10); int int[] numbers = [] numbers = Utils.CreateArray Utils.CreateArray< <int int>( >(-

  • 1, 100);

1, 100); string[] names = string[] names = Utils.CreateArray Utils.CreateArray("", 10); ("", 10); int int[] numbers = Utils.CreateArray( [] numbers = Utils.CreateArray(-

  • 1, 100);

1, 100); L05 - 5

slide-6
SLIDE 6

CS 3505

Generics and Java 1.5

Java 1.5 did add generics, but there are

some differences

– Java 1.5 made no changes to the VM. As such… – Java 1.5 generics can’t have primitive types as a parameterized

  • type. (no List<int> in Java 1.5)

– Generic information “erased at compile-time” (no generics in reflection, complied classes) – No gain in performance over “object-based” collections.

L05 - 6

slide-7
SLIDE 7

CS 3505

Generic Summary

Generics take a bit to get used to, but it’s

not too bad.

– Take advantage of the new generic collection classes when you can » System.Collections.Generic – Generic versions in a different namespace than the older versions » Backward compatibility for older codes

L05 - 7

slide-8
SLIDE 8

CS 3505

Partial Classes

After the complexity of generics, partial

classes are easy.

Partial class just allow you to have the code

for one class in multiple files

– Instead of just one.

Why is this useful?

L05 - 8

slide-9
SLIDE 9

CS 3505

Partial Classes

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

  • r 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

  • r der ) {
  • r der ) {
  • r der s. Add( or der
  • r der s. Add( or 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

  • r 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

  • r 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

  • r der ) {
  • r der ) {
  • r der s. Add( or der
  • r der s. Add( or 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

  • r der s. Count

> 0; > 0; } } } }

L05 - 9

slide-10
SLIDE 10

Partial Classes

Are partial classes good to use?

– Where are your class methods?

Some engineering soundness lost for

convenience.

– Use at your own risk.

slide-11
SLIDE 11

Aside Delegate References

In 1.x version of C# we would add to a

delegate as:

– foo += new delegatename(functionbar);

In the 2.x version the compiler is able to “do

the right thing”

– foo += functionbar;

CS 3505 L05 - 11

slide-12
SLIDE 12

CS 3505

Nullable Types

Sometimes, we need to say that

something has no value

– This is fairly easy with objects, because we can use ‘null’ for no value. – But, this is very hard with primitive types.

For this, we can use nullable types

– Nullable types are a great match for databases, because many databases use nulls. – Nullable types can only be value types (primitive types/structs)

The syntax is pretty simple

– int? is a nullable integer. – Nullable<T> makes a nullable type out of any value type (primitive or struct) » Will not work with reference types (classes).

L05 - 12

slide-13
SLIDE 13

Structs and Classes

Are structs a good idea? Consider Vector2:

– Vector2 a, b; – a = new Vector2(3, 20); – b = a; – a.Normalize();

What is b?

slide-14
SLIDE 14

CS 3505

Iterators

To move through collections, we need to

support an Enumerator

Let’s look at an example. First, a simple list

class.

– Note we use a ListEnumerator helper class.

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

slide-15
SLIDE 15

CS 3505

Enumerator: Old Version

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;

1; } } publ i c publ i c bool bool M

  • veNext

M

  • veNext ( ) {

( ) { 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

slide-16
SLIDE 16

CS 3505

List (New Version)

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

slide-17
SLIDE 17

CS 3505

Iterators and the Yield Statement

  • Okay. How can this possibly work?

– It looks like we are returning just an item of the list. » But we loop over the whole thing. » Well, actually, it looks like we just return the first item in the

  • list. The loop doesn’t make sense.

– But, we have a yield return instead of a plain return. » Hum…

The magic is in the yield.

– You just write the iterator as if you were walking through all the elements. » In this case, we are looping through the array. – But, for each value of the container you visit, you make a yield return statement that “returns” that value. – The C# compiler does the rest for you.

L05 - 17

slide-18
SLIDE 18

CS 3505

Iterators

The yield statement is pretty powerful stuff

– But once you get it, it’s very, very useful.

But, what about the magic?

– 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.

For now, this is limited to creating Enumerators

– 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

slide-19
SLIDE 19

Java 1.5

Java 1.5 added some features to Java 1.4

– Generics (similar to C#) – Annotations (somewhat like Attributes in C#) – Enumerated Types (different than C#, enums are classes) – Boxing and Unboxing (no more casting to/from int to Integer) – for/in statement (much like the foreach statement)

Yes, you could say that a lot of this was

“borrowed” from C#

– But that’s how languages evolve.

CS 3505 L05 - 19

slide-20
SLIDE 20

Project #2 – Pinball simulation

Simply a simulation for this part of the

project.

– Project #3 will implement game-like features

Must use XNA!

– Use the supplied update/draw paradigm

Additional C# practice (and XNA warmup)

slide-21
SLIDE 21

Project #2 - Engineering

There are several steps to this project:

– We will identify these in classroom discussion (and edit this slide)

slide-22
SLIDE 22

Project #2

Automate some programming decisions

where appropriate.

– You have a window, you can draw a sprite at the mouse location. Why would you ever position or locate elements by hand?

Larger products often require programmers

to create their own productivity tools

– One-off tools are common. Write them, use them and throw them away.

slide-23
SLIDE 23

Project #2 - Artwork

Choose:

– Coordinate system – Sprite organization – Drawing / artwork takes time. Plan ahead. (Good artwork takes a lot of time!)

(Classroom discussion filled out these

topics.)

slide-24
SLIDE 24

Project #2 - Logic

How will you organize your sprites (and

physical objects)?

Where will the game logic go? Do objects update themselves, or is there

another class where positions are changed?

Ramifications…

slide-25
SLIDE 25

Project #2 - Collisions

Collisions are the heart of this project.

– Without good collisions, you just have bad code. – Devote time to getting these right: » Expect several generations of collision code » Some vector math required, use Vector2 for support

slide-26
SLIDE 26

Project #2 - Collisions

A few vector routines will help you greatly:

– Distance from object to point – Reflection » Elastic » Energy adding (bumpers) » Energy absorbing (sides, other rigid surfaces) – Direction » Is the ball moving away from an object?

Note: my code makes some assumptions that may be incorrect,

but you’ll rarely see them.

slide-27
SLIDE 27

Demos

To finish, I’ll do some vector math and

demos.