Window Programming Unit 3 Function Array
Window Programming Unit 3 Function Array
Unit III
Functions, Arrays and Strings
* String –
In C#, string is an object of [Link] class that represents sequence of characters.
We can perform many operations on strings such as concatenation, comparison, getting
substring, search, trim, replacement etc.
string vs String –
In C#, string is keyword which is an alias for [Link] class. That is why string and
String are equivalent. We are free to use any naming convention.
string s1 = “abc”;
String s2 = “def”;
a) C# String Property –
1) Length –
Gets the number of characters in the current String object.
Syntax –
[Link] // s1 is string object
b) C# String methods –
3) Contains(string s1) –
The C# Contains( ) method is used to return a value indicating whether the specified
substring occurs within this string or not. If the specified substring is found in this string, it
returns true otherwise false.
C#.Net akshaypawle@[Link] 1
4) Copy(string s1) –
The C# Copy( ) method is used to create a new instance of String with the same value as
a specified String. Its return type is string.
9) ToLower( ) –
The C# ToLower( ) method is used to convert a string into lowercase. It returns a string.
10) ToString( ) –
The C# ToString( ) method is used to return instance of String. It returns a string object.
11) ToUpper( ) –
The C# ToUpper( ) method is used to convert string into uppercase. It returns a string.
12) Trim( ) –
The C# Trim( ) method is used to remove all leading and trailing white-space characters
from the current String object.
C#.Net akshaypawle@[Link] 2
namespace stringExample
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
C#.Net akshaypawle@[Link] 3
String s2 = [Link];
string s3 = [Link];
//Compare
txt_compare.Text = ([Link](s1, s2)).ToString( );
//Concat
txt_concat.Text = [Link](s1, s2);
//Contains
txt_contains.Text = ([Link](s2)).ToString( );
//Copy
txt_copy.Text = [Link](s1);
//Equals
txt_equals.Text = ([Link](s1, s2)).ToString( );
//replace
txt_replace.Text = [Link](s1, s2);
//Tolower
txt_tolower.Text = [Link]( );
//Toupper
txt_toupper.Text = [Link]( );
//remove
txt_remove.Text = [Link](5, 6);
//substring
txt_substring.Text = [Link](9,2);
//Tostring
int n = 123;
txt_tostring.Text = [Link]( );
C#.Net akshaypawle@[Link] 4
//Trim
string s4 = " India is my country ";
txt_trim.Text = [Link]( );
}
* C# Function –
Function is a block of code that perform specified task. Function is used to execute
statements specified in the code block. To use a method, you need to −
1) Define the method
2) Call the method
1) Defining Method in C# -
The syntax for defining a method in C# is as follows –
Syntax –
<Access_Specifier> <Return_Type> Function_Name (<Parameter_List>)
{
// function body
// return statement
}
Where,
Access_Specifier: It is used to specify function accessibility in the application.
Return_Type: It is used to specify the data type of function return value.
Function_Name: It is a unique name that is used to make Function call.
Parameter_List: It is a list of arguments that we can pass to the function during call.
Function_Body: It is a block that contains executable statements.
C#.Net akshaypawle@[Link] 5
2) Calling Method in C# -
Where,
Function_Name: It is a name through which function is call.
Parameter_List: It is a list of arguments that we can receive through the function call.
1) Call By Value –
In C#, value-type parameters are that pass a copy of original value to the function rather
than reference. It does not modify the original value. A change made in passed value does not
alter the actual value.
2) Call By Reference –
C# provides a ref keyword to pass argument as reference-type. It passes reference of
arguments to the function rather than copy of original value. The changes in passed values are
permanent and modify the original variable value.
3) Out Parameter –
C# provides out keyword to pass arguments as out-type. It is like reference-type, except
that it does not require variable to initialize before passing. We must use out keyword to pass
argument as out-type. It is useful when we want a function to return multiple values.
namespace functionExample
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
C#.Net akshaypawle@[Link] 6
C#.Net akshaypawle@[Link] 7
txt_b.Text = [Link]( );
}
}
}
Output -
* Introduction –
Like other programming languages, array in C# is a group of similar types of elements
that have contiguous memory location. In C#, array is an object of base type [Link]. In
C#, array index starts from 0. We can store only fixed set of elements in C# array.
C#.Net akshaypawle@[Link] 8
Syntax –
Data_Type[ ] Array_Name = new Data_Type[size];
Examples –
int[ ] arr = new int[5];
string[ ] names = new string[2];
Syntax –
Data_Type[ , ] Array_Name = new Data_Type[size];
Examples –
int[ , ] numbers = new int[3, 2];
C#.Net akshaypawle@[Link] 9
* Array Class –
The Array class is the base class for all the arrays in C#. It is defined in the System
namespace. The Array class provides various properties and methods to work with arrays.
Properties –
1) Length –
Gets an integer that represents the total number of elements in all the dimensions of the
Array.
2) Rank –
Gets the rank (number of dimensions) of the Array.
Methods –
1) GetValue(int index) –
It returns the value for a given index in an array.
4) Reverse(Array arrayName) –
Reverses the sequence of the elements in the entire one-dimensional Array.
5) Sort(Array arrayName) –
It is used to sort the elements in an entire one-dimensional Array.
C#.Net akshaypawle@[Link] 10
namespace arrayClass
{
public partial class Form1: Form
{
int[ ] no = new int[5] { 20, 40, 50, 10, 30 };
public Form1( )
{
InitializeComponent( );
}
C#.Net akshaypawle@[Link] 11
//indexof method
txt_indexof.Text = ([Link](no, 50)).ToString( );
//setvalue method
[Link](25, 2);
for (int i = 0; i < [Link]; i++)
{
txt_setvalue.Text = txt_setvalue.Text + no[i] + " ";
}
//copy method
int[ ] m = new int[5];
[Link](no, m, 5);
for (int i = 0; i < [Link]; i++)
{
txt_copy.Text = txt_copy.Text + m[i] + " ";
}
//reverse method
[Link](no);
for (int i = 0; i < [Link]; i++)
{
txt_reverse.Text = txt_reverse.Text + no[i] + " ";
}
C#.Net akshaypawle@[Link] 12
* ArrayList class –
ArrayList is a non-generic type of collection in C#. It can contain elements of any data
types. It is similar to an array, except that it grows automatically as you add items in it. Unlike
an array, you don't need to specify the size of ArrayList.
Note – Need to add [Link] namespace to use ArrayList class in the project.
Syntax –
ArrayList array_list_name = new ArrayList( );
Example –
ArrayList myList = new ArrayList( );
You can also add items when you initialize it using object initializer syntax.
Property –
1) Count –
It gets the number of elements actually contained in the ArrayList.
Methods –
1) Add(Object value) –
This method is used to add a single element at the end of ArrayList.
C#.Net akshaypawle@[Link] 13
3) Remove(Object value) –
This method is used to remove a specified element from an ArrayList.
4) RemoveAt(int index) –
This method is used to remove an element from the specified index location.
5) Contains(Object value) –
This method checks whether specified element exists in the ArrayList or not. Returns true
if exists otherwise false.
6) Reverse( ) –
This method arranges elements in reverse order. Last element at zero index and so on.
7) Sort ( ) –
This method arranges elements in ascending order. However, all the elements should
have same data type so that it can compare with default comparer otherwise it will throw
runtime exception.
C#.Net akshaypawle@[Link] 14
namespace arrayListClass
{
public partial class Form1 : Form
{
ArrayList myList = new ArrayList( ) { 10, "AMOL" };
public Form1( )
{
InitializeComponent( );
}
//add method
[Link](12.5);
for (int i = 0; i < [Link]; i++)
{
txt_add.Text = txt_add.Text + myList[i] + " ";
}
//insert method
[Link](1, "ATUL");
for (int i = 0; i < [Link]; i++)
{
C#.Net akshaypawle@[Link] 15
//remove method
[Link]("ATUL");
for (int i = 0; i < [Link]; i++)
{
txt_remove.Text = txt_remove.Text + myList[i] + " ";
}
//removeat method
[Link](1);
for (int i = 0; i < [Link]; i++)
{
txt_removeat.Text = txt_removeat.Text + myList[i] + " ";
}
//contains method
txt_contains.Text = ([Link](12.5)).ToString( );
//reverse method
[Link]( );
for (int i = 0; i < [Link]; i++)
{
txt_reverse.Text = txt_reverse .Text + myList[i] + " ";
}
//sort method
ArrayList newList = new ArrayList( ) { 30, 10, 20 };
[Link]( );
for (int i = 0; i < [Link]; i++)
{
txt_sort.Text = txt_sort.Text + newList[i] + " ";
}
}
}
}
C#.Net akshaypawle@[Link] 16
* Jagged Array –
A jagged array is an array whose elements are arrays. The elements of a jagged array can
be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
Syntax –
Data_Type[ ][ ] Array_Name = new Data_Type[size][dimension];
A jagged array is initialized with two square brackets [ ][ ]. The first bracket specifies the
size of an array and the second bracket specifies the dimension of the array which is going to be
stored as values. (Remember, jagged array always store an array.)
Example –
The following is a declaration of a single-dimensional array that has two elements, each
of which is a single-dimensional array of integers:
Before you can use jaggedArray, its elements must be initialized. You can initialize the
elements like this:
jaggedArray[0][0] = 10; // Assign 10 to the first element ([0]) of the first array ([0])
jaggedArray[0][1] = 20; // Assign 20 to the second element ([1]) of the first array ([0])
jaggedArray[0][2] = 30; // Assign 30 to the third element ([2]) of the first array ([0]):
jaggedArray[1][0] = 40; // Assign 40 to the first element ([0]) of the second array ([1]):
jaggedArray[1][1] =50; // Assign 50 to the second element ([1]) of the second array ([1]):
C#.Net akshaypawle@[Link] 17
namespace JaggedArrayExample
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}
private void btn_display_Click(object sender, EventArgs e)
{
// Declare the array of two elements:
int[][] jaggedArray = new int[2][];
jaggedArray[0][0] = 10;
jaggedArray[0][1] = 20;
jaggedArray[0][2] = 30;
jaggedArray[1][0] = 40;
jaggedArray[1][1] = 50;
C#.Net akshaypawle@[Link] 18
C#.Net akshaypawle@[Link] 19