c-jumpstart module 3.pdf

Upload: edmundo-lozada

Post on 02-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 C-Jumpstart Module 3.pdf

    1/24

    03 | Features of C#, Part 2Jerry Nixon | Microsoft Developer EvangelistDaren May | President & Co-founder, Crank211

  • 7/27/2019 C-Jumpstart Module 3.pdf

    2/24

    Controlling Programmatic Flow Manipulating Types and Strings

    Module Overview

  • 7/27/2019 C-Jumpstart Module 3.pdf

    3/24

    MVA

    Controlling Programmatic Flow

  • 7/27/2019 C-Jumpstart Module 3.pdf

    4/24

    Many statements impact program flow:

    Selection statements if, else, switch

    Iteration statements do, for, foreach, in, while

    Jump statements

    break, continue, default, goto, return, yield

    Controlling Flow

  • 7/27/2019 C-Jumpstart Module 3.pdf

    5/24

    Selection statements evaluate Booleanexpressions and direct execution If statements can be nested within otherif statements.

    Selection Statements : if

  • 7/27/2019 C-Jumpstart Module 3.pdf

    6/24

    The ternary or conditional operator canbe used as if statement shorthand.

    Selection Statements :ternary

  • 7/27/2019 C-Jumpstart Module 3.pdf

    7/24

    Switch (008)

  • 7/27/2019 C-Jumpstart Module 3.pdf

    8/24

    while and do-while statements executea body of code if the expressionevaluates to true. while evaluates the expression before

    executing the body, so the body mayexecute 0 or more times.

    do-while evaluates the expression after thefirst execution of the body, so the body

    executes at least once

    Iteration Statements: while, do-while

  • 7/27/2019 C-Jumpstart Module 3.pdf

    9/24

    for loops are similar to while loops for loops include clauses that executebefore the loop begins and after everyiteration:

    Initialization clause typically used toinitialize one of more loop variables

    Iteration clause typically used to update

    the loop variable

    Iteration Statements: for

  • 7/27/2019 C-Jumpstart Module 3.pdf

    10/24

    Foreach loop iterates over each elementin an enumerable object Array, Collection, List

    Iteration Statements: foreach

  • 7/27/2019 C-Jumpstart Module 3.pdf

    11/24

    Jump statements redirect execution

    breakends a loop or exits a switch continue skips a loop iteration and starts the next iteration

    goto transfers execution to a position marked by a label

    return exits a method

    throwraises an exception

    Jump Statements

  • 7/27/2019 C-Jumpstart Module 3.pdf

    12/24

    Iteration, break and continue (010)

  • 7/27/2019 C-Jumpstart Module 3.pdf

    13/24

    MVA

    Manipulating Types

  • 7/27/2019 C-Jumpstart Module 3.pdf

    14/24

    Casting allows us to work with types in a general sensbase object or as an instance of an interface impleme

    We can explicitly attempt to cast an object to anothe An advantage of strong typing is that the compiler often k

    cast is possible. It doesnt always know.

    Compilation will fail if the compiler detects an invalid cast.

    But, what about scenarios that the compiler cant det

    Casting Types

  • 7/27/2019 C-Jumpstart Module 3.pdf

    15/24

    In this scenario, instances of Class2 andClass3 can be cast an instance of Class1. However, an instance of Class2 cannever be cast to Class3. If Class2 is cast to Class1, can it then becast to Class3?

    Casting Types

  • 7/27/2019 C-Jumpstart Module 3.pdf

    16/24

    C# provides us with the is operator

    returns true if an object is an instance of a type The as operator attempts to cast an object to a speci

    returning the instance cast to the type

    null if not possible

    does not raise an exception

    Is and As

  • 7/27/2019 C-Jumpstart Module 3.pdf

    17/24

    Casting Demo

  • 7/27/2019 C-Jumpstart Module 3.pdf

    18/24

    MVA

    Manipulating Strings

  • 7/27/2019 C-Jumpstart Module 3.pdf

    19/24

    A string object is an immutable (unchangeable) sequcharacters.

    Any method that manipulates a string, actually returnstring. The StringBuilder class provides a mutable implemen

    string.

    What is a string?

  • 7/27/2019 C-Jumpstart Module 3.pdf

    20/24

    StringBuilder (012)

  • 7/27/2019 C-Jumpstart Module 3.pdf

    21/24

    The string class provides manymethods for manipulating strings Bear in mind that new stringobjects are returned for:

    Replace

    ToUpper

    Concat

    String Manipulation

  • 7/27/2019 C-Jumpstart Module 3.pdf

    22/24

    Regular expressions are a specialized syntax to find apatterns in strings

    Regular Expressions

    M d l R

  • 7/27/2019 C-Jumpstart Module 3.pdf

    23/24

    Controlling Programmatic Flow Manipulating Types and Strings

    Module Recap

  • 7/27/2019 C-Jumpstart Module 3.pdf

    24/24

    2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trathe U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee tprovided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTA