top

Search

C# Tutorial

Methods are a series of statements in a code block that perform a specific task. These statements can be executed by calling the method with the required arguments. The Main() Method is a prerequisite in all C# programs.Method Definition in C#Methods are defined within a class or a structure. The method definition specifies the method name and the parameters types required. When calling the method, the arguments for each parameter is specified.The syntax for method definition is given as follows:AccessSpecifier ReturnType NameOfMethod (ListOfParameters){        // Method Statements }In the above syntax, the AccessSpecifier provides the visibility of the method in the class. The ReturnType is the value returned by the method. The NameOfMethod is the name provided to the method as a unique identifier. ListOfParameters is the list of the parameters in the method.Method Calling in C#A method can be called using its name. For the method calling, the name of the method is followed by parenthesis, even if there are no arguments.The syntax for method calling is given as follows:NameOfMethod (ListOfParameters);In the above syntax, the NameOfMethod is the name provided to the method as a unique identifier. ListOfParameters is the list of the parameters in the method.A program that demonstrates method definition and method calling is given as follows:Source Code: Program to demonstrate method definition and method calling in C#using System; namespace MethodDemo {   class Example {      static void EvenOrOdd(int n)      {          if(n%2 == 0)          Console.WriteLine("{0} is even",n);          else          Console.WriteLine("{0} is odd",n);                }      static void Main(string[] args)      {          EvenOrOdd(5);          EvenOrOdd(8);      }   } }The output of the above program is as follows:5 is odd 8 is evenRecursion in C#Recursive methods are those that call themselves whether directly or indirectly. They are quite useful in solving complex problems as these problems can be expressed as progressively smaller problems until the base case is obtained.A program to find the factorial of a number using recursion is given as follows:Source Code: Program to find factorial of a number using recursion in C#using System; namespace MethodDemo {   class Example {      static int Factorial(int n)      {          if((n==0)||(n==1))          return 1;          else          return n*Factorial(n-1);      }      static void Main(string[] args)      {          Console.WriteLine("Factorial of 5 is {0}",Factorial(5));          Console.WriteLine("Factorial of 8 is {0}",Factorial(8));      }   } }The output of the above program is as follows:Factorial of 5 is 120 Factorial of 8 is 40320Passing By ValueWhen the parameters are passed by value to a method, new memory variables are created for these parameters. The changes done in those memory variables don't reflect back to the original values.A program that demonstrates passing by value is given as follows:Source Code: Program that demonstrates passing by value in C#using System; namespace PassByValueDemo {   class Example {      static void Swap(int m, int n)      {         int temp;         temp = m;         m = n;         n = temp;      }      static void Main(string[] args)      {         int a = 5;         int b = 10;         Console.WriteLine("Passing By Value");         Console.WriteLine("Before Swap method is called");         Console.WriteLine("a = {0}", a);         Console.WriteLine("b = {0}", b);         Swap(a, b);         Console.WriteLine("After Swap method is called");         Console.WriteLine("a = {0}", a);         Console.WriteLine("b = {0}", b);      }   } }The output of the above program is as follows:Passing By Value Before Swap method is called a = 5 b = 10 After Swap method is called a = 5 b = 10In the above output, you can see the values won’t get swapped.Passing By ReferenceWhen the parameters are passed by reference to a method, new memory variables are not created for these parameters. Rather, the reference parameters point to the original parameters so that the changes done in the reference variables reflect back to the original values.The reference parameters can be declared using the ref keyword in C#. A program that demonstrates this is as follows:Source Code: Program that demonstrates passing by reference in C#using System; namespace PassByRefDemo {   class Example {      static void Swap(ref int m, ref int n)      {         int temp;         temp = m;         m = n;         n = temp;      }      static void Main(string[] args)      {         int a = 5;         int b = 10;         Console.WriteLine("Passing By Reference");         Console.WriteLine("Before Swap method is called");         Console.WriteLine("a = {0}", a);         Console.WriteLine("b = {0}", b);         Swap(ref a, ref b);         Console.WriteLine("After Swap method is called");         Console.WriteLine("a = {0}", a);         Console.WriteLine("b = {0}", b);      }   } }The output of the above program is as follows:Passing By Value Before Swap method is called a = 5 b = 10 After Swap method is called a = 10 b = 5In the above output, you can see the values get swapped successfully.Output ParametersThe output parameters are useful in transferring data out of the method. They can be used to return multiple values while the return statement can only return one value at a time. The out keyword is used to declare an output parameter.A program that demonstrates this is as follows:Source Code: Program that demonstrates output parameters in C#using System; namespace OutputDemo {   class Example {      static void OutValue(out int val )      {         val = 7;      }      static void Main(string[] args)      {         int num = 4;         Console.WriteLine("Value of num before calling OutValue: {0}", num);         OutValue(out num);         Console.WriteLine("Value of num after calling OutValue: {0}", num);      }   } }The output of the above program is as follows:Value of num before calling OutValue: 4 Value of num after calling OutValue: 7
logo

C# Tutorial

Methods in C#

Methods are a series of statements in a code block that perform a specific task. These statements can be executed by calling the method with the required arguments. The Main() Method is a prerequisite in all C# programs.

Method Definition in C#

Methods are defined within a class or a structure. The method definition specifies the method name and the parameters types required. When calling the method, the arguments for each parameter is specified.

The syntax for method definition is given as follows:

AccessSpecifier ReturnType NameOfMethod (ListOfParameters)

{
       // Method Statements
}

In the above syntax, the AccessSpecifier provides the visibility of the method in the class. The ReturnType is the value returned by the method. The NameOfMethod is the name provided to the method as a unique identifier. ListOfParameters is the list of the parameters in the method.

Method Calling in C#

A method can be called using its name. For the method calling, the name of the method is followed by parenthesis, even if there are no arguments.

The syntax for method calling is given as follows:

NameOfMethod (ListOfParameters);

In the above syntax, the NameOfMethod is the name provided to the method as a unique identifier. ListOfParameters is the list of the parameters in the method.

A program that demonstrates method definition and method calling is given as follows:

Source Code: Program to demonstrate method definition and method calling in C#

using System;
namespace MethodDemo
{
  class Example {
     static void EvenOrOdd(int n)
     {
         if(n%2 == 0)
         Console.WriteLine("{0} is even",n);
         else
         Console.WriteLine("{0} is odd",n);          
     }
     static void Main(string[] args)
     {
         EvenOrOdd(5);
         EvenOrOdd(8);
     }
  }
}

The output of the above program is as follows:

5 is odd
8 is even

Recursion in C#

Recursive methods are those that call themselves whether directly or indirectly. They are quite useful in solving complex problems as these problems can be expressed as progressively smaller problems until the base case is obtained.

A program to find the factorial of a number using recursion is given as follows:

Source Code: Program to find factorial of a number using recursion in C#

using System;
namespace MethodDemo
{
  class Example {
     static int Factorial(int n)
     {
         if((n==0)||(n==1))
         return 1;
         else
         return n*Factorial(n-1);
     }
     static void Main(string[] args)
     {
         Console.WriteLine("Factorial of 5 is {0}",Factorial(5));
         Console.WriteLine("Factorial of 8 is {0}",Factorial(8));
     }
  }
}

The output of the above program is as follows:

Factorial of 5 is 120
Factorial of 8 is 40320

Passing By Value

When the parameters are passed by value to a method, new memory variables are created for these parameters. The changes done in those memory variables don't reflect back to the original values.

A program that demonstrates passing by value is given as follows:

Source Code: Program that demonstrates passing by value in C#

using System;
namespace PassByValueDemo
{
  class Example {
     static void Swap(int m, int n)
     {
        int temp;
        temp = m;
        m = n;
        n = temp;
     }
     static void Main(string[] args)
     {
        int a = 5;
        int b = 10;
        Console.WriteLine("Passing By Value");
        Console.WriteLine("Before Swap method is called");
        Console.WriteLine("a = {0}", a);
        Console.WriteLine("b = {0}", b);
        Swap(a, b);
        Console.WriteLine("After Swap method is called");
        Console.WriteLine("a = {0}", a);
        Console.WriteLine("b = {0}", b);
     }
  }
}

The output of the above program is as follows:

Passing By Value
Before Swap method is called
a = 5
b = 10
After Swap method is called
a = 5
b = 10

In the above output, you can see the values won’t get swapped.

Passing By Reference

When the parameters are passed by reference to a method, new memory variables are not created for these parameters. Rather, the reference parameters point to the original parameters so that the changes done in the reference variables reflect back to the original values.

The reference parameters can be declared using the ref keyword in C#. A program that demonstrates this is as follows:

Source Code: Program that demonstrates passing by reference in C#

using System;
namespace PassByRefDemo
{
  class Example {
     static void Swap(ref int m, ref int n)
     {
        int temp;
        temp = m;
        m = n;
        n = temp;
     }
     static void Main(string[] args)
     {
        int a = 5;
        int b = 10;
        Console.WriteLine("Passing By Reference");
        Console.WriteLine("Before Swap method is called");
        Console.WriteLine("a = {0}", a);
        Console.WriteLine("b = {0}", b);
        Swap(ref a, ref b);
        Console.WriteLine("After Swap method is called");
        Console.WriteLine("a = {0}", a);
        Console.WriteLine("b = {0}", b);
     }
  }
}

The output of the above program is as follows:

Passing By Value
Before Swap method is called
a = 5
b = 10
After Swap method is called
a = 10
b = 5

In the above output, you can see the values get swapped successfully.

Output Parameters

The output parameters are useful in transferring data out of the method. They can be used to return multiple values while the return statement can only return one value at a time. The out keyword is used to declare an output parameter.

A program that demonstrates this is as follows:

Source Code: Program that demonstrates output parameters in C#

using System;
namespace OutputDemo
{
  class Example {
     static void OutValue(out int val )
     {
        val = 7;
     }
     static void Main(string[] args)
     {
        int num = 4;
        Console.WriteLine("Value of num before calling OutValue: {0}", num);
        OutValue(out num);
        Console.WriteLine("Value of num after calling OutValue: {0}", num);
     }
  }
}

The output of the above program is as follows:

Value of num before calling OutValue: 4
Value of num after calling OutValue: 7

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial