CS3505/5020 Software Practice II Classroom Examples Yield C#: - - PowerPoint PPT Presentation

cs3505 5020 software practice ii
SMART_READER_LITE
LIVE PREVIEW

CS3505/5020 Software Practice II Classroom Examples Yield C#: - - PowerPoint PPT Presentation

CS3505/5020 Software Practice II Classroom Examples Yield C#: Managed v. Disposable Homework tips CS 3505 L05 - 1 Yield demo Key ideas: An iterator block exists in a method with an enumerable return type. The method is called


slide-1
SLIDE 1

CS3505/5020 Software Practice II

Classroom Examples – Yield C#: Managed v. Disposable Homework tips

CS 3505 L05 - 1

slide-2
SLIDE 2

Yield demo

Key ideas:

– An iterator block exists in a method with an enumerable return type. – The method is called an iterator. – The iterator block yields values one at a time through the iteration mechanism. – A foreach loop can then iterate over the values yielded from the iterator method.

Classroom demo – see source code.

slide-3
SLIDE 3

Resource management in C#

Managed code

– Heap pointers cannot be acquired except through ‘new’ or reference copy – Garbage collector determines those references that are not accessible – cleans them up – Garbage collection is not deterministic – can manually initiate, still not guaranteed results. – For the heap, this is sufficient.

slide-4
SLIDE 4

Resource management in C#

Not all resources are memory resources

– File handles (OS) – Graphics object (Direct X, etc.) – Network handles (OS) – Database connections (OS/app) – Locks, etc. (OS)

A managed resource (heap object) may

  • btain an OS (unmanaged) resource. When

should it release it?

slide-5
SLIDE 5

Resource management in C#

An object may persist for a long time due to

garbage collection uncertainty.

The IDisposable interface was created to

mark classes that may hold unmanaged resources.

If an object is IDisposable, its Dispose

message should be called to release unmanaged resources.

When?

slide-6
SLIDE 6

Resource management in C#

Call Dispose only when:

– You are done with the object – You are sure there are no still-dependent references to the object – The object is about to go out of scope

Failure to dispose of IDisposable objects

can lead to resource leaks

slide-7
SLIDE 7

The ‘using’ statement

The using statement has this form: It serves two purposes:

– Creates a local scope for an IDisposable variable – Automatically calls Dispose on the variable when it goes

  • ut of scope.

using (Type name = ref) { // Use object stored in name here }

slide-8
SLIDE 8

The ‘using’ statement

Example: Another example – taking an XNA

screenshot

using (SomeType c = new SomeType()) { c.ruleTheWorld(); } // c.Dispose() is called when it goes out of scope. // c does not exist here.

slide-9
SLIDE 9

Peter’s Engineering tip of the day

Development of any significantly sized

process:

– involves daily code generation – involves changing views of low-level program structure – forgetfulness (and lack of foresight) leads to cumbersome solutions

Refactor every day

slide-10
SLIDE 10

Peter’s Engineering tip of the day

Refactor every day

– Clarify your variable usage – Rename elements as appropriate – Consider new classes or new method arrangements – Don’t add features during this process » You should still be able to pass your tests afterwards – Don’t let refactoring consume all your time » Perfection is unattainable – just make it good

slide-11
SLIDE 11

Peter’s Engineering tip of the day

Refactor every day

– Your own knowledge about your code will be refreshed – Coding in a ‘clean’ program is quicker and easier. – Bugs and errors may surface during this process. » Better now than later – You may break something

slide-12
SLIDE 12

Homework help

The remainder of the time is reserved for

help with the homework:

– Vectors – Drawing – Events – Etc.