Lecture 7 - Function and Array
Lecture 7 - Function and Array
https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
Course Content
Review of C# Syntax: Overview of Writing Applications using C#, Data types, Operators, and
Expressions
C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods
Developing the Code for a Graphical Application: Implementing Structs and Enums
Implementing Type-safe Collections: Creating Classes, Organizing Data into Collections,
4
Declaring C# Methods
• The general form of method declaration is
<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
Method Body
}
Following are the various elements of a method −
Access Specifier − This determines the visibility of a variable or a method from
8
Monitoring Applications using
creating and Invoking Methods
Programming Example
static void Main(string[] args)
{
Program p = new Program();
9
Declaring C# Methods
Example: Code shows a function FindMax that takes two integer values
and returns the larger of the two. It has public access specifier, so it can
be accessed from outside the class using an instance of the class.
18
Creating Overloaded Methods
Here is an example of method overloading.
public class Methodoveloading
{
public int add(int a, int b) //two int type Parameters method
{
return a + b;
}
public int add(int a, int b,int c) //three int type Parameters with same method same a
{
return a +b+c+d;
19
}
}
Arrays in C#
• An array stores a fixed-size sequential collection of elements
of the same type.
• An array is used to store a collection of data, but it is often
more useful to think of an array as a collection of variables
of the same type stored at contiguous memory locations.
• The lowest address corresponds to the first element and the
20
Declaring Arrays in C#
• To declare an array in C#, you can use the following syntax −
datatype[] arrayName;
using System;
public class Exercise
{