03 data types

Upload: kvinothscet

Post on 04-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 03 Data Types

    1/26

  • 7/30/2019 03 Data Types

    2/26

    2Microsoft

    Objectives

    .NET is designed around the CTS, or Common Type System. TheCTS is what allows assemblies, written in different languages, towork together. To ensure interoperability across languages,Microsoft has also defined the CLS, or Common LanguageSpecification, a subset of the CTS that all languages support.Otherwise, the types in C# are what you would expect from a

    modern OOPL

    The Common Type System

    Value vs. reference types Arrays

    Namespaces

  • 7/30/2019 03 Data Types

    3/26

    3Microsoft

    Part 1

    The Common Type System

  • 7/30/2019 03 Data Types

    4/26

    4Microsoft

    The Common Type System (CTS)

    CTS is based on a hierarchy of classes defined in FCL all types inherit from Object (all except interface types)

    String Array ValueType Exception Delegate Class1

    MulticastDelegate

    Class2

    Class3

    Object

    Enum1

    Structure1EnumPrimitive types

    Boolean

    Byte

    Int16

    Int32

    Int64

    Char

    Single

    Double

    Decimal

    DateTime

    System-defined types

    User-defined types

    Delegate1

    TimeSpan

    Guid

  • 7/30/2019 03 Data Types

    5/26

    5Microsoft

    The Common Language Specification (CLS)

    Not all languages support all CTS types and features C# supports unsigned integer types, VB.NET does not

    C# is case sensitive, VB.NET is not

    C# supports pointer types (in unsafe mode), VB.NET does not

    C# supports operator overloading, VB.NET does not

    CLS was drafted to promote language interoperability

    vast majority of classes within FCL are CLS-compliant

  • 7/30/2019 03 Data Types

    6/26

    6Microsoft

    Mapping C# to CTS

    Language keywords map to common CTS classes:

    Keyword Description Special format for literals

    bool Boolean true false

    char 16 bit Unicode character 'A' '\x0041' '\u0041'

    sbyte 8 bit signed integer none

    byte 8 bit unsigned integer none

    short 16 bit signed integer none

    ushort 16 bit unsigned integer none

    int 32 bit signed integer none

    uint 32 bit unsigned integer U suffix

    long 64 bit signed integer L or l suffixulong 64 bit unsigned integer U/u and L/l suffix

    float 32 bit floating point F or f suffix

    double 64 bit floating point no suffix

    decimal 128 bit high precision Mormsuffix

    string character sequence "hello", @"C:\dir\file.txt"

  • 7/30/2019 03 Data Types

    7/26

    7Microsoft

    Example

    An example of using types in C#

    declare before you use (compiler enforced)

    initialize before you use (compiler enforced)

    public class App{public static void Main(){int width, height;width = 2;height = 4;

    int area = width * height;

    int x;int y = x * 2;...

    }

    }

    declarations

    decl + initializer

    error, x not set

  • 7/30/2019 03 Data Types

    8/26

  • 7/30/2019 03 Data Types

    9/26

    9Microsoft

    Part 2

    Value vs. reference types

  • 7/30/2019 03 Data Types

    10/26

    10Microsoft

    Value vs. reference types

    C# separates data types into two categories

    Value types:

    variable represents a value ("bits")

    Reference types:

    variable represents a reference to a heap-based object

    actual data resides in the object

    int i;

    i = 10;

    10

    string s;

    s = "calico";

    "calico"

  • 7/30/2019 03 Data Types

    11/26

    11Microsoft

    How do you know which types are which?

    Memorization!

    Though it's pretty obvious based on past experience

    primitive types like bool, int and double are values

    remainder are reference types

    int i;string s;Customer c1, c2;

    i = 23;s = "a message";

    c1 = null;c2 = new Customer();

  • 7/30/2019 03 Data Types

    12/26

    12Microsoft

    Boxing and Unboxing

    When necessary, C# will auto-convert value object

    value ==> object is called "boxing"

    object ==> value is called "unboxing"

    int i, j;

    object obj;string s;

    i = 32;obj = i; // boxed copy!i = 19;

    j = (int) obj; // unboxed!

    s = j.ToString(); // boxed!s = 99.ToString(); // boxed!

  • 7/30/2019 03 Data Types

    13/26

    13Microsoft

    User-defined reference types

    Classes!

    for example, Customer class we worked with earlier

    public class Customer

    {

    public string Name; // fields

    public int ID;

    public Customer(string name, int id) // constructor

    {

    this.Name = name;

    this.ID = id;

    }

    public override string ToString() // method

    { return "Customer: " + this.Name; }

    }

  • 7/30/2019 03 Data Types

    14/26

    14Microsoft

    Working with reference types

    Creating, assigning, and comparing:

    Customer c1, c2, c3;string s1, s2;

    c1 = new Customer("joe hummel", 36259);

    c2 = new Customer("marybeth lore", 55298);c3 = null; // c3 references no object

    c3 = c1; // c3 now references same obj as c1

    if (c1 == null) ... // do I ref an object?

    if (c1 == c2) ... // compares referencesif (c1.Equals(c2)) ... // compares objects

    if (s1 == s2) ... // exception: == overloaded to// compare string data

  • 7/30/2019 03 Data Types

    15/26

    15Microsoft

    Defining equality

    Classes should override Equals

    public class Customer

    {...

    public override bool Equals(object obj)

    {

    Customer other;

    if ((obj == null) || (!(obj is Customer)))

    return false; // definitely not equal

    other = (Customer) obj; // typecast to access

    return this.ID == other.ID; // equal if same id...

    }

  • 7/30/2019 03 Data Types

    16/26

  • 7/30/2019 03 Data Types

    17/26

    17Microsoft

    Part 3

    Arrays

  • 7/30/2019 03 Data Types

    18/26

    18Microsoft

    Arrays

    Arrays are reference types

    based on Array class in FCL

    must be created using new

    0-based indexing

    assigned default values (0 for numeric, null for references, etc.)

    int[] a;a = new int[5];

    a[0] = 17;

    a[1] = 32;int x = a[0] + a[1] + a[4];

    int l = a.Length;

    element access

    create

    number of elements

  • 7/30/2019 03 Data Types

    19/26

    19Microsoft

    Multi-dimensional arrays

    C# supports arrays as a single object OR array of arrays

    latter allows you to implement jagged arrays

    Customer[,] twoD;int[][] jagged2D;

    // 2D array as single objecttwoD = new Customer[10, 100];twoD[0, 0] = new Customer();twoD[9, 99] = new Customer();

    // 2D array as array of arrays

    jagged2D = new int[10][];jagged2D[0] = new int[10];jagged2D[1] = new int[20];jagged2D[9] = new int[100];

    jagged2D[0][0] = 1;jagged2D[9][99] = 100;

  • 7/30/2019 03 Data Types

    20/26

    20Microsoft

    Part 4

    Namespaces

  • 7/30/2019 03 Data Types

    21/26

    21Microsoft

    Namespaces

    Namespaces are a means for organizing types

    a namespace N is a set of names scoped by N

    namespaces are often nested

    namespace Workshop

    {

    public class Customer{...

    }

    public class Product{...

    }

    }//namespace

    Workshop.Customer

  • 7/30/2019 03 Data Types

    22/26

    22Microsoft

    Example

    Framework Class Library (FCL)contains 1000's of classes

    how to organize?

    how to avoid name collisions?

    with FCL

    within FCL

  • 7/30/2019 03 Data Types

    23/26

    23Microsoft

    FCL namespaces

    FCL's outermost namespace is "System"

    FCL technologies nested within System

    Namespace Purpose Assembly

    System Core classes, types mscorlib.dll

    System.Collections Data structures mscorlib.dll

    System.Data Database access System.Data.dll

    System.Windows.Forms GUI System.Windows.Forms.dll

    System.XML XML processing System.Xml.dll

  • 7/30/2019 03 Data Types

    24/26

    24Microsoft

    Namespace != Assembly

    Orthogonal concepts:

    namespace for organization

    assembly for packaging

    One namespace could be spread across multiple assemblies

    One assembly may contain multiple namesspaces

    e.g. mscorlib.dll

  • 7/30/2019 03 Data Types

    25/26

    25Microsoft

    Summary

    CTS is the common type system

    same type system for all languages

    types implemented by classes in FCL

    fundamental difference between value & reference types

    CLS is the common language specification

    types that are guaranteed to work across languages

    Try not to confuse namespaces with assemblies

    namespaces help with organization assemblies denote implementation / packaging

  • 7/30/2019 03 Data Types

    26/26

    26Microsoft

    References

    Books:

    I. Pohl, "C# by Dissection"

    S. Lippman, "C# Primer"

    J. Mayo, "C# Unleashed"