0% found this document useful (0 votes)
3 views19 pages

Window Programming Unit 3 Function Array

Unit III of the B.Sc.(CS) program covers Functions, Arrays, and Strings in C#. It explains string operations, methods for string manipulation, function definitions, and parameter passing techniques, along with the Array and ArrayList classes. The document includes example programs demonstrating these concepts.

Uploaded by

dipakkhillare040
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)
3 views19 pages

Window Programming Unit 3 Function Array

Unit III of the B.Sc.(CS) program covers Functions, Arrays, and Strings in C#. It explains string operations, methods for string manipulation, function definitions, and parameter passing techniques, along with the Array and ArrayList classes. The document includes example programs demonstrating these concepts.

Uploaded by

dipakkhillare040
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

lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

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 –

1) Compare(string s1, string s2) –


The C# Compare( ) method is used to compare first string with second string. It returns
an integer value. If both strings are equal, it returns 0. If first string is greater than second string,
it returns 1 else it returns -1.

2) Concat(string s1, string s2) –


It is used to concatenate two specified instances of String. It returns a string object.

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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

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.

5) Equals(string s1, string s2) –


The C# Equals( ) method is used to check whether two specified String objects have the
same value or not. If both strings have same value, it return true otherwise false.

6) Remove(Int beginIndex, Int length) –


The C# Remove( ) method is used to get a new string after removing all the characters
from specified beginIndex till given length. If length is not specified, it removes all the
characters after beginIndex.

7) Replace(string s1, string s2) –


The C# Replace( ) method is used to get a new string in which all occurrences of a
specified Unicode character in this string are replaced with another specified Unicode character.
It returns a string.

8) Substring(Int startIndex, Int length) –


The C# SubString( ) method is used to get a substring from a String. The substring starts
at a specified character position and continues to the end of the string. It returns a 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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

Program – Write a program to demonstrate string methods.

namespace stringExample
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}

private void btn_stringclass_Click(object sender, EventArgs e)


{
string s1 = [Link];

C#.Net akshaypawle@[Link] 3

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

//Trim
string s4 = " India is my country ";
txt_trim.Text = [Link]( );
}

private void btn_exit_Click(object sender, EventArgs e)


{
[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.

Access_Specifier, Parameter_List and return statement are optional.

C#.Net akshaypawle@[Link] 5

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

2) Calling Method in C# -

Function_Name (<Parameter_List >);

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.

* Passing Parameters to a Function –

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.

Program – Write a program to demonstrate different types of passing parameters to a


function (pass by value, pass by reference & out parameter).

namespace functionExample
{
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
}

C#.Net akshaypawle@[Link] 6

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

private void btn_value_Click(object sender, EventArgs e)


{
int a = 10;
disp_value(a);
txt_a.Text = [Link]( );
}

public void disp_value(int b)


{
b++;
txt_b.Text = [Link]( );
}

private void btn_ref_Click(object sender, EventArgs e)


{
int a = 10;
disp_ref(ref a);
txt_a.Text = [Link]( );
}

public void disp_ref(ref int b)


{
b++;
txt_b.Text = [Link]( );
}

private void btn_out_Click(object sender, EventArgs e)


{
int a;
disp_out(out a);
txt_a.Text = [Link]( );
}

public void disp_out(out int b)


{
b = 10;

C#.Net akshaypawle@[Link] 7

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

txt_b.Text = [Link]( );
}

private void btn_clear_Click(object sender, EventArgs e)


{
txt_a.Clear( );
txt_b.Clear( );
}

}
}

Output -

* Array and ArrayList class –

* 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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

There are 3 types of arrays in C# programming:


1. Single Dimensional Array
2. Multidimensional Array (rectangular arrays)
3. Jagged Array (array-of-arrays)

1. Single Dimensional Array –


Single-dimensional arrays are the simplest form of arrays. All items in a single dimension
array are stored in a row starting from 0 to the size of array - 1. Single Dimensional array is also
called one-dimensional array.

Syntax –
Data_Type[ ] Array_Name = new Data_Type[size];

Examples –
int[ ] arr = new int[5];
string[ ] names = new string[2];

Single dimensional array initialization –

int[ ] numbers = new int[5] { 1, 2, 3, 4, 5 };


string[ ] names = new string[2] { "Rocky", "Sam" };

2) Multidimensional arrays (rectangular arrays) –


Arrays can have more than one dimension called as multidimensional array. The most
common kind of multidimensional array is the two-dimensional array. A two dimensional array
can be thought of as a grid of rows and columns.

Syntax –
Data_Type[ , ] Array_Name = new Data_Type[size];

Examples –
int[ , ] numbers = new int[3, 2];

Multidimensional array initialization –


int[ , ] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, {5, 6} };

C#.Net akshaypawle@[Link] 9

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

* 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.

2) IndexOf(Array arrayName, Object value) –


It is used to search for the specified object and returns the index of its first occurrence in
a one-dimensional array.

3) Copy(Array sourceArray, Array destinationArray, int length) –


It is used to copy elements of an array into another array by specifying starting index.

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.

6) SetValue(Object value, int index) –


It is used to set new value to a specified index.

C#.Net akshaypawle@[Link] 10

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

Program – Write a program to demonstrate Array class.

namespace arrayClass
{
public partial class Form1: Form
{
int[ ] no = new int[5] { 20, 40, 50, 10, 30 };

public Form1( )
{
InitializeComponent( );
}

private void Form1_Load(object sender, EventArgs e)


{
for (int i = 0; i < [Link]; i++)
{
txt_no.Text = txt_no.Text + no[i] + " ";
}
}

C#.Net akshaypawle@[Link] 11

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

private void btn_properties_Click(object sender, EventArgs e)


{
txt_length.Text = ([Link]).ToString( );
txt_rank.Text = ([Link]).ToString( );
}

private void btn_methods_Click(object sender, EventArgs e)


{
//getvalue method
txt_getvalue.Text = ([Link](2)).ToString( );

//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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings


//sort method
[Link](no);
for (int i = 0; i < [Link]; i++)
{
txt_sort.Text = txt_sort.Text + no[i] + " ";
}
}
}
}

* 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.

ArrayList myList = new ArrayList( ) { 10, "AMOL" };

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.

2) Insert(int index, Object value) –


This method is used to insert a single element at the specified index.

C#.Net akshaypawle@[Link] 13

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

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.

Program – Write a program to demonstrate ArrayList class.

C#.Net akshaypawle@[Link] 14

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings


using [Link];

namespace arrayListClass
{
public partial class Form1 : Form
{
ArrayList myList = new ArrayList( ) { 10, "AMOL" };

public Form1( )
{
InitializeComponent( );
}

private void Form1_Load(object sender, EventArgs e)


{
for (int i = 0; i < [Link]; i++)
{
txt_mylist.Text = txt_mylist.Text + myList[i] + " ";
}
}

private void btn_methods_Click(object sender, EventArgs e)


{
//count property
txt_count.Text = ([Link]).ToString( );

//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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings


txt_insert.Text = txt_insert.Text + myList[i] + " ";
}

//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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

* 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:

int[ ][ ] jaggedArray = new int[2][ ];

Before you can use jaggedArray, its elements must be initialized. You can initialize the
elements like this:

jaggedArray[0] = new int[3];


jaggedArray[1] = new int[2];

You can assign individual array elements like following examples:

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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

Program – Write a program to demonstrate the concept of Jagged Array.

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][];

// Initialize the elements:


jaggedArray[0] = new int[3];
jaggedArray[1] = 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

Downloaded by Dipak Khillare (dipakkhillare040@[Link])


lOMoARcPSD|28694321

[Link].(CS) TY Unit III – Functions, Arrays and Strings

// Display the array elements:


for (int i = 0; i < [Link]; i++)
{
for (int j = 0; j < jaggedArray[i].Length; j++)
{
if (i == 0)
{
txt_first.Text = txt_first.Text + jaggedArray[i][j] + " ";
}
else if(i == 1)
{
txt_second.Text = txt_second.Text + jaggedArray[i][j] + " ";
}
}
}
}

private void btn_exit_Click(object sender, EventArgs e)


{
[Link]( );
}
}
}

C#.Net akshaypawle@[Link] 19

Downloaded by Dipak Khillare (dipakkhillare040@[Link])

You might also like