Methods in C# (with Examples) – Programming, Pseudocode Example, C# Programming Example
C# Console General Methods

Methods in C# (with Examples)

A method is a collection of statements that perform some specific task and return result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code.

Methods are useful because they let us use the same block of statements at many points in the program. However, they become more useful if we allow them to have parameters.
A parameter is a means of passing a value into a method call. The method is given the data to work on. As an example, consider the code below:

Example 1:

Code:

Output:

 

Examples 2:  Below is a simple example of a method that calculates rectangle’s area:

Code:

Output:

 

Example 3: 

Output:

 

Examples 4:

C# Code:

Output:

 

Examples 5: Method to Calculate the Sum of Prices of Books

Code:

 

Exapmles  6: Method to Show whether a Number is Positive.

To clarify the way method execution depends upon its input let’s take look at another example. The method gets as input a number of type int, and according to it returns to the console “Positive“, “Negative” or “Zero“:

Code:

Output:

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.