cs3505 5020 software practice ii
play

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


  1. CS3505/5020 Software Practice II Classroom Examples – Yield C#: Managed v. Disposable Homework tips CS 3505 L05 - 1

  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.

  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.

  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 obtain an OS (unmanaged) resource. When should it release it?

  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?

  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

  7. The ‘using’ statement � The using statement has this form: using (Type name = ref) { // Use object stored in name here } � It serves two purposes: – Creates a local scope for an IDisposable variable – Automatically calls Dispose on the variable when it goes out of scope.

  8. The ‘using’ statement � Example: using (SomeType c = new SomeType()) { c.ruleTheWorld(); } // c.Dispose() is called when it goes out of scope. // c does not exist here. � Another example – taking an XNA screenshot

  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

  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

  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

  12. Homework help � The remainder of the time is reserved for help with the homework: – Vectors – Drawing – Events – Etc.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend