You are on page 1of 6

1.

Methods
A method is a code block that contains a series of statements. A program causes the
statements to be executed by calling the method and specifying any required
method arguments. In C#, every executed instruction is performed in the context of a
method.

The Main method is the entry point for every C# application and it's called by the
common language runtime (CLR) when the program is started. 

Defining Methods in C#
When you define a method, you basically declare the elements of its structure. The
syntax for defining a method in C# is as follows –

<Access Specifier> <Return Type> <Method Name>(Parameter List) {


Method Body
}

Following are the various elements of a method −


 Access Specifier − This determines the visibility of a variable or a
method from another class.

 Return type − A method may return a value. The return type is the data
type of the value the method returns. If the method is not returning any
values, then the return type is void.

 Method name − Method name is a unique identifier and it is case


sensitive. It cannot be same as any other identifier declared in the
class.

 Parameter list − Enclosed between parentheses, the parameters are


used to pass and receive data from a method. The parameter list refers
to the type, order, and number of the parameters of a method.
Parameters are optional; that is, a method may contain no parameters.

 Method body − This contains the set of instructions needed to


complete the required activity.
Example
public int Add(int num1,int num2)
{
int result=num1+num2;
return result;
}

public - Access Specifier 


int - Return type 
Add - Method name 
(int num1,int num2) - Parameter list 
VOID METHOD

using System;

namespace VOIDMETHODS
{
class Program
{
static void Main(string[] args)
{

writesomething();
writesomethingspecific("hello there method2 ");
Console.Read();

//void method without parameter

public static void writesomething()


{

Console.WriteLine("hello there method1 ");


}

//void method with parameter

public static void writesomethingspecific(string s1)


{

Console.WriteLine(s1);
}

}
}
METHOD WITH RETURN TYPE

using System;

namespace methodswithreturntype
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine(ADD(3,4));
Console.WriteLine(DIV(25, 13));
Console.Read();
}

public static int ADD(int num1,int num2)


{

return num1 + num2;


}

public static double DIV(double num1,double num2)


{

return num1 / num2;


}

}
}
2. TRY CATCH AND FINALLY
using System;

namespace trycatchfinally
{
class Program
{
static void Main(string[] args)
{

string a = Console.ReadLine();
string b= Console.ReadLine();
try
{
int a1 = Int32.Parse(a);
int b1 = Int32.Parse(b);
Console.WriteLine(add(a1, b1));
}
catch (Exception)
{
Console.WriteLine("please eneter a number only");

}
finally
{
Console.WriteLine("this will execute anyways");
}
}

public static int add(int num1, int num2)


{

return num1 + num2;


}

}
}

And the idea behind this finally block is to,

for example,
if you tried to start an Internet connection or if you try to write into a file, there
eventually can happen is error.

So let's say the Internet connection breaks or you don't have access to a file that you
want to write into or some other exceptions that can come up and you want to close
the connection or the file either way. So you have open to file and now you try to
write into that file and after you have written into that file within the try block and
maybe it worked, maybe it didn't work, but what you definitely want to do is to close
the file again, or you try to download something from the internet and if it worked or
it didn't work, doesn't matter. You need to close the connection to the Internet
afterwards, and that's something that you would do .

You might also like