05 iec t1s1 oops session 07

Upload: caininme

Post on 30-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    1/32

    Slide 1 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    In this session, you will learn to:

    Describe memory allocation

    Use structure

    Use enumerations

    Implement arraysUse collections

    Objectives

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    2/32

    Slide 2 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    The memory allocated to variables is referred to in the

    following ways:

    Value types: Contains data. Built-in data types, such as int,

    char, and float are value types.

    Reference types: Contains address referring to a block of

    memory. Data types, such as string and class are reference

    types.

    Let us understand the concept of memory allocation in

    detail.

    Describing Memory Allocation

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    3/32

    Slide 3 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Describing Memory Allocation (Contd.)

    int Num1;

    Num1=50;Initialization

    Initializing Num2 with Num1

    int Num2;

    Num2=Num1;

    Variable Declaration

    Value Type:

    Variable Declaration 50

    50

    Both Num1 and Num2 Contain 50

    Num1

    Num2

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    4/32

    Slide 4 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Describing Memory Allocation (Contd.)

    Value Type (Contd.):

    Num1=60;

    Num1

    60

    Num2

    50

    The Value of Num2 Remains Unaffected

    New Value Assigned to Num1

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    5/32

    Slide 5 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Car Suzuki= new Car();

    Suzuki.Model=10;

    Object of Class Car()

    Member Variable of Class Car()

    Initialization

    Initializing Mercedes with Suzuki

    Object of Class Car()Car Mercedes;

    Mercedes=Suzuki;

    Describing Memory Allocation (Contd.)

    Reference Type:

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    6/32

    Slide 6 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Suzuki

    Mercedes

    ***

    ***

    10

    Referring to Memory

    Location Where the Data

    is Stored

    Referring to Memory

    Location Where the Data

    is Stored

    Describing Memory Allocation (Contd.)

    Reference Type (Contd.):

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    7/32Slide 7 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    A structure is a value type data type.

    When you want a single variable to hold related data of

    various data types, you can create a structure.

    To create a structure you use the struct keyword.

    The following code shows the example of a declaring astructure names Bill_Details:

    struct Bill_Details

    { public string inv_No; // Invoice Number

    string ord_Dt; // Order Date

    string custName; // Customer name

    Using Structure

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    8/32Slide 8 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    public string product; // Product Name

    public double cost; // Cost of the product

    public double due_Amt; // Total amount due

    }

    Using Structure (Contd.)

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    9/32Slide 9 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Enumeration is a value type data type.

    Enumeration contains its own values and cannot inherit or

    pass inheritance.

    Enumerators allows you to assign symbolic names to

    integral constants.To enumerate, you can use the enum keyword.

    Using Enumeration

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    10/32

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    11/32Slide 11 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    After declaring the enumeration type, you can use the

    enumeration type in the same manner as any other data

    type:

    int First_Day = (int)Days.Sat;

    int Last_Day = (int)Days.Fri;

    Implementing Enumerations

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    12/32Slide 12 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Implementing Arrays

    An array is a collection of values of the same data type.

    The following figure shows the array structure in the

    systems memory.

    Index

    Value 0

    Index

    Value 6

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    13/32Slide 13 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Declaring an Array

    An array needs to be declared before it can be used in a

    program.

    You can declare an array by using the following statement:

    datatype[] Arrayname;

    Let us understand the explanation of the various elements

    of the array declaration through an example.

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    14/32Slide 14 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Declaring an Array (Contd.)

    int[ ] Score; Datatype

    Is used to specify the

    data type for the

    elements

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    15/32Slide 15 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Declaring an Array (Contd.)

    int[ ] Score; [ ]

    Is used to specify the

    rank of the array

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    16/32Slide 16 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Declaring an Array (Contd.)

    int[ ] Score; Arrayname

    Is used to specify the

    name of the array using

    which the elements of the

    array will be initialized and

    manipulated

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    17/32Slide 17 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    In C#, you can initialize the array variable and can assign

    values to the array elements. In addition, you can copy the

    array variable to another variable.

    During initialization, you need to use the new keyword to

    create an instance of the array. In addition, the size of the

    array is also specified while it is initialized.

    The following is an example of array initialization:

    int[] Score; // Array declaration

    Score = new int[10]; //Array Instance

    Initializing and Assigning Values to Array

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    18/32

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    19/32Slide 19 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    When an array is initialized, you can access the element

    values and manipulate them.

    The foreach loop is specially used for manipulating arrays.

    The following is the syntax of the foreach loop:

    foreach (type identifier in expression){

    //statements

    }

    Manipulating Array Elements

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    20/32Slide 20 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    The following is the example offoreach loop:

    int[] Numbers = { 4, 3, 2, 1, 0, -1, -2, 9, 5 };

    Console.WriteLine("The Contents of an Array

    is:");

    foreach (int K in Numbers)

    {

    Console.WriteLine("{0} \t",K);

    }

    Manipulating Array Elements (Contd.)

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    21/32Slide 21 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    While declaring a method if you are not sure about the

    number of arguments passed as a parameter, you can use

    the param array.

    The following is the example ofparam array:

    public int Adding_ArrayElement(params int[] List)

    {

    int Total = 0;

    foreach ( int I in List )

    {

    Total += I;}

    return Total;

    }

    Manipulating Array Elements (Contd.)

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    22/32Slide 22 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Demo: Matrix Subtraction UsingArrays

    Problem Statement:

    David, a student of California University, is currently pursuing

    mathematics. He is working on a project named Matrix

    Subtraction. He needs to perform the following tasks for his

    project:

    Accept data in two Arrays.

    Perform the subtraction operation.

    Verify the value of subtraction.

    Help David to create the C# program using Visual Studio IDE.

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    23/32

    Slide 23 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Solution:

    To solve the preceding problem, David needs to perform the

    following tasks:

    1. Create a console-based application for Matrix Subtraction.

    2. Build and execute an application.

    Demo: Matrix Subtraction Using Arrays (Contd.)

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    24/32

    Slide 24 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Multidimensional Arrays

    The rank value of the array is also known as the dimension

    of the array.

    The array can be single dimensional or multidimensional.

    Single dimensional array stores data in a row.

    Multidimensional array stores data using differentdimensions.

    The following figure is a graphical representation of values

    stored in a single dimensional array and a multidimensional

    array.int [] Num; int[,] Num;

    0

    0 1 2 3 4 1

    0 1 2 3 4

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    25/32

    Slide 25 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Multidimensional Arrays (Contd.)

    The Array class is the base class for all the arrays in C#.

    The Array class provides properties and methods to work

    with arrays.

    Properties: The following table explains some of the most

    commonly used properties of the Array class.

    Properties Explanation

    Length Returns the total number of items in all the dimensions of anarray

    Rank Returns the total number of items in all the dimensions of anarray

    IsFixedSize Return a value indicating if an array has a fixed size or not

    IsReadOnly Returns a value indicating if an array is read-only or not

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    26/32

    Slide 26 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Multidimensional Arrays (Contd.)

    Methods: The following table explains some of the most

    commonly used methods of the Array class.

    Properties Explanation

    Sort Performs sort operation on an array passed to it as a parameter

    Clear Removes all items of an array and sets a range of items in thearray to 0

    GetLength Returns the number of items in an Array

    GetValue Returns the value of the specified item in an Array

    IndexOf Returns the index of the first occurrence of a value in aone-dimensional Array or in a portion of the Array

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    27/32

    Slide 27 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Using Collections

    Arrays are used to collect the elements of same data type.

    The .NET Framework provides several classes that also

    collect elements in specialized ways. Theses classes are

    the Collection classes, and are declared in the

    System.Collections namespace and sub-namespaces.

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    28/32

    Slide 28 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Using Collections (Contd.)

    The collection classes accept, hold, and return their

    elements as items.

    The element type of collection class is an object. Object is a

    reference type.

    The following figure shows the storage of values in array oftype int:STACK HEAP

    9 7 3 2

    Array

    @

    int [] array= {9, 7, 3, 2};

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    29/32

    Slide 29 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Using Collections (Contd.)

    The action which automatically converts the value type to

    the reference type, is known as boxing.

    The following figure shows the boxing technique.

    STACK HEAP

    @ @ @ @

    Array

    @

    int [] array= {9, 7, 3, 2};

    7 2

    39

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    30/32

    Slide 30 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    Using Collections (Contd.)

    When you want to access the elements of an array through

    its index value location in an array, use an ArrayList

    class.

    The following table describes the use of various methods of

    an ArrayList class.

    Method Use

    Add Adds an object to the end of the ArrayList

    Remove Removes the element at the first occurrence of a specific object from theArrayList

    Clear Removes all the elements from the ArrayList

    Insert Inserts an element into the ArrayList at the specified indexTrimToSize Sets the capacity to the actual number of elements in the ArrayList

    Sort Sorts the elements in the ArrayList

    Reverse Reverses the element in the ArrayList

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    31/32

    Slide 31 of 32Session 7Ver. 1.0

    Object-Oriented Programming Using C#

    In this session, you learned that:

    Memory allocated to variables are of two types, value type and

    reference type.

    Value-types are the simplest types in C#. Variables of value

    types directly contain their data in the variable.

    Reference-types variables contain only a reference to data. Thedata is stored in a separate memory area.

    A value type variable holds its value in the stack.

    A reference type variable holds a reference to an object in the

    heap.

    To hold related data of various data type in single variable,structures are used.

    C# provides the features ofenum to create user defined data

    types with numbers as an index value to access them.

    An array is a collection of values of the same data type.

    Summary

  • 8/14/2019 05 Iec t1s1 Oops Session 07

    32/32

    Object-Oriented Programming Using C#

    The foreach statement interprets the common loop process

    and removes the need for you to check the array size.

    Param arrays are used in the methods with parameter list

    when the total number of parameters is not known.

    The .NET Framework provides several classes that also collect

    elements together in other specialized ways. These are theCollection classes, and they live in the System

    namespace.

    ArrayList is useful when you want to manipulate the values

    in an array easily.

    Summary (Contd.)