- C# Tutorial
- C# Tutorial
- C# Basic Syntax
- C# Operators
- C# if else
- C# switch
- C# Loops
- C# break Vs. continue
- C# Arrays
- C# Strings
- C# Methods
- C# Examples
- C# Add Two Numbers
- C# Swap Two Numbers
- C# Reverse a Number
- C# Reverse a String
- C# Celsius to Fahrenheit
- Computer Programming
- Learn Python
- Python Keywords
- Python Built-in Functions
- Python Examples
- Learn C++
- C++ Examples
- Learn C
- C Examples
- Learn Java
- Java Examples
- Learn Objective-C
- Web Development
- Learn HTML
- Learn CSS
- Learn JavaScript
- JavaScript Examples
- Learn SQL
- Learn PHP
C# Methods with Examples
A method in C# is a block of code used to perform a particular task. Methods are used when we need to create a block of code once and execute it multiple times. You will understand why I am saying this later on this page. But for now, let's show you the general form for creating a method in C#.
accessSpecifier returnType methodName(parameterList) { // block of code // or // body of the method }
For example:
static void greeting() { Console.WriteLine("I hope you enjoyed your weekend."); }
Since I used static as an access specifier, it is not required to create an instance of the program for this method to work. Then I used void as the return type, which means that the method greeting() will not return any value.
Now, to call a C# method, just write the method along with its parameters if the method that is going to be called accepts parameters. Since the previous method I defined, which was greeting(), does not accept any parameters, to call this method I have to write the name of the method followed by parantheses or () in this way.
greeting();
Let me create a proper C# example to demonstrate the definition and calling of a function.
using System; namespace MyFirstCSharpProgram { class MyClassXYZ { static void greeting() { Console.WriteLine("I hope you enjoyed your weekend."); } static void Main(string[] args) { greeting(); } } }
The snapshot given below shows the exact output produced by the above C# example. This snapshot was taken from the output console of Microsoft Visual Studio after executing the above C# program.
I used the red arrow to draw your attention to the output. Since the execution of the C# program starts with the Main() method, you need to call your defined method from inside this method. Also, you can call the same method multiple times.
C# Methods with Parameters
In this section, I will tell you how you can pass parameters to a method.
A parameter is used to pass it as information. The parameter will act as a variable inside the method definition. Let me give you an example, and then I will explain how the thing works.
using System; namespace MyFirstCSharpProgram { class MyClassXYZ { static void add(int a, int b) { int sum = a + b; Console.WriteLine("\nSum = {0}", sum); } static void Main(string[] args) { Console.WriteLine("Enter the First Number: "); int numOne = Convert.ToInt32(Console.ReadLine()); int numTwo = Convert.ToInt32(Console.ReadLine()); add(numOne, numTwo); } } }
The sample run with user inputs of 12 and 43 as the first and second numbers is shown in the snapshot given below.
The dry run of the above C# program goes this way.
- Since the execution of the program starts from the Main() method, I have written a Console.WriteLine() statement, which prints the text "Enter the First Number: " on the output console.
- Then, using the second statement, using Console.ReadLine(), an input was received by the user at run-time of the program, which was further converted into an int type value and initialized to the numOne variable.
- Similar things go with the next statement. That is another input from the user that was received and initialized to the numTwo variable.
- And using the fourth statement of the Main() method, I called the method add(). While calling this method, I passed the first and second numbers as its two parameters, which were then copied to the variables a and b inside the definition of the add() method.
- Now, inside the add() method, I've made a variable called sum and filled it with the result of adding a and b together.
- And then I have printed the summation value on the output.
C# Methods with Return Value
We can also create a method in C# that can return values. Here is an example demonstrating a function with a return value in C#.
using System; namespace MyFirstCSharpProgram { class MyClassXYZ { static int add(int a, int b) { int res = a + b; return res; } static void Main(string[] args) { Console.WriteLine("Enter the First Number: "); int numOne = Convert.ToInt32(Console.ReadLine()); int numTwo = Convert.ToInt32(Console.ReadLine()); int sum = add(numOne, numTwo); Console.WriteLine("\nSum = {0}", sum); } } }
You will get the same output as the previous example's output.
« Previous Tutorial fresherearth.com »