Powered by Blogger.

Saturday, March 8, 2014

Methods in C#.Net

In this article we will discuss about methods, what are the use of methond and how to write a method in C#.Net. You can check my previous articles on Tutorials on WCF in Asp.Net, Implement SQL Server authentication in asp.net and Session management in Asp.Net.

Also you can check out:

- How to add Eval for hyperlink in gridview in asp.net?

- WCF tutorial and example in C#.Net

- Get all tables and stored procedures in sql server 2008

- Methods are very much helpful to organize code, which contains one or more lines of code.

- Methods specifies a logical task and helps to manage code properly.

- When you declare a method in C#.Net, the first part of the declaration specifies the data type of the return value, and the second part indicates the method name. The method also can contain parameters.

- If your method doesn’t return any information, you should use the void keyword.


void MyVoidMethod()
// Code goes here.

This method did not return any value.

int MyMethodReturnsAnInteger()
return 99;

// The above method returns an interger value and in this example it returns 99.

int MyMethodReturnsAnInteger(int value)
return 100 + value;

//The above methid retuns an integer as well as take one parameter. Here it takes a integer parameter.

Apart from this C#.Net method can specify accessibility like private, public etc.

private void MyVoidMethod()
// Code goes here.

To call methods you can directly use the method name.

MyVoidMethod(); //For void methods

int result = MyMethodReturnsAnInteger (5); //For methods that contains one parameter and returns an integer.