0% found this document useful (0 votes)
2 views5 pages

CSharp Methods

This document provides a simple guide to methods in C#, explaining their purpose, syntax, and components such as access modifiers, return types, and parameters. It covers examples of void methods, methods with parameters, and methods with return values, as well as concepts like static methods and method overloading. Additionally, it highlights common mistakes and offers practice ideas for creating methods.
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)
2 views5 pages

CSharp Methods

This document provides a simple guide to methods in C#, explaining their purpose, syntax, and components such as access modifiers, return types, and parameters. It covers examples of void methods, methods with parameters, and methods with return values, as well as concepts like static methods and method overloading. Additionally, it highlights common mistakes and offers practice ideas for creating methods.
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

C# Methods - Simple English Guide

What is a Method?

A Method in C# is a block of code that performs a specific task. Instead of writing the same code many times, you
create a method once and call it whenever you need it.

Why Do We Use Methods?

• To avoid repeating code.

• To make programs cleaner and easier to read.

• To organize code into small parts.

• To make debugging easier.

General Syntax

access_modifier return_type MethodName(parameters)


{
// code
}

Parts of a Method

1. Access Modifier

Usually public or private.

2. Return Type

Determines what the method returns.

Examples: void, int, double, string, bool.

3. Method Name

The name used to call the method.


4. Parameters

Inputs sent to the method.

Void Method Example

static void Hello()


{
[Link]("Hello");
}

Calling a Method

To use a method, we call it:

Hello();

Methods with Parameters

static void PrintName(string name)


{
[Link](name);
}

PrintName("Ahmed");

Parameter vs Argument

Parameter: variable inside the method definition.

Argument: actual value sent to the method.

Methods with Return

static int Sum(int x, int y)


{
return x + y;
}

What Does return Mean?

The return statement sends a value back to the place where the method was called.

Using Returned Values

int result = Sum(5, 3);


[Link](result);

Why return is Powerful

Returned values can be:


• stored in variables

• printed

• compared in conditions

• used in calculations

What is static?

A static method belongs to the class itself, not to an object.

Example:

static void Hello()


{
}

Why Main is static

The program starts from Main() automatically, so it must be static.

Flow of Execution
The program starts from Main().
When Main calls another method, execution moves to that method.
After finishing, execution returns back to Main.

Variable Scope

Variables created inside a method can only be used inside that method.

Method Overloading

You can create multiple methods with the same name if they have different parameters.

Example:

static int Sum(int x, int y)


{
return x + y;
}

static int Sum(int x, int y, int z)


{
return x + y + z;
}

Common Mistakes

• Forgetting static.

• Forgetting return.

• Wrong return type.

• Calling a method incorrectly.

How to Think About Methods

Ask yourself:

1. What inputs does the method need?

2. What should it return?


3. What task should it perform?

Practice Ideas

Try creating methods for:

• Finding the maximum number

• Finding average

• Converting temperature

• Checking even/odd

• Rectangle area

• Circle area

• Prime number check

You might also like