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.
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.
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
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
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.
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.
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
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *