Learn Tiny Bit of C# in 7 days-Day1

download Learn Tiny Bit of C# in 7 days-Day1

of 22

Transcript of Learn Tiny Bit of C# in 7 days-Day1

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    1/22

    Learn C# in 7 Days – Day 1

    Introduction: As the title specifes “Learn C# in 7 days” I will be writin

    article !or the sa"e the intended !ocus is towards the beinners so that they

    can easily rasp the C# Lanuae concepts and beco"e C# de$eloper a!terreadin the articles% &o let's et started with Day 1% In case you !eel I ha$e

    le!t the $ery i"portant topic !eel !ree to co""ent and any correcti$e

    "easures and i"ple"entation are "ost welco"ed%

      Day1 Contents

    1% Introduction(% Aenda)% Creatin *our +irst ,rora"-% Console ,rora"s.% /scape &e0uencin

    % /2ceptional 3andlin7% Arrays4% +or /ach Loop5% ,arallel%+oreachLoop

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    2/22

    Introduction

    C# is ob6ect oriented prora""in lanuae which allows us to build

    lare $ariety applications% It was de$eloped by icroso!t within %8/9

    !ra"ewor% C# is one o! the "any lanuaes that are supported by

    %8/9 !ra"ewor% Its de$elop"ent tea" is led by Anders 3e6lsber% 9he

    Current $ersion is C# %; which was released on isual &tudio you can download !ro" the

    below lin https:www%$isualstudio%co" %Step2. Create New Project Clic! on File"# New"# Project

    https://en.wikipedia.org/wiki/Anders_Hejlsberghttps://www.visualstudio.com/https://www.visualstudio.com/https://en.wikipedia.org/wiki/Anders_Hejlsberg

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    3/22

    Step$% Select Visual C& *ou can see the !ollowin set o! options a$ailable with >isual C# usin

    these co"ponents we can de$elop our respecti$e applications%

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    4/22

    Biht now we will select Console 'pplication and na"e our ,ro6ect as

    C&harp&tepy&tep% ?e will i"ple"ent the C # concepts usin Console

    'pplication because it si"plifes the learnin process o! C#% @nce the

    loadin o! classes and re0uired co"ponent need !or $isual studio pro6ect et

    loaded we will ha$e our ,rora"%cs fle as shown below:

    usin &yste"usin &yste"%Collections%Eenericusin &yste"%Lin0usin &yste"%9e2tusin &yste"%9hreadin%9ass

    na"espace C&harp&tepy&tepF  class ,rora"  F  static $oid ainGstrinH arsJ  F

      K  KK

    I will be ettin rid o! little code as it's our frst war" up topic I don't want

    you to "ess up and con!use% In short I will only eep the code that is

    necessary%

    usin &yste"

      class ,rora"  F  static $oid ainGJ

      F

      K  K

     

    8ow you can see I ha$e re"o$ed so"e code that will tal in our later section%

    &o now I want to print ?elco"e to Learn C# &tep y &tep in Console window%

    y console window I "ean to say is Command Prompt% &o in order to do

    that I will 6ust write Console.(rite)ine*+message,-.  % 3ere Console is a

    class% ?riteLine is used to write the "essae or data to the screen or the ser%Console%Beadline is used to tae input !ro" the ser until and unless user presses the

    input !ro" the eyboard the screen waits !or the input%

    usin &yste"

      class ,rora"  F  static $oid ainGJ  F  Console%?riteLineGMLearn C# &tep by &tepMJ  K

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    5/22

      K

    &o once I run "y application in debu "ode the co""and window 6ust pops up and ets closed

    in order to eep the window open we use one "ore "ethod o! Console called Beadey which will

    not close the console window until and unless the user presses a ey or you can run the prora"

    usin ctrlN!. run without debuin%

    usin &yste"

      class ,rora"  F  static $oid ainGJ  F  Console%?riteLineGMLearn C# &tep by &tepMJ  Console%BeadOeyGJ  K  K

    Let's !ocus little in depth !or the abo$e code to understand it better%

    sing Sytem%

     9his line sinifes that in our prora" we are oin to "ae use o! &yste"

    8a"espace% &o you "iht be thinin what the hell 8a"espace is%

    Namespace is a collection o! classes when we use our Console%?riteLine or

    Console%BeadLine you "ay ha$e seen the !ollowin

     9hat speci!y the Console class is present in the &yste" 8a"espace i! we

    re"o$e the sin &yste" 8a"espace we would et co"pile error

     9hat "eans co"piler don't now where do Console e2ist% ?e can add the

    !ully con$entional eyword &yste"%Console class na"e be!ore usin its

    !unction as shown below

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    6/22

      class ,rora"  F  static $oid ainGJ  F  &yste"%Console%?riteLineGMLearn C# &tep by &tepMJ  &yste"%Console%BeadLineGJ  K

    Kut doin this we ha$e to use &yste" where$er we want to use the Console Class in order to use&yste" class so in order to a$oid this we use the" at top in sin &ection%

    /$ery lines o! Code should be inside a Class we will discussin about class

    once we o ahead%

      class ,rora"  F  static $oid ainGJ  F  &yste"%Console%?riteLineGMLearn C# &tep by &tepMJ

      &yste"%Console%BeadLineGJ  KK

    Static Void /ain*-

    0s an entry point unction wic tells te compiler rom were te e3ecution is going

    to start.

    Once te program get e3ecuted it4s enter te entry point unction and e3ecute te

    sets o instructions.

    56ip7 (en you want to see te 8ow o program you can insert a 9rea!point using F:

    and see your program 8ow line 9y line.

    Console ,rora"sIn this topic we will be enhancin our frst prora" by tain input

    !ro" the ser and displayin the desired results to the ser%As we ha$e learnt to write to Console we write Console%?riteLine so

    when we want to read the ser Input we use Consol%BeadLineGJ 9his

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    7/22

    !unction reads the ne2t lines o! characters !ro" the standard input

    strea" as shown abo$e in fure% In order to read the ser Input we

    ha$e to read the ser Input in a $ariable 5note $ariable is a location in

    "e"ory which is used to store data used in Application% @nce the

    $alue is stored in $ariable we will display the ser entered $alue

    concatenated with to Learn C #&tep by &tep as shown below:

    usin &yste"

      class ,rora"  F  static $oid ainGJ  F  Console%?riteLineGMLearn C# &tep by &tepMJprintin to the Console  Console%?riteLineGM/nter your 8a"eMJprintin to the Console  strin ser8a"e P Console%BeadLineGJBeadin ser Input  Console%?riteLineGM?elco"e MNser8a"eN M to Learn C# &tep by &tepMJprintin user

    entered concatenated with other strin%  Console%BeadLineGJ

     

    K

     K

    Let o throuh code

    As we can see that Console%?riteLine has been printed to the console

    ?indow now yellow shaded lines read the ser Input stores it into

    $ariable called ser8a"e and in ne2t line we write the $ariable $alue

    alon with strin as shown below%

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    8/22

      C& is a case sensiti;e language i.e.

     9he word Case sensiti$e "eans the te2t is sensiti$e to capitaliQation o!letters or words% e2%Class and class are two diRerent word because o! C o! frst bein Caps% *oucan see the below fure !or "ore elaboration%

    added

    >ariable

    Is the s"all "e"ory data which are allocated to our prora" where actual data will be stored%

    In below e2a"ple we are usin a &trin $ariable to tae the input !ro" the ser and display the

    sa"e to the screen%

    usin &yste"

    na"espace readlineF  class ,rora"  F  static $oid ainGstrinH arsJ  F  Console%?riteLineGM/nter your 8a"eMJ  strin user8a"e P Console%BeadLineGJ  Console%?riteLineGM*our 8a"e is F;KMuser8a"eJ

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    9/22

      Console%BeadOeyGJ

      K  KK

    Console%?riteLineGM*our 8a"e is F;KMuser8a"eJ

    3ere we are concatenate “ *our 8a"e is” and user8a"e $alue

    IntLet tae up that we ha$e another re0uire"ent o! Ae as we all now that ae is aninteer type% &o we ha$e to tae inteer as a $ariable% ?hen we try to read the

    input !ro" the ser as shown below the error appears

    As the error specifes cannot con$ert strin to a inteer whene$er we tae input

    !ro" the user it's is always treat as a strin so we need to con$ert it to our re0uired

    datatype%

    usin &yste"

    na"espace readlineF  class ,rora"  F  static $oid ainGstrinH arsJ

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    10/22

      F  Console%?riteLineGM/nter your 8a"eMJ  strin user8a"e P Console%BeadLineGJ  Console%?riteLineGM/nter your aeMJ  int userAe PCon$ert%9oInt1G Console%BeadLineGJJ  Console%?riteLineGM*our 8a"e is F;K and your ae is F1KMuser8a"euserAeJ  Console%BeadOeyGJ

      K  KK

     9he output o! the prora" i! users inputs correct inteer $alue%

    8ow i! I try insertin the nonSinteer $alue I will !ace an e2ceptionerror as shown

    below “Input strin was not in a correct !or"at”% &o now to learn about /2ceptional

    3andlin we will learn ahead in this article%

    added

    /scape &e0uencin&o"eti"es we need to add Character co"binations consistin o! a bacslash

    GTJ !ollowed by a letter in our prora"s e% ?e want our output to ha$e been

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    11/22

    lie this +

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    12/22

    &o in order to do /2ceptional 3andlin we need two thins%

     9ry Catch and fnally we can see in abo$e fure !ro" where our /2ception

    was raised or prora" crashed% &howin the actual unhandled e2ception will

    annoy the user as they are cryptic and do not "ae sense to the end user%

    ?e can tell the co"piler that I a" e2ceptin to et error in this line and in

    case you et e2ception can you 6ust throw the /2ception into the Catch as

    shown below%

    usin &yste"

      class ,rora"  F  static $oid ainGJ  F  int a P 1;  int b P ;  int result  try F

    result Pab  Console%?riteLineGMTn Besult a!ter di$ision is MresultJ  Console%BeadLineGJ  K  catchG/2ception e2J  F  Console%?riteLineGMDi$isor should be reater than ;MJ  Console%BeadLineGJ  K

      K

     K

    /2ception is actually a class that deri$es !ro" &yste"%/2ception class% ?ith

    the help o! this we can show the ser +riendly "essae to the sers rather

    than Co"piler "essae shown by the co"piler%

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    13/22

    An e2ception Class has se$eral use!ul properties Gwe will learn "ore about

    properties as we o aheadJ that pro$ide $aluable in!or"ation about the/2ception%

    • essae: Describes the current e2ception details

    • &tac9race: ,ro$ide the call stac to the line nu"ber in the "ethod

    where the e2ception occurred%

    ?hat i! rather than usin /2ception class we use Di$ideyUero/2ception which isa child class o! /2ception% &yste"%/2ception is base class o! /2ception

    • Di$ideyUero/2ception Inherits !ro" Arith"etic/2ception

    Arith"etic/2ception Inherits !ro" &yste"/2ception• &yste"/2ception Inherits !ro" /2ception

    8ow usin Di$ideyUero/2ception

      catch GDi$ideyUero/2ception e2J  F

      Console%?riteLineGMDi$isor should be reater then ;MJ  Console%?riteLineGe2%Data%9o&trinGJJ  Console%?riteLineGe2%essae%9o&trinGJJ  Console%?riteLineGe2%&tac9race%9o&trinGJJ  Console%BeadLineGJ

      K

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    14/22

    ut this catch bloc will only handle Di$ideyUero/2ception so in order to

    catch other /2ception we can include another catch bloc and use base

    e2ception type as shown below%

    8ow I a" tryin to read the content o! the fle and i! any e2ception occurs

    while readin the sa"e e2ception will be handled $ia /2ception class else i!

    while di$idin we et di$ide by Qero /2ception that will be handled by

    Di$ideyUero /2ception class%

    usin &yste"usin &yste"%I@

      class ,rora"

      F  static $oid ainGJ  F  int a P 1;  int b P ;  int result  try  F  &trea"Beader ob6 P new &trea"BeaderGVMC:TsersTDe$eloperTDocu"entsTArticleT%net+ra"eworT,roduct OeMJ  Console%?riteLineGob6%Bead9o/ndGJJ  ob6%CloseGJ

     result P a b

      K  catch GDi$ideyUero/2ception e2J  F

    Console%?riteLineGMDi$isor should be reater than ;MJ  Console%BeadLineGJ  K

      catch G/2ception e2J  F

      Console%?riteLineGe2%essae%9o&trinGJJ  Console%BeadLineGJ

      K

      K

    K

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    15/22

    +inallyS here the set o! state"ents are written that you want to e2ecuteirrespecti$e o! /2ception occurs or not% /% Disposin AD@%8/9 ob6ects Close

    I@ ob6ects etcGW8ote we will be discussin these topics aheadJ% +inally bloc

    is not "andatory%

    In abo$e code as soon as the /2ception occurs the handle is trans!erred to

    the /2ception class nelectin all other state"ent a!ter that that would

    result in ob6ect occupyin the "e"ory% &o in order to close the sa"e we will

    do it in fnally bloc as shown below%

    usin &yste"usin &yste"%I@

      class ,rora"  F  static $oid ainGJ  F  int a P 1;  int b P ;  int result  &trea"Beader ob6 P null  try  F

      ob6 P new &trea"BeaderGVMC:TsersTDe$eloperTDocu"entsTArticleT%net+ra"eworT,roduct OeMJ  Console%?riteLineGob6%Bead9o/ndGJJ

      result P a b

      K  catch GDi$ideyUero/2ception e2J  F  Console%?riteLineGMDi$isor should be reater then ;MJ

     

    K

      catch G/2ception e2J

      F

      Console%?riteLineGe2%essae%9o&trinGJJ

     

    K

     fnally

      F  i! Gob6XPnullJ

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    16/22

      Fob6%CloseGJ

      K  Console%?riteLineGMI a" fnally blocMJ  Console%BeadLineGJ

      K

      K

     K

    Inner /2ceptions@nce while e2ecutin our ,rora" we !ace so"e e2ception so we handle the

    e2ception in catch bloc what i! we want to do so"e operation inside catch

    bloc !or e% ?ritin e2ception to a te2t fle etc% In order to do these

    operations we "ay !ace so"e other Inner /2ception inside a catch bloc so

    let's see how we handle the Inner /2ceptions% 8ow as I ha$e "entioned I

    want to lo the /2ception to a te2t fle with e2ception "essae trace and

    data and ti"e%

    usin &yste"usin &yste"%I@

      class ,rora"  F  static $oid ainGJ  F  int a P 1;  int b P ;  int result

     

    try  F

     

    result P a b

      K

      catch G/2ception e2J

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    17/22

      F

      strin +ilepath P VMC:TsersTDe$eloperTDocu"entsTArticleT%net +ra"eworT/2ception%t2tM  i!  G+ile%/2istsG+ilepathJJ  F  &trea"?riter ob6/2ception P new &trea"?riterG+ilepathJ  ob6/2ception%?riteGDate9i"e%8owNM MNe2%essaeNM MNe2%&tac9raceJ

     K

      else  F  throw new +ile8ot+ound/2ceptionGM+ile not !ound MN +ilepathe2%essaeJ  K

      Console%?riteLineGe2%essae%9o&trinGJJ

     

    K

     

    K K

     *ou can see in abo$e code that once the Di$ide by Uero /2ception co"es I a" writin the

    e2ception to te2t fle as shown below

    ?hat i! while writin the /2ception I !ace another e2ception so in order to handle that we use 9hrow /2ception as shown abo$e%

    Arrays

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    18/22

    Array is a roup o! si"ilar data types /% I we want to store 1; inteers% As

    this article is !ocused on C# I will not be oin in depth o! Arrays% I will help

    you how to declare and use Arrays in c#%

    &ynta2 o! Declarin an Array

    intH int'rr P new intH) siQe to specifed  Gna"e o! arrayJ

    /:

    usin &yste"usin &yste"%Collections%Eenericusin &yste"%Lin0usin &yste"%9e2tusin &yste"%9hreadin%9ass

    na"espace ArraysF  class ,rora"  F  static $oid ainGstrinH arsJ  F  intH intArr P new intH1;

     

    !or Gint i P ; i Y 1; iNNJ loopin threw I and assinin I $alue to array  F  intArrHi P i  K  Console%?riteLineGM9he Arrays consist o! !ollowin $aluesMJ  !orGint iP;iY1;iNNJ  F  Console%?riteLineGintArrHiJ  K  Console%BeadLineGJ

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    19/22

      K  KK

    Ad$antaesS

    • Arrays are stronly typed they don't allow storin other data type apart !ro"

    declared data type%

    • (D arrays are used to represent "atrices.

    +i 1: Arrays are stronly typed i%e% it can only accept the $alue o! declared datatype%

    Disad$antaesS

    • @nce we declare the array siQe it cannot be chaned%

    • &ince the array siQe is f2ed so i! insert less $alue the "e"ory is wasted and

    "ore than &yste"%Inde2@ut@!Bane/2ception will happen%

    +oreach Loop

    +or each loop is used to iterate threw the collection doesn't includes initialiQationter"ination and incre"entdecre"ent characteristics as in other loops%

    Ad$antaes:

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    20/22

    • 8o need to now the siQe o! the Collection%

    • Loops are easier to write it least error prone loop%

    5Note Code &nippet !or !oreach Loop is !oreach and then press tab%

    /2:

    &ynta2: !oreachGdatatype sa"e a collection Datatype na"e in CollectionJFstate"entsK

    usin &yste"usin &yste"%Collections%Eenericusin &yste"%Lin0usin &yste"%9e2tusin &yste"%9hreadin%9ass

    na"espace ArraysF

      class ,rora"  F  static $oid ainGstrinH arsJ  F  intH intArr P new intH1;  creatin a collection o! int array  !or Gint i P ; i Y 1; iNNJ  F  intArrHi P i  K  Console%?riteLineGM9he Array consist o! !ollowin $aluesMJ

      intArr is a collection!oreach Gint >alue in intArrJ

      F

      Console%?riteLineG>alueJ  K

     Console%BeadLineGJ

      K  KK

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    21/22

    Fig% Demonstrating ow 'ctually For >ac )oop Flows

    's we mo;e aead we learn more 9ene?ts o using or eac loop.

    Parallel Foreac)oop0n .net /icrosot@ .N>6 Framewor! A /icrosotpro;ide Parallel Computing eatures wicspeci?es new programming wor!ing model orwriting multi7treaded and asyncronous code . 0won4t 9e going in dept o tat and will ocus onParallel Foreac)oop gi;en in Parallel Computing.'s we a;e learn a9o;e a9out Foreac loop we alsoa;e Parallel oreac loop. 0n seBuential oreacloop te loop is run rom single tread wile inParallel Foreac loop it uses multiple treads. 6eimplementation o Parallel Foreac loop sould 9edone wen we a;e our independent set o

    https://msdn.microsoft.com/library/ms171868(v=vs.100).aspx#parallel_computinghttps://msdn.microsoft.com/library/ms171868(v=vs.100).aspx#parallel_computing

  • 8/18/2019 Learn Tiny Bit of C# in 7 days-Day1

    22/22

    statements i.e. not depending upon eac oter.6e parallel loop runs aster wen te computeras a;aila9le cores.

    Conclusion3ere we co"plete our day 1% In day 2 we will talk about Namespaces, Classes, 

    Inheritance, Access Modifiers, Properties, Interface and Abstract Class. So keep readin, keep

    learnin.

    I! any con!usion you can co""ent !or the sa"e% Any !eedbacs and added

    in!or"ation is heartily welco"ed%

    https://www.youtube.com/watch?v=IQTbvVemMAg&list=PLAC325451207E3105&index=18https://www.youtube.com/watch?v=Us2i7D-urgs&list=PLAC325451207E3105&index=19https://www.youtube.com/watch?v=Us2i7D-urgs&list=PLAC325451207E3105&index=19https://www.youtube.com/watch?v=OLTk0xAVY00&list=PLAC325451207E3105&index=21https://www.youtube.com/watch?v=OLTk0xAVY00&list=PLAC325451207E3105&index=21https://www.youtube.com/watch?v=IQTbvVemMAg&list=PLAC325451207E3105&index=18https://www.youtube.com/watch?v=Us2i7D-urgs&list=PLAC325451207E3105&index=19https://www.youtube.com/watch?v=OLTk0xAVY00&list=PLAC325451207E3105&index=21https://www.youtube.com/watch?v=OLTk0xAVY00&list=PLAC325451207E3105&index=21