Writelines statements foreach statement iteration of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: collections Created by implementing IEnumerable foreach (Customer c in customers.OrderBy("name")) { if (c.Orders.Count != 0) { ... } } Statements Jump Statements break Exit inner-most loop continue End iteration of inner-most loop goto <label> Transfer execution to label statement return [<expression>] Exit a method throw See exception handling Statements Exception Handling Exceptions are the C# mechanism for handling unexpected error conditions Superior to returning status values Can’t be ignored Don’t have to handled at the point they occur Can be used even where values are not returned (e.g. accessing a property) Standard exceptions are provided Statements Exception Handling try...catch...finally statement try block contains code that could throw an exception catch block handles exceptions Can have multiple catch blocks to handle different kinds of exceptions finally block contains code that will always be executed Cannot use jump statements (e.g. goto) to exit a finally block Statements Exception Handling throw statement raises an exception An exception is represented as an instance of System.Exception or derived class Contains information about the exception Properties Message StackTrace InnerException You can rethrow an exception, or catch one exception and throw another Statements Exception Handling try { Console.WriteLine("try"); throw new Exception(“message”); } catch (ArgumentNullException e) { Console.WriteLine(“caught null argument"); } catch { Console.WriteLine("catch"); } finally { Console.WriteLine("finally"); } Statements Synchronization Multi-threaded applications have to protect against concurrent access to data Must prevent data corruption The lock statement uses an instance to provide mutual exclusion Only one lock statement can have access to the same instance Actually uses the .NET Framework System.Threading.Monitor class to provide mutual exclusion Statements Synchronization public class CheckingAccount { decimal balance; public void Deposit(decimal amount) { lock (this) { balance += amount; } } public void Withdraw(decimal amount) { lock (this) { balance -= amount; } } } Statements using Statement C# uses automatic memory management (garbage collection) Eliminates most memory management problems However, it results in non-deterministic finalization No guarantee as to when and if object destructors are called Statements using Statement Objects that need to be cleaned up after use should implement the System.IDisposable interface One method: Dispose() The using statement allows you to create an instance, use it, and then ensure that Dispose is called when done Dispose is guaranteed to be called, as if it were in a finally block Statements using Statement public class MyResource : IDisposable { public void MyResource() { // Acquire valuble resource } public void Dispose() { // Release valuble resource } public void DoSomething() { ... } using (MyResource r = new MyResource()) { } r.DoSomething(); } // r.D...
View Full Document

Ask a homework question - tutors are online