





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Learn about methods in C# programming, including their declaration, invocation, parameters, overloading, and best practices. Discover the benefits of using methods and how they help improve code organization, readability, and maintainability.
Typology: Slides
1 / 45
This page cannot be seen from the preview
Don't miss anything!






































Methods
Table of Contents
Simple Methods
static void PrintHelloWorld ()
Console.WriteLine("Hello World");
Method named
PrintHelloWorld
PrintHeader();
PrintHeader();
Method body
always
surrounded
by { }
Why Use Methods?
Declaring and Invoking Methods
static void PrintText(string text)
Console.WriteLine(text);
Declaring Methods
Method Name
Type Parameters
Method
Body
Invoking a Method (2)
static void Main()
{
PrintHeader();
}
static void PrintHeader()
{
PrintHeaderTop();
PrintHeaderBottom();
}
static void Crash()
{ Crash(); }
Methods with Parameters
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
Problem: Sign of Integer Number
(^2) The number 2 is positive.
The number 0 is zero.
The number - 5 is negative.
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());
}
Problem: Printing Triangle
Solution: Printing Triangle
static void PrintLine(int start, int end)
for (int i = start; i <= end; i++)
Console.Write(i + " ");
Console.WriteLine();