Docsity

Methods and Overloading in C#: Declaration, Invocation, Parameters, and Best Practices, Slides of Electronic Technology

Partial preview of the text

Download Methods and Overloading in C#: Declaration, Invocation, Parameters, and Best Practices and more Slides Electronic Technology in PDF only on Docsity!

Defining and Using Methods, Overloads

Methods

Table of Contents

1. What Is a Method?

2. Declaring and Invoking Methods

 Void and Return type Methods

3. Methods with Parameters

4. Overloading Methods

5. Program Execution Flow

6. Naming and Best Practices

Simple Methods

 Named block of code , that can be invoked later

 Sample method definition :

 Invoking (calling) the

method several times:

static void PrintHelloWorld ()

Console.WriteLine("Hello World");

Method named

PrintHelloWorld

PrintHeader();

PrintHeader();

Method body

always

surrounded

by { }

 More manageable programming

 Splits large problems into small pieces

 Better organization of the program

 Improves code readability

 Improves code understandability

 Avoiding repeating code

 Improves code maintainability

 Code reusability

 Using existing methods several times

Why Use Methods?

Declaring and Invoking Methods

 Methods are declared inside a class

 Variables inside a method are local

static void PrintText(string text)

Console.WriteLine(text);

Declaring Methods

Method Name

Type Parameters

Method

Body

 A method can be invoked from:

 The main method – Main()

 Its own body – recursion

Invoking a Method (2)

static void Main()

{

PrintHeader();

}

static void PrintHeader()

{

PrintHeaderTop();

PrintHeaderBottom();

}

static void Crash()

{ Crash(); }

 Some other method

Methods with Parameters

 You can pass zero or several parameters

 You can pass parameters of different types

 Each parameter has name and type

static void PrintStudent(string name, int age, double grade)

{

Console.WriteLine("Student: {0}; Age: {1}, Grade: {2}",

name, age, grade);

}

Method Parameters (2)

Parameter

type

Parameter

name

Multiple parameters

of different types

 Create a method that prints the sign of an integer number n:

Problem: Sign of Integer Number

(^2) The number 2 is positive.

The number 0 is zero.

The number - 5 is negative.

 Write a method that receives a grade between 2.00 and 6.

and prints the corresponding grade in words

 2.00 - 2.99 - "Fail"

 3.00 - 3.49 - "Poor"

 3.50 - 4.49 - "Good"

 4.50 - 5.49 - "Very good"

 5.50 - 6.00 - "Excellent"

Problem: Grades

3.33 Poor

Very good

Fail

Solution: Grades

private static void PrintInWords(int grade)

{

string gradeInWords = string.Empty;

if (grade >= 2 && grade <= 2.99)

gradeInWords = "Fail";

//TODO: make the rest

Console.WriteLine(gradeInWords);

}

static void Main()

{

int n = int.Parse(Console.ReadLine();

PrintInWords());

}

 Create a method for printing triangles as shown below:

Problem: Printing Triangle

 Create a method that prints a single line , consisting of numbers

from a given start to a given end :

Solution: Printing Triangle

static void PrintLine(int start, int end)

for (int i = start; i <= end; i++)

Console.Write(i + " ");

Console.WriteLine();