Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Methods and Structures in C#

Rating
-
Sold
-
Pages
6
Uploaded on
20-04-2025
Written in
2024/2025

In C#, methods are blocks of code that perform specific tasks and can be invoked to execute their functionality. Methods can be defined to return values or simply perform actions without returning anything, depending on their return type. The structure of a method includes key components such as access modifiers (which determine the visibility of the method), return types (indicating whether a value is returned or not), and parameters (used to pass data to the method). Methods can be overloaded, meaning multiple methods can have the same name but differ in their parameters, allowing for greater flexibility when calling them.

Show more Read less
Institution
Course

Content preview

Methods and Structures in C#

Methods

A method is a block of code designed to perform a specific task. In C#, methods are
used to define functionality that can be invoked from other parts of the program.
Here's the general syntax for defining a method:

csharp

CopyEdit

access_modifier return_type MethodName(parameter_list) {

// method body

}

Parts of a Method:

• Access Modifier: Specifies the visibility of the method (e.g., public or private).
By default, methods are private if no access modifier is provided.

• Return Type: Specifies the type of value the method returns. If the method
doesn’t return a value, the return type is void.

• Method Name: The name of the method, which is case-sensitive. Method
names are followed by parentheses.

• Parameter List: Parameters are used to pass data to and from a method. They
are optional.

Here’s an example of two methods in C#:

Example 1: Void Method

csharp

CopyEdit

public void PrintRectangleArea(int width, int height) {

int area = width * height;

Console.WriteLine("The area of the rectangle is " + area);

}

Example 2: Method with Return Value

, csharp

CopyEdit

public int GetRectangleArea(int width, int height) {

int area = width * height;

return area;

}

To invoke a method, you call the method's name with the required arguments:

csharp

CopyEdit

DemoMethod obj = new DemoMethod(); // Creating an object instance

obj.PrintRectangleArea(5, 3); // Invoking the void method

int result = obj.GetRectangleArea(5, 3); // Invoking the method with return value

Console.WriteLine("Result = " + result);

Method Overloading

C# supports method overloading, allowing you to create methods with the same name
but different parameter types or numbers. The compiler selects the appropriate
method based on the arguments passed.

Example of Overloaded Methods:

csharp

CopyEdit

// Method for square area

public int GetArea(int side) {

return side * side;

}



// Method for rectangle area with integer parameters

public int GetArea(int width, int height) {

Written for

Institution
Course

Document information

Uploaded on
April 20, 2025
Number of pages
6
Written in
2024/2025
Type
Class notes
Professor(s)
Miguel besa
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
jhuneesparcia

Get to know the seller

Seller avatar
jhuneesparcia STI College
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
3
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions