50
Q3M1 – OOP C# Dudy Fathan Ali S.Kom Value Types & Reference Types Q3M1 Dudy Fathan Ali, S.Kom (DFA) 2015 CEP - CCIT Fakultas Teknik Universitas Indonesia

Object Oriented Programming - Value Types & Reference Types

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming - Value Types & Reference Types

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Value Types & Reference TypesQ3M1

Dudy Fathan Ali, S.Kom (DFA)2015

CEP - CCITFakultas Teknik Universitas Indonesia

Page 2: Object Oriented Programming - Value Types & Reference Types

Introduction

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

o The variables in a program are allocated memory at run time in the system.

o Variables are referred in two ways, value type and reference type.

o Value type variables contain data, whereas reference type variables hold the reference to the memory location where data is stored.

Page 3: Object Oriented Programming - Value Types & Reference Types

Describing Memory Allocation

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

The memory allocated to variables is referred to in two ways, value types and reference types.

- OOP C#, NIIT Courseware MMSv2.

int Num1 = 50;int Num2 = 100;

Example :

50

Value types are also called direct types because they contain data.

100

Num1

Num2

Memory Allocated for Value Type Variable

Page 4: Object Oriented Programming - Value Types & Reference Types

Describing Memory Allocation

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

The memory allocated to variables is referred to in two ways, value types and reference types.

- OOP C#, NIIT Courseware MMSv2.

int Num1 = 50;int Num2 = Num1;Console.WriteLine(Num1);Console.WriteLine(Num2);

Another Example :

50

50

Num1

Num2

Value types are also called direct types because they contain data.

Memory Allocated for Value Type Variable

Page 5: Object Oriented Programming - Value Types & Reference Types

Describing Memory Allocation

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

The memory allocated to variables is referred to in two ways, value types and reference types.

- OOP C#, NIIT Courseware MMSv2.

int Num1 = 50;int Num2 = Num1;Console.WriteLine(Num1);Num1++;Console.WriteLine(Num2);

Another Example :

Output:

5050

incrementing Num1 will have no effect on Num2

Value types are also called direct types because they contain data.

Memory Allocated for Value Type Variable

Page 6: Object Oriented Programming - Value Types & Reference Types

Describing Memory Allocation

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

class Car{ public int Model; public void Display_Model() { Console.WriteLine (Model); }}

class Program{ static void Main (string[] args) { Car Ford = new Car (); Ford.Model = 10; Car Mercedes = Ford; Ford.Display_Model(); Mercedes.Display_Model (); }}

Memory Allocated for Reference Type Variable

****

Ford

****

Mercedes10

**** Output:

1010

Page 7: Object Oriented Programming - Value Types & Reference Types

Describing Memory Allocation

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

class Car{ public int Model; public void Display_Model() { Console.WriteLine (Model); }}

class Program{ static void Main (string[] args) { Car Ford = new Car (); Ford.Model = 10; Car Mercedes = Ford; Ford.Display_Model(); Ford.Model++; Mercedes.Display_Model (); }}

Memory Allocated for Reference Type Variable

****

Ford

****

Mercedes10

****

11

Incrementing Form.Model will changing the value of Mercedes.Model.

Page 8: Object Oriented Programming - Value Types & Reference Types

Enumeration

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Enumeration is a value type data type, it allows you to assign symbolic names to integral constants.

- OOP C#, NIIT Courseware MMSv2.

enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri };

To declare an enumeration type called Days, where the values are restricted to thesymbolic names of the weekdays, use the following code:

Code :

Integral Constant

0 1 2 3 4 5 6

Symbolic Name

Sat Sun Mon Tue Wed Thu Fri

Representation :

Page 9: Object Oriented Programming - Value Types & Reference Types

Enumeration

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

class EnumTest{ enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri }; static void Main(string[] args) { int First_Day = (int)Days.Sat; int Last_Day = (int)Days.Fri; Console.WriteLine(“Sat = ” + First_Day); Console.WriteLine(“Fri = ” + Last_Day); Console.ReadLine(); }}

Integral Constant

0 1 2 3 4 5 6

Symbolic Name

Sat Sun Mon Tue Wed Thu Fri

Full Code :

Representation :

Output:

Sat = 0Fri = 6

Page 10: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

An array is a collection of values of the same data type. Array elements are accessed using a single name and an index number representing the position of the element within the array. Array is a reference type data type.

Array Structure

Page 11: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

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 :

<data type>[] <array name>

Code Structure :

String[] DaftarNama;

Example Code :

String[] DaftarNama = new String[2];DaftarNama[0] = “Bambang”;DaftarNama[1] = “Suprapto”;

Initializing and Assigning Value :

Page 12: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

string[] n1 = new string[5];Data Dalam Memori :

Data Dalam Array :

i=0

Indeks di mulai dari 0

Assigning Value

Page 13: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

string[] n1 = new string[5];Data Dalam Memori :

Data Dalam Array :

i=0Masuk Nilai “1”

Assigning Value

Page 14: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1

string[] n1 = new string[5];

n1[0] = “1”;

Data Dalam Memori :

i=0

a

1 a

Assigning Value

Page 15: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1

string[] n1 = new string[5];

n1[0] = “1”;

Data Dalam Memori :

i=0

a

1 a

Masuk Nilai “5”

a

Assigning Value

Page 16: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

a

b

Assigning Value

Page 17: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

Masuk Nilai “7”

a

b

Assigning Value

Page 18: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5 7

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

n1[2] = “7”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

c

i=2

7 c

a

b

c

Assigning Value

Page 19: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5 7

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

n1[2] = “7”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

c

i=2

7 c

a

b

c

Masuk Nilai “3”

Assigning Value

Page 20: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5 7 3

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

n1[2] = “7”;

n1[3] = “3”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

c

i=2

7 c

a

b

c

i=3

d

d

3 d

Assigning Value

Page 21: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5 7 3

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

n1[2] = “7”;

n1[3] = “3”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

c

i=2

7 c

a

b

c

i=3

d

d

3 d

Masuk Nilai “9”

Assigning Value

Page 22: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5 7 3 9

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

n1[2] = “7”;

n1[3] = “3”;

n1[4] = “9”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

c

i=2

7 c

a

b

c

i=3

d

d

3 d

e

e

e9

i=4

Assigning Value

Page 23: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5 7 3 9

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

n1[3] = “7”;

n1[4] = “3”;

n1[5] = “9”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

c

i=2

7 c

a

b

c

i=3

d

d

3 d

e

e

e9

i=4Bisa diisi lagi?

Assigning Value

Page 24: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

1 5 7 3 9

string[] n1 = new string[5];

n1[0] = “1”;

n1[1] = “5”;

n1[3] = “7”;

n1[4] = “3”;

n1[5] = “9”;

Data Dalam Memori :

i=0

a

1 a

i=1

b

5 b

c

i=2

7 c

a

b

c

i=3

d

d

3 d

e

e

e9

i=4

Tidak! Karena Array PENUH.

Assigning Value

Page 25: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar :

x = 5

Page 26: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar :

x = 5

i=0

Page 27: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar :

x = 5

i=0 i < x

Y

Page 28: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar :

x = 5

n[0] = 1i=0 i < x

Y

Page 29: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1

x = 5

n[0] = 1 Tampil “1”i=0 i < x

Y

Page 30: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1

n[0] = 1 Tampil “1” i = i+ 1i=0

x = 5

i < x

Y i yang baru = 1

Page 31: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1

x = 5

i=1 i < x

Y

Page 32: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1

x = 5

n[1] = 5i=1 i < x

Y

Page 33: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15

x = 5

n[1] = 5 Tampil “5”i=1 i < x

Y

Page 34: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15

n[1] = 5 Tampil “5” i = i+ 1i=1

x = 5

i < x

Y i yang baru = 2

Page 35: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15

x = 5

i=2 i < x

Y

Page 36: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15

x = 5

n[2] = 3i=2 i < x

Y

Page 37: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 153

x = 5

n[2] = 3 Tampil “3”i=2 i < x

Y

Page 38: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 153

n[2] = 3 Tampil “3” i = i+ 1i=2

x = 5

i < x

Y i yang baru = 3

Page 39: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 153

x = 5

i=3 i < x

Y

Page 40: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 153

x = 5

n[3] = 8i=3 i < x

Y

Page 41: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1538

n[3] = 8 Tampil “8”i=3

x = 5

i < x

Y

Page 42: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1538

n[4] = 4 Tampil “4” i = i+ 1i=3

x = 5

i < x

Y i yang baru = 4

Page 43: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1538

i=4

x = 5

i < x

Y

Page 44: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 1538

n[4] = 4i=4

x = 5

i < x

Y

Page 45: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15384

n[4] = 4 Tampil “4”i=4

x = 5

i < x

Y

Page 46: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15384

n[4] = 4 Tampil “4” i = i+ 1i=4

x = 5

i < x

Y i yang baru = 5

Page 47: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15384

i=5

x = 5

i < x

N

Page 48: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Display Array Value

string[] n1 = new string[5]n1[0] = 1;n1[1] = 5;n1[2] = 3;n1[3] = 8;n1[4] = 4;

int x,i;x = n1.length();i = 0;for(i=0;i<x;i++){ Console.WriteLine(n1[i]);}Console.ReadLine();

Data array

int x,i

i++

Y

N

next command

x = n1.lengthi = 0

i < x

Tampil nilai “i”

1 5 3 8 4i=0 i=1 i=2 i=3 i=4

Tampil Di Layar : 15384

Data telah tampil semua

Page 49: Object Oriented Programming - Value Types & Reference Types

Array

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Latihan Mandiri

1. Buatlah array untuk menyimpan sejumlah nama siswa (jumlah siswa berdasarkan inputan user)

2. Buatlah 2 array untuk menyimpan angka ganjil dan angka genap dari 0 – 20 dan

3. Jumlahkanlah isi dari array ganjil dan array genap4. Jumlahkanlah array ganjil dan genap dengan index yang sama

kemudian masukkan hasil penjumlahannya ke dalam array yang baru

5. Hasil dari ke empat latihan di atas harus bisa ditampilkan

Page 50: Object Oriented Programming - Value Types & Reference Types

Q3M1 – OOP C# Dudy Fathan Ali S.Kom

Thank You!Dudy Fathan Ali S.Kom

[email protected]

Credits array content by : Yaddarabullah S.Kom M.Kom