Arrays
Arrays
Overview
Iterative Statements
Arrays Implementation in C#
Strings
Enumerations
statements;
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
statements;
6
Break and Continue
The break statement is used to exit from the loop
and prevents the execution of the remaining 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
9
Arrays in C# contd…
Two-Dimensional/Multi-Dimensional
Jagged Array
10
Array Instantiation
Datatype [] Array_name=new Data type[size];
11
Array Instantiation
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
14
System.Array Class
sort() : It sorts array elements in ascending order.
System.Array.Sort(array name)
15
Two-Dimensional Array
It stores values in one or more columns
16
Two-Dimensional Array contd…
17
Jagged Arrays
Jagged arrays are often called array of arrays
2.Types of arrays?
A. Single Dimensional , Multi Dimensional
Jagged Arrays
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}
26
Enumerations contd…
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
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.
29
30