Session 11_TP 6

download Session 11_TP 6

of 44

Transcript of Session 11_TP 6

  • 7/23/2019 Session 11_TP 6

    1/44

    Session 6

    Advanced C# Concepts -

    II

  • 7/23/2019 Session 11_TP 6

    2/44

    C# Simplifed / Session 6 / 2 o 44

    Review An assembly consists o:

    A set o types and resources tat orm a lo!ical unit ounctionality"

    A maniest containin! inormation tat describes teassembly" e can deploy classes witin te same namespace into

    di$erent assemblies% and classes witin di$erentnamespaces into one assembly"

    Assemblies are o two types: &rivate Assemblies and Sared

    Assemblies 'e version number o an assembly is represented as a our(

    part number in te ollowin! ormat: ...

  • 7/23/2019 Session 11_TP 6

    3/44

    C# Simplifed / Session 6 / ) o 44

    Review Contd* 'e Sstem.Re!ection namespace contains around orty

    classes and interaces tat can be employed to !etinormation about an ob+ect"

    Collections are datatypes tat provide us wit a way to storeand collectively act upon data"

    ,-ceptions are raised by te runtime wen an error occurs" 'e try and catc bloc. elps in andlin! raised e-ceptions" sin! te 'row statement% we can eiter trow a system

    e-ception% or our own custom e-ception" 'e statements in te 0inally bloc. are e-ecuted re!ardless

    o te control 1ow"

  • 7/23/2019 Session 11_TP 6

    4/44

    C# Simplifed / Session 6 / 4 o 44

    b+ectives Explain Properties

    Discuss type of properties such as

    Public Private

    Read-only

    Write-only

    Discuss Indexers

    Implement Delegates

    Dene and Raise Events

  • 7/23/2019 Session 11_TP 6

    5/44

    C# Simplifed / Session 6 / 3 o 44

    &roperties in C# C# provides te acility to protect a feld

    in a class by readin! and writin! to it

    trou! a eature called properties C# properties enable a similar type o

    protection wile at te same time allowsaccess to te property li.e a feld"

    5n !eneral terms% a property would meante caracteristics or inormation abouta particular entity"

  • 7/23/2019 Session 11_TP 6

    6/44

    C# Simplifed / Session 6 / 6 o 44

    &roperties in C# ( Contd 'e !eneral orm or declarin! te properties is

    access7modifer8 return7type8property7name89"et9set9

    were access7modifer8 can be private% public%protectedor internal" 'e return7type8 can beany valid C# type"

  • 7/23/2019 Session 11_TP 6

    7/44 C# Simplifed / Session 6 / ; o 44

    &roperties < =et > Setpublic class Employee

    {

    private String sName

    private String sId public string sId

    {

    get

    {

    return sId;

    } set

    {

    sId = value

    }

    }

  • 7/23/2019 Session 11_TP 6

    8/44 C# Simplifed / Session 6 / ? o 44

    &roperties < =et > Set

    }

    Employee emp = new Employee();

    empsId = !S""#!;

    $onsole%rite&ine('e student id is

    {"}!* empsId);

    ,ac time we need to access te feld weneed to call te set and !et metods

  • 7/23/2019 Session 11_TP 6

    9/44 C# Simplifed / Session 6 / @ o 44

    &roperties ( ,-ampleusing System;public class Student

    {

    public string sName; ++,ield

    private string internal-sId;++,ieldpublic string sId ++.roperty {

    get

    {

    return internal_sId;

    } set

    {

    internal_sId = value;

    }

    }

    }

  • 7/23/2019 Session 11_TP 6

    10/44 C# Simplifed / Session 6 / B o 4

    &roperties < ,-ample Contd*

    class est

    {

    static void /ain()

    {

    Student one = new Student();

    onesName = 'Sam'; one.sId = "St423";

    Console.Writeine!"#e $a%e o& t#e Student is {'}

    and #is Id is {(}") one.s$a%e) one.sId*;

    }

    }

  • 7/23/2019 Session 11_TP 6

    11/44 C# Simplifed / Session 6 / o 4

    &roperties ( ,-planation

  • 7/23/2019 Session 11_TP 6

    12/44 C# Simplifed / Session 6 / 2 o 4

    'ypes o &roperties ublic roperties: sers can access tese

    properties witout any restrictions rivate roperties: are accessible to te

    users only rom inside te class or structurewere tey are defned"

    Read $ %rite ropert: &rovides bot readand write access to te data members"

    Read - &nl ropert: &rovides only readaccess to te data members"

    %rite - &nl ropert: &rovides only writeaccess to te data members"

  • 7/23/2019 Session 11_TP 6

    13/44 C# Simplifed / Session 6 / ) o 4

    Read(only &roperty ( ,-amplepublic class Employee

    {

    private int empsalary = #0"""; ++,ield

    public int EmpSalary ++.roperty

    { get

    {

    return empsalary;

    }

    }

    }

    class Employeeest

    {

  • 7/23/2019 Session 11_TP 6

    14/44 C# Simplifed / Session 6 / 4 o 4

    Read(only &roperty (

    utputstatic void /ain()

    {

    Employee ob1Employee = new Employee();

    System$onsole%rite&ine ("e salary o2 te

    Employee is {"}"* ob1EmployeeEmpSalary);ob1EmployeeEmpSalary = 30"""; ++error }

    }

  • 7/23/2019 Session 11_TP 6

    15/44 C# Simplifed / Session 6 / 3 o 4

    &roperties s 0ields &roperties are lo!ical felds

    &roperties are an e-tension ofelds

    nli.e felds% properties do notcorrespond directly to astora!e location

  • 7/23/2019 Session 11_TP 6

    16/44 C# Simplifed / Session 6 / 6 o 4

    &roperty Accessors Accessors o properties contain te

    statements tat are to be e-ecuted wena property is "et/set

    'e accessor declarations can contain te"etaccessor or te setaccessor or bot"

    'e "etaccessor is similar to any

    metod tat returns a value 'e setaccessor is similar to a metod

    tat returns a void

  • 7/23/2019 Session 11_TP 6

    17/44 C# Simplifed / Session 6 / ; o 4

    5nde-ers 5nde-ers are simple components o C#

    Created or arrays so tat te elements

    are accessed directly by speciyin! teinde- rom te class ob+ect

    Allow an ob+ect to be inde-ed in te

    same way as an array ,nable array(li.e access to te

    members o te class

  • 7/23/2019 Session 11_TP 6

    18/44 C# Simplifed / Session 6 / ? o 4

    5nde-ers ( ,-ampleclass Inde4erE4ample{

    public string56 string&ist =new string5#"6;

    public string tis5int inde46

    { get

    {

    return string&ist5inde46;

    }

    set{

    string&ist5inde46 = valueoString();

    }

    }

    }

  • 7/23/2019 Session 11_TP 6

    19/44 C# Simplifed / Session 6 / @ o 4

    5nde-ers ( utputclass est

    {

    static void /ain()

    { Inde4erE4ample inde4est = new Inde4erE4ample();

    inde4eststring&ist5#6='Sam';

    inde4est536='om';

    System$onsole%rite&ine('inde4est5#6 is

    {"}7ninde4est536 is {#}'* inde4est5#6* inde4est536);

    }}

  • 7/23/2019 Session 11_TP 6

    20/44 C# Simplifed / Session 6 / 2B o 4

    Steps or declarin! an 5nde-er 'e access modifer tat decides te

    visibility o te inde-er is to be mentioned

    State te return type o te inde-er 'e t'is.eyword Speciy te data type o te inde-

    State te variable name or te inde- Speciy te "etand setaccessors inside

    te inde-er

  • 7/23/2019 Session 11_TP 6

    21/44

    C# Simplifed / Session 6 / 2 o 4

    Rules or defnin! an 5nde-er

    At least one 5nde-er parametermust be specifed

    'e parameters sould beassi!ned values

  • 7/23/2019 Session 11_TP 6

    22/44

    C# Simplifed / Session 6 / 22 o 4

    5nde-ers vs" Arrays 5nde-ers do not point to memory

    locations

    5nde-ers can ave non < inte!ersubscripts Dinde-esE

    5nde-ers can be overloaded

  • 7/23/2019 Session 11_TP 6

    23/44

    C# Simplifed / Session 6 / 2) o 4

    5nde-er < ,-ample using System$ollections;

    class StrInde4

    {

    public 8astable student&ist = new 8astable();

    public int tis5string name6 {

    get

    {

    return (int) student&ist5name6;

    }

    set {

    student&ist5name6 = value;

    }

    }

    }

  • 7/23/2019 Session 11_TP 6

    24/44

    C# Simplifed / Session 6 / 24 o 4

    5nde-er < ,-ample

    Contd*class est{

    static void /ain()

    {

    StrInde4 ob1Inde4 = new StrInde4(); ob1Inde4 5'Sam'6 = 393::;

    ob1Inde4 5'om'6 =

  • 7/23/2019 Session 11_TP 6

    25/44

    C# Simplifed / Session 6 / 23 o 4

    5nde-er ,-ample ( 2using System$ollections;

    class Inde4erE4ample

    {

    public string56 string&ist = new string5#"6;

    public string tis5int inde46

    {

    get

    {

    return string&ist5inde46;

    } set

    {

    string&ist5inde46 = valueoString();

    }

    }

    public 8astable student&ist = new 8astable();

  • 7/23/2019 Session 11_TP 6

    26/44

    C# Simplifed / Session 6 / 26 o 4

    5nde-er ,-ample < 2

    Contd*public int tis5string number6

    {

    get

    { return (int) student&ist 5number6;

    }

    set

    {

    student&ist 5number6 = value;

    }

    }

    }

  • 7/23/2019 Session 11_TP 6

    27/44

    C# Simplifed / Session 6 / 2; o 4

    5nde-er ,-ample < 2

    Contd*class est

    {

    static void /ain()

    {

    Inde4erE4ample inde4est = new Inde4erE4ample();

    inde4eststring&ist5#6 = 'Sam';

    inde4est536 = 'om';

    inde4est 5'Sam'6 = 393;

    inde4est 5'om'6 =

  • 7/23/2019 Session 11_TP 6

    28/44

    C# Simplifed / Session 6 / 2? o 4

    sin! Fultiple &arameters Fore tan one inde-er parameter can be specifed in an inde-er Fultiple parameters would mean tat te inde-er would be

    accessed li.e a multi(dimensional array

    class est/ultip

    {

    public int tis5int 2irst.* int second.6

    {

    ++et and Set >ccessors appear ere

    }

    }

  • 7/23/2019 Session 11_TP 6

    29/44

    C# Simplifed / Session 6 / 2@ o 4

    sin! Fultiple &arameters

    {

    void static /ain()

    {

    est/ultip myest = new est/ultip();

    int I = myest5#*#6; }

    }

    Class 'est5nde-ers wit multiple inde-erparameters are accessed li.e a Fulti(Gimensional Array"

  • 7/23/2019 Session 11_TP 6

    30/44

    C# Simplifed / Session 6 / )B o 4

    Gele!ates Contain a reerence to a metod Helps us decide% wic metod to call at

    runtime 'ecnically% a dele!ate is a reerencetype used to encapsulate a metod wita specifc si!nature and return type"

    Steps < Gefnin! a dele!ate 5nstantiatin! a dele!ate sin! a dele!ate

  • 7/23/2019 Session 11_TP 6

    31/44

    C# Simplifed / Session 6 / ) o 4

    Gefnin! Gele!ates

    public delegate void

    ?elegateName();

    Gefnin! a dele!ate involves speciyin! tereturn types and parameters tat eac metodmust provide

    """public delegate int Call+unctions !int a) int b*;

    """

  • 7/23/2019 Session 11_TP 6

    32/44

    C# Simplifed / Session 6 / )2 o 4

    5nstantiatin! Gele!atesclass 'estGele!ates9 public dele!ate int Call0unctionsDint a% int bEI class Fatsperations 9 public int Fultiply0nDint a% int bE 9 return aJbI public int Givision0nDint a% int bE 9 return a/bI

    5nstantiatin! adele!atemeans ma.in!

    it point DorreerE to somemetod"

  • 7/23/2019 Session 11_TP 6

    33/44

    C# Simplifed / Session 6 / )) o 4

    5nstantiatin! Gele!ates

    Contd*class est

    {

    static void /ain()

    { Call+unctions ,elegate-b;

    /at#s-perations %at# = ne0

    /at#s-perations!*;

    ,elegate-b = ne0

    Call+unctions!%at#./ultipl1+un*; }

    }

    }

  • 7/23/2019 Session 11_TP 6

    34/44

    C# Simplifed / Session 6 / )4 o 4

    sin! Gele!atesclass est?elegates{

    public delegate int $all,unctions(int num#* int

    num3);

    class /ats@perations {

    public int /ultiply,n(int num#* int num3)

    {

    return num#Anum3;

    }

    public int ?ivision,n(int num#* int num3) {

    return num#+num3;

    }

    }

    }

    sin! a dele!ate means instantiatin! a metod tat usesdele!ates"

  • 7/23/2019 Session 11_TP 6

    35/44

    C# Simplifed / Session 6 / )3 o 4

    sin! Gele!ates ( Contdclass est{

    static void /ain()

    {

    $all,unctions ?elegate@b1; /ats@perations mat = new /ats@perations();

    ?elegate@b1 = new $all,unctions(mat/ultiply,n);

    int result=?elegate@b1(0* 9);

    System$onsole%rite&ine ('e Besult a2ter

    calling /ultiply,n is C {"}'*result);

    }?elegate@b1 = new $all,unctions(mat?ivision,n);

    int result( = ,elegate-b!) 3*;

    System$onsole%rite&ine ('e Besult a2ter

    calling ?ivision,n is C {"}7n'*result#);

  • 7/23/2019 Session 11_TP 6

    36/44

    C# Simplifed / Session 6 / )6 o 4

    sin! Gele!ates ( utput

  • 7/23/2019 Session 11_TP 6

    37/44

    C# Simplifed / Session 6 / ); o 4

    ,vents ,vents in C# allow an ob+ect to notiy oter ob+ects

    about te event or tat a can!e as occurred

    'e ob+ect tat notifes te oters about te eventis .nown as te &ubliser

    An ob+ect tat re!isters to an event is .nown aste Subscriber

    Steps Gefnin! an ,vent Subscribe ob+ects to tat event Kotiy subscribers o te event

  • 7/23/2019 Session 11_TP 6

    38/44

    C# Simplifed / Session 6 / )? o 4

    Gefnin! ,vents ile defnin! an event te publiser

    defnes a dele!ate

    defnes an event based on te dele!ate

    public delegatevoid delegate/e();private eventdelegate/e event/e;

  • 7/23/2019 Session 11_TP 6

    39/44

    C# Simplifed / Session 6 / )@ o 4

    Subscribin!Subscribin! an ob+ect to an eventdepends upon te e-istence o te

    event

    event/e = ne0 delegate/e!ob./et#od*;

    event/e = ne0 delegate/e!ob5./et#od*;

  • 7/23/2019 Session 11_TP 6

    40/44

    C# Simplifed / Session 6 / 4B o 4

    Kotiyin!'o notiy all te ob+ects tat avesubscribed to an event we +ust need

    to raise te eventiDconditionE9 eventMe()*

    Raisin! an event is li.e callin! a

    metod"

  • 7/23/2019 Session 11_TP 6

    41/44

    C# Simplifed / Session 6 / 4 o 4

    ,vents and Gele!ates(,-ample

    using System;

    class $lass>

    {

    public void ?isp/etod()

    {

    $onsole%rite&ine ('$lass > as been noti2ied o2

    Noti2yEvery@ne EventD');

    }

    }

    class $lass

    { public void ?isp/etod()

    {

    $onsole%rite&ine ('$lass as been noti2ied o2

    Noti2yEvery@ne EventD');

    }

    }

  • 7/23/2019 Session 11_TP 6

    42/44

    C# Simplifed / Session 6 / 42 o 4

    ,vents and Gele!ates(

    ,-ampleclass est

    {

    public static void /ain()

    {

    est?elegate d> = new est?elegate(); $lass> ob1> = new $lass>();

    $lass ob1 = new $lass();

    d>Noti2yEvery@ne F= new

    est?elegate/e?elegate (ob1>?isp/etod);

    d>Noti2yEvery@ne F= new

    est?elegate/e?elegate (ob1?isp/etod); d>Noti2y();

    }

    }

  • 7/23/2019 Session 11_TP 6

    43/44

    C# Simplifed / Session 6 / 4) o 4

    ,vents and Gele!ates(

    ,-ampleclass est?elegate{

    public delegate void /e?elegate();

    public event /e?elegate Noti2yEvery@ne;

    public void Noti2y(){

    i2(Noti2yEvery@ne D= null)

    {

    $onsole%rite&ine ('Baise Event C ');

    Noti2yEvery@ne();

    }}

    }

  • 7/23/2019 Session 11_TP 6

    44/44

    Summary &roperties provide te opportunity to protect a feld in a class by

    readin! and writin! it usin! accessors" An inde-er allows an ob+ect to be inde-ed in te same way as an

    array" A dele!ate contains a reerence to a metod rater tan te metod

    name" 'ere are tree steps involved in usin! dele!ates tat are as ollows"

    +e,nin" a Gele!ate Instantiatin" a Gele!ate sin" a Gele!ate

    ,vents in C# allow an ob+ect to notiy oter ob+ects about te event%or about a can!e tat as occurred"

    'ere are tree steps involved to andle events in C# tat are asollows" +e,nin" an vent Subscribin" objects to t'at event Noti/in" subscribers o/ t'e event (0'en it occurs)