Monday, 18 May 2015

Control Flow


Branching

If - else if - else


or for shorthand there's the turnary operator   ? : string pass = score >80? "Yes" : "No"

The values in the result (strings) must match the type of variable being assigned to.

Switch

Restricted to Integers, Characters, Strings and Enums
No "fall throughs" - must have a break;
Case labels are actually constants (hence the restriction)
Default label is optional

switch(name)
{
  case "Name1":
     DoThis();
     break;
  case "Name2":
     DoThat();
     break
  default:
     DoTheOther();
     break; // Even need this break!
}

Can stack multiple cases that lead to the same code.

Iterating

for, while, do-while, foreach

for, and while both execute 0 or more times
do-while executes at least once.

foreach iterates over a collection.   Compiler is looking for a GetEnumerator (that is derived from IEnumerator) method on a type.  If found you can use foreach to iterate over it.
While in a foreach loop you cannot modify the collection.


Jumping

break; continue; goto; return; throw
break - can use from within any loop/iteration (e.g. for or while) as well as selection like switch.
continue - similar but it doesn't exit the loop it just goes to the next iteration (used within loops)
goto - go to somewhere else in code marked with a label.

return - can be used in a void method (just return statement rather than return <variable>)
yield return - lazily build up a collection - return type must be IEnumerable.  Does a continuation.  Can return one item to the calling app, which does some processing then calls into sub routine again.  The subroutine picks up where it left off, e.g. in the middle of a loop.  Handy if the time taken to produce the whole list is long - it returns each result as it is calculated rather than making the caller wait until it is all completed.

yield break - ends the IEnumerable.

Exceptions

Throw

Used to raise an exception.  Type safe, structured error handling
Runtime unwinds the stack until it finds a handler.  May terminate an app if no suitable handler is found.  (ASP.Net won't terminate the webserver.  It catches the exception - optionally returning an error page - so that the web server doesn't get torn down).

Try Catch

Don't need to name a variable when catching an exception, i.e. this is ok:-
try
{
  DoSomething();
}
catch (Exception)
{
}

but you can't get any info from the exception unless you name the exception variable.

Catching System.Exception catches everything - except a few special exceptions.
Chain catch blocks with most specific first.

finally verses using statements
Using statements are another way of ensuring resources are cleaned up properly even if an exception occurs.  However the Type of variable within the using block must expose the IDisposable interface

Re-throwing Exceptions

You can catch and rethrow an exception - either the original or a different exception.
keyword throw;  

You could type in throw ex but that would show the exception as originating in your catch block.  Essentially you've thrown a new exception of the same type.

You could throw an exception of a new type, e.g. for security reasons.  Catch database errors but throw a general exception error to users.  It may be a custom, more meaningful exception type.

Custom Exceptions

Derive from a common base exception, e.g. System.Exception
Classname - suffix of Exception
Make it serializable (Serializable decorator and special constructor that uses serialization information)

Insert Snippet -> Visual C# -> Exception 
and type and tab information I need.







No comments:

Post a Comment