0% found this document useful (0 votes)
1 views30 pages

Arrays

The document provides an overview of arrays, strings, and enumerations in C#, detailing control statements, loop constructs, and array types including single-dimensional, multi-dimensional, and jagged arrays. It also covers string methods and formatting, as well as the definition and usage of enumerations. Key concepts include array instantiation, string manipulation methods, and the characteristics of enumerations.

Uploaded by

vikaslaps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views30 pages

Arrays

The document provides an overview of arrays, strings, and enumerations in C#, detailing control statements, loop constructs, and array types including single-dimensional, multi-dimensional, and jagged arrays. It also covers string methods and formatting, as well as the definition and usage of enumerations. Key concepts include array instantiation, string manipulation methods, and the characteristics of enumerations.

Uploaded by

vikaslaps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Arrays , Strings & Enumerations

Overview

Iterative Statements

Arrays Implementation in C#

Strings

Enumerations

All Rights Reserved with Trendz IT Ltd


2
Control Statements
Loop s are used to execute one or more lines of
code repetitively

The following loop constructs are supported by


C#
The while loop

The do-while loop

The for loop

The for each loop


All Rights Reserved with Trendz IT Ltd
3
While Loop
The while loop construct is used to execute a
block of statements for definite number of times
depending on the condition

The following is the syntax for while expression


while(expression)

statements;

All Rights Reserved with Trendz IT Ltd


4
do- while Loop
The do-while loop construct is similar to the while loop
construct

Statements are executed at least once

The following is the syntax for do-while expression


do

statements;

} while(expression);
All Rights Reserved with Trendz IT Ltd
5
The for loop
The for loop structure is used to execute a block of
statements for a specific no of times

The following is the syntax of the for loop construct


for(initialization ; condition ; increment/decrement)

statements;

6
Break and Continue
The break statement is used to exit from the loop
and prevents the execution of the remaining loop

The continue statement is used to skip all the


subsequent instructions and take the control back
to the loop

7
Break and Continue contd…
int evencount = 0, i = 0;
for(i = 0; ; i++)
{
if(i >= 60)
break; // Terminate the for loop
if((i % 2) != 0)
continue; // Will skip the reminder of the for loop
evencount++;
}
System.Console.WriteLine("Total Even Numbers Between 0 – 60 is:
{0}", evencount);

8
Arrays in C#
Array is a collection of values of same data type

The following figure shows the array structure in


system memory

9
Arrays in C# contd…

There are 3 types of arrays


Single-Dimensional

Two-Dimensional/Multi-Dimensional

Jagged Array

10
Array Instantiation
Datatype [] Array_name=new Data type[size];

Data type: specifies what type of data is stored

Array_name: Indicates name of the array

new: It allocates memory to the array at runtime

Size: Indicates number of elements

11
Array Instantiation

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

string []names=new string[3];

int []data=new int[5]{10,20,30,40,50} ;

12
Arrays in C# contd…

13
Arrays in C# contd…
Assigning values to array
int []a= new int[4]
a[0]=10

a[1]=20

a[2]=30

a[3]=40

int []a=new int[4]{10,20,30,40}

int []a={10,20,30,40} //Dynamic allocation

a[index]- returns the array value at particular index

14
System.Array Class
sort() : It sorts array elements in ascending order.
System.Array.Sort(array name)

reverse(): It arrange array elements in reverse order


System.Array.Reverse(array name)

copy() : It copies one Array elements into to another array


System.Array.Copy(source array name, destination array name,
count)

Length: It returns size of the array


Rank: It returns dimension of the array

15
Two-Dimensional Array
It stores values in one or more columns

Has rows and columns


Ex: int [,] table=new table[4,3];

16
Two-Dimensional Array contd…

Assigning Values to 2-Dimensional Arrays


int [,] a=new int[2,2]{{12,32},{34,54}};
int[,]table =new table[4,3] {
{400, 450, 510},
{500, 560, 630},
{625, 676, 740},
{1000, 1250, 1600}
};

17
Jagged Arrays
Jagged arrays are often called array of arrays

An element of a jagged array itself is an array


Datytype [][] Array_name = new Datatype[size][] ;

Size indicates no of arrays stored in jagged array


Ex: int [][]j _array =new int[3][];

j_array[0]=new int[] {1,2,3,4};

j_array[1]=new int[] {5,6,7};

j_array[2] =new int[] { 8,9};


18
JAM
1.What is an array ?
A. Collection of similar data type elements

2.Types of arrays?
A. Single Dimensional , Multi Dimensional
Jagged Arrays

3.What are the members of Arrays Class ?


A. Sort() , Reverse() , Copy() , Rank , Length

19
Strings in c#
String is a collection of characters
Memory is allocated on the heap
string s=“abc”;
string s1 = @"D:/Sample/ConsoelAPp“;
String is a predefined reference type
Strings are Immutable
Strings have fixed size
String memory cannot be changed dynamically

20
Methods of String Class
copy()- copies one string into another
String. Copy(value)
concat()-concats two strings
String.Concat(s1,s2)
compare()-compares two strings
compare(string s1,string s2,bool ignorecase)
Equal(String s)- Compares two strings and return
boolean value
split()-splits string into substrings
string []s=s1.split('separator',' separator',....)

21
Methods of String Class
indexof("char")-finds the index of given char
indexof(“string”)-finds the index of given string
contains("substring")-checks if substring exists in the
string
tolower()-returns string in lowercase
toupper()-returns string in uppercase
TocharArray()-converts string to char array
replace("old string", "new string")- replaces old string
with new string
replace('old char', 'new char')-replaces old char with
new char

22
Formatting strings
Formatting strings are used to format the output
{0:Fn}-To display fixed decimal points n=1,2,….
{0:E}-To display output in exponential format
{0:C}-To display in currency format-$123
{0:P}-To display in percentage format-123%
{0:D}-To display date in long format-March 5 2011
{0:d}-To display date in small format-02/5/2011
{0:T}-To display time in long format-12:23:12
{0:t}-To display time in small format-12:23

23
Enumerations
Enumeration is the set of named constants
It is a user defined value type
enum keyword is used to declare enumeration
enumeration type is any integral type like int ,long
,byte short
The default type of the enumeration list elements is
int

24
Enumerations contd…
enum enumname:datatype {enumeration list};
default data type is int
Ex:enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
In Enumeration list the items are called enumerators
The value of the first enumerator is 0 .
The value of the each successive enumerator is
increased by 1

25
Enumerations contd…
Enumerator can be initialized to override the default
value
Ex: enum Days{sat=1,sun,mon,tue,wed,thu}

Assigning enumerator value to variables


int i= (int) Days.sat [returns value]

Accessing the enumerator name to variables


string name= Days.sat [returns name]

26
Enumerations contd…

Enum is a predefined class for enumerations


under system library.
predefined methods in System. Enum

getnames()-it returns all names in enumeration list


getvalues()- it returns all values in enumeration list

27
Quiz
1.How can you sort the elements of the array in descending
order?
A.By calling Sort() and then Reverse() methods. B.By
calling Reverse() and then Sort() methods. C .Both D. None
A. A

2.Which of these statements correctly declares a


two-dimensional array in C#?
A.int[,] myArray; B.int[][] myArray; C.int[2] myArray;
D.System.Array[2] myArray
A. A

3.Which one is not user defined data type?


A.Class B String C.Struct D.Enumerator
A. B
28
CYU

1.What is an Enum?
A.An enum is a value type with a set of related named constants
often referred to as an enumerator list. The enum keyword is used to
declare an enumeration. It is a primitive data type, which is user
defined.

2.Members of String Class


A.Concat() , Split() , Join()

3.How to find size of the String?


A.Using length property

4.Strings are mutable of immutable?


A.Strings are immutable

29
30

You might also like