C# Simplified, part 2: Methods & Programming Constructs
(Page 1 of 6 )
In this article, you will learn about methods, which perform actions and carry out funtions. You will also learn about programming constructs and looping statements in C#. This is the second article in a series covering C#; the first article in the series is available
here.
About the C# Simplified Series C# Simplified covers each and every concept of the C# programming language in a concise manner. The articles in this series have been divided into several parts and will provide detailed explanations along with source code and screenshots. This series has been specifically written for beginners and students with an aim to teach C# in the quickest possible time. Please send your comments to csharpsimplified@gmail.com.
Methods
Methods are building blocks in C#. They perform some kind of action or carry out many functions such as printing, saving and opening a dialog box. The action could be anything, based on your application. For instance, the code for printing a report can be given inside a method. After declaring the method you can call it, preferably with a button event. You can also reuse this method with the help of inheritance. Inheritance is one of the features of any Object Oriented programming language such as C#.
There are basically two types of methods in C#. They are known as instance and class methods. Even though both methods perform the same work, there are differences in their declaration and usage. Methods can also be declared with parameters, and accessed by passing the required values.
Instance Method
The instance method is a method which is declared outside the Main() method. It can be accessed only by creating an object of the class, as in the case of an Instance Variable (see Listing 2.1).
Listing 2.1
using System;
class Instancemethod
{
//Method declared outside the main and hence Instance Method.
void show()
{
int x = 500;
int y = 700;
Console.WriteLine(x);
Console.WriteLine(y);
}
public static void Main()
{
//Object created
Instancemethod IM = new Instancemethod();
//Instance method called
IM.show();
}
}
In the above listing, a method named show() is declared, which prints the values of two variables. Inside the Main() method, an object of the class named IM is created and the method is called.
Class Method
A class method is also placed outside the Main() method with the exception that it should be declared with the keyword static. An interesting point to note is that class methods can be accessed without creating an object of the corresponding class. The general format for accessing a class method is C lassname.Methodname. Listing 2.2 illustrates the working of a class method:
Listing 2.2
using System;
class Classmethod
{
//A class method declared, notice the static keyword
static void show()
{
int x = 700;
int y = 900;
Console.WriteLine(x);
Console.WriteLine(y);
}
public static void Main()
{
/* Class method called without creating an object
of the class */
Classmethod.show();
}
}
In the above listing, a method named show() is declared, which prints the values of two variables. Inside the Main() method, the method is called without creating an object of the class. This is because of the static keyword, which we applied while declaring the method.
Next: Declaring methods with parameters >>
More C# Articles
More By Anand Narayanaswamy