Ppt 0811

download Ppt 0811

of 35

Transcript of Ppt 0811

  • 8/14/2019 Ppt 0811

    1/35

    March 9, 2010 C# 1

    : Value Type

    (User - Defined) struct

    (Built - in)

    sbyte

    byte

    short

    ushort

    intuint

    long

    ulong

    float

    double

    decimal

    char

    bool

    (User - Defined) enum

  • 8/14/2019 Ppt 0811

    2/35

    March 9, 2010 C# 2

    : Reference Type

    Reference

    Type

    class class

    string , Exception

    interface

    array

    delegate

  • 8/14/2019 Ppt 0811

    3/35

    March 9, 2010 C# 3

    Using Reference-TypeVariables

    Comparing Value Types to Reference Types

    Declaring and Releasing Reference Variables

    Invalid References

    Comparing Values and Comparing References

    Multiple References to the Same Object

    Using References as Method Parameters

  • 8/14/2019 Ppt 0811

    4/35

    March 9, 2010 C# 4

    Comparing Value Type to ReferenceTypes

    Value Types

    The variablecontains the valuedirectly

    Examples :char, int

    Reference Types

    The variablecontains areference to thedata

    Data is stored in aseparate memoryarea

    int mo1 ;mo1 = 42 ;

    string mo1 ;mo1 = "Hello" ;

    42 Hello

    mo1

  • 8/14/2019 Ppt 0811

    5/35

    March 9, 2010 C# 5

    Declaring and Releasing ReferenceVariables

    Declaring Reference Variables

    Releasing Reference Variables

    c1 = null ;

    coordinate c1 ;c1 = new coordinate( ) ;c1.x = 6.12 ;

    c1.y = 4.2 ; 6.12

    c1

    4.2

    6.12

    c1

    4.2

  • 8/14/2019 Ppt 0811

    6/35

    March 9, 2010 C# 6

    Invalid References

    If You Have Invalid References

    You cannot access members orvariables

    Invalid References at Compile Time

    Compiler detects use ofuninitializedreferences

    Invalid References at Run Time

    System will generate an exceptionerror

    - NullReferenceException

  • 8/14/2019 Ppt 0811

    7/35

    March 9, 2010 C# 7

    Comparing Values and ComparingReferences

    Comparing Value Types ( string )

    == and != compare values

    Comparing Reference Types ( string )

    == and != compare the reference, notthe values

    6.12O 4.2

    6.12O 4.2

    Different

  • 8/14/2019 Ppt 0811

    8/35

    March 9, 2010 C# 8

    Multiple References to the SameObject

    Two References Can Refer to the SameObject

    Two ways to access the same object forread/write

    coordinate c1 = new coordinate( ) ;

    coordinate c2 ;c1.x = 2.3 ; c1.y = 7.6 ;c2 = c1 ;Console.WriteLine (c1.x + " , " + c1.y) :Console.WriteLine (c2.x + " , " + c2.y);

    6.12

    c1

    4.2

    c2

  • 8/14/2019 Ppt 0811

    9/35

    March 9, 2010 C# 9

    Using References as Method Parameters

    References Can Be Used as Parameters

    When passed by value, data beingreferenced may be changed

    static void PassCoordinateByValue (coordinate c){

    c.x++ ; c.y++ ;}

    2 3

    c

    3 4

    loc.x = 2 ; loc.y = 3 ;PassCoordinateByValue (loc) ;Console.WriteLine(loc.x + " , " + loc.y) ;

    loc

  • 8/14/2019 Ppt 0811

    10/35

    March 9, 2010 C# 10

    class coordinate {

    public double x = 0.0; public double y = 0.0; }

    static coordinate Example ( coordinate ca,

    ref coordinate cb,out coordinate cc )

    { }

    static void PassCoordinateByValue (coordinate c) {

    { c.x++; c.y++; }

    coordinate loc = new coordinate( );

    loc.x = 2; loc.y = 3;

    PassCoordinateByValue ( loc );

    Console.WriteLine( loc.x, loc.y);

    static void PassCoordinateByValue (coordinate c) {

    { c = new coordinate ( );

    c.x++; c.y++; }

  • 8/14/2019 Ppt 0811

    11/35

    March 9, 2010 C# 11

    Using Common ReferenceTypes

    Exception Class

    String Class

    Common String Methods, Operators, andProperties

    String Comparisons

    String Comparison Operators

  • 8/14/2019 Ppt 0811

    12/35

    March 9, 2010 C# 12

    Exception Class

    Exception is a Class

    Exception Objects Are Used to Raise Exceptions

    Create an Exception object by using new

    Throw the object by using throw

    Exception Types Are Subclasses of Exception

    InvalidCastException, DivideByZeroException..

  • 8/14/2019 Ppt 0811

    13/35

    March 9, 2010 C# 13

    String Class

    Multiple Character Unicode Data

    Shorthand for System.String

    Immutable ( )

    StringBuilder (mutable : can modify)

    string s = Hello ;

    s[0] = c : // Compile-time error

  • 8/14/2019 Ppt 0811

    14/35

    March 9, 2010 C# 14

    Common String Methods, Operators, andProperties

    Brackets

    Insert Methods

    Length Property

    Copy Method

    Concat Method

    Trim Method

    ToUpper andToLower Methods

  • 8/14/2019 Ppt 0811

    15/35

    March 9, 2010 C# 15

    String Comparisons

    Equals Method

    Value comparison

    if (s1.Equals(s2)) //instance method

    if (String.Equals(s1,s2)) //staticmethod

    Compare Method

    More comparison

    Case-insensitive option Dictionary ordering

    Locale-Specific Compare Options

  • 8/14/2019 Ppt 0811

    16/35

    March 9, 2010 C# 16

    String Comparison Operators

    The == and != Operators Are Overloaded forStrings

    They Are Equivalent to String.Equals and !String.Equals

    string a = Test ;string b = Test ;if ( a == b ) . // Returns true

  • 8/14/2019 Ppt 0811

    17/35

    March 9, 2010 C# 17

    The Object Hierarchy

    The Object Type

    Common Methods

    Reflection

  • 8/14/2019 Ppt 0811

    18/35

    March 9, 2010 C# 18

    The object Type

    Synonym for System.Object

    Base Class for All Classes

    SystemException

    Exception

    Object

    MyClassString

  • 8/14/2019 Ppt 0811

    19/35

    March 9, 2010 C# 19

    Common Methods

    Common Methods for All Reference Types

    ToString method

    Equals method

    GetType method Finalize method

  • 8/14/2019 Ppt 0811

    20/35

    March 9, 2010 C# 20

    Reflection

    You Can Query the Type of an Object

    System.Reflection Namespace

    The typeofOperator Returns a Type Object

    Compile-time classes only GetType Method in System.Object

    Run-time class information

  • 8/14/2019 Ppt 0811

    21/35

    March 9, 2010 C# 21

    Namespace in the .NET Framework

    System.IO Namespace

    System.Xml Namespace

    System.Data Namespace

    Other Useful Namespace

  • 8/14/2019 Ppt 0811

    22/35

    March 9, 2010 C# 22

    System.IO Namespace

    Access to File System Input/Output

    File, Directory

    StreamReader, StreamWriter bytes,characters

    FileStream random access to files

    BinaryReader, BinaryWriter binary types

  • 8/14/2019 Ppt 0811

    23/35

    March 9, 2010 C# 23

    System.Xml Namespace

    XML Support

    Various XML-Related Standards

  • 8/14/2019 Ppt 0811

    24/35

    March 9, 2010 C# 24

    System.Data Namespace

    System.Data.SqlClient

    SQL Server .NET Data Provider

    System.Data

    Consists mostly of the classes thatconstitute the ADO.NET architecture

  • 8/14/2019 Ppt 0811

    25/35

    March 9, 2010 C# 25

    Other Usefule Namespaces

    System Namespace

    System.Net Namespace

    System.Net.Sockets Namespace

    System.Windows.Forms Namespace

  • 8/14/2019 Ppt 0811

    26/35

    March 9, 2010 C# 26

    Data Conversions

    Converting Value Types

    Parent/Child Conversions

    The is Operator

    The as Operator Conversions and the object Type

    Conversions and Interfaces

    Boxing and Unboxing

  • 8/14/2019 Ppt 0811

    27/35

    March 9, 2010 C# 27

    Converting Value Types

    Impilcit Conversions

    Explicit Conversions

    Cast operator

    Exceptions System.Convert Class

    Handles the conversions internally

  • 8/14/2019 Ppt 0811

    28/35

    March 9, 2010 C# 28

    Reference Type Data Conversion

    Parent - General

    Child - Specific

  • 8/14/2019 Ppt 0811

    29/35

    March 9, 2010 C# 29

    Parent/Child Conversions

    Conversion to Parent Class Reference

    Implicit or explicit

    Always succeeds

    Can always assign to object

    Conversion to Child Class Reference

    Explicit casting required

    Will check that the reference is of the

    correct type Will raise InvalidCastException if not

  • 8/14/2019 Ppt 0811

    30/35

    March 9, 2010 C# 30

    The is Operator

    Returns true If a Conversion Can Be Made

    InvalidCastException

    Bird b ;if ( a is Bird )

    b = ( Bird ) a ; // Safeelse

    Console.WriteLine (Not a Bird) ;

  • 8/14/2019 Ppt 0811

    31/35

    March 9, 2010 C# 31

    The as Operator

    Converts Between References Types, Like Cast

    On Error

    Returns null

    Does not raise an exception

    Bird b = a as Bird ;if ( b == null )

    Console.WriteLine (Not a Bird) ;

  • 8/14/2019 Ppt 0811

    32/35

    March 9, 2010 C# 32

    Conversions and the object Type

    The object Type Is the Base for All Classes

    Any Reflection Can Be Assigned to object

    Any object Variable Can Be Assigned to AnyReference

    With appropriate type conversion andchecks

    The object Type and is Operator

    object ox ;ox = a ;ox = (object) a ;ox = a as object ;

    b = (Bird) ox ;b = ox as Bird ;

  • 8/14/2019 Ppt 0811

    33/35

    March 9, 2010 C# 33

    Conversion and Interfaces

    An Interface Can Only Be Used to Access ItsOwn Members

    Other Methods and Variables of the Class AreNot Accessible Through the Interface

    Rectangle r = new Rectangle ( ) ;r.Move ( ) ;r.Paint ( ) ;

    IVisual v = ( IVisual ) r ;

    v.Move ( ) ; //not validv.Paint ( ) ; // ok

  • 8/14/2019 Ppt 0811

    34/35

    March 9, 2010 C# 34

    Boxing and Unboxing

    Unified Type System

    Boxing (implicit : always success)

    Unboxing

    Calling Object Methods on Value Types

    p = ( int ) box ;int p = 123 ;object box ;box = p ;

    123box

    123

  • 8/14/2019 Ppt 0811

    35/35

    March 9 2010 C# 35

    Data Conversions

    Value = Value implicit conversionexplicit conversion

    Reference = Reference parent = child : implicit

    child = parent : explicit

    Reference = Value boxing

    Value = Reference unboxing