Вы находитесь на странице: 1из 3

Methods in C#.

Net
Types of Methods in C#.Net:
 Methods

• Abstract Method

• Virtual Method

• Override Method

• Static Method

• Main Method

Method:
In object-oriented programming, a method is a subroutine (a portion of code) that is
exclusively associated either with a class or with an object. A method usually consists of a

• sequence of programming statements to perform an action,

• a set of input parameters to customize those actions,

• and possibly an output value (called the return value) of some kind.

Methods provide a mechanism for accessing and manipulating the encapsulated data stored in an
object.

Abstract Method:
An Abstract Method is a method which is just declared but not implemented in the same class.
It is declared with keyword Abstract. This method is implemented with keyword “override” in the child
class. An abstract method can only exist in an abstract class. A class which implements the abstract
class must implement all its abstract methods.

Public Abstract Class C1

Public Abstract Void M1(); // an abstract method M1 in a class C1

Public Abstract Void M2();

Public Class C2: C1

Public override Void M1()


{

} // an implementation of abstract method M1 in child class C2

Public override Void M2()

Virtual Method:
A Virtual Method is a method which is just declared but not implemented in the same class. It
is declared with keyword Virtual. This method is implemented with keyword “override” in the child
class. A virtual method can exist in a non-abstract i.e. simple class. A class which implements the
virtual method needs not to implement all the virtual methods exist in that class.

Public Class C1

Public Virtual Void M1(); // a Virtual method M1 in a class C1

Public Virtual Void M2();

Public Class C2: C1

Public override Void M1()

} // an implementation of virtual method M1 in child class C2

Override Method:
An override Method is a method which is written in child class to override or implement the
body (functionality) of the method of parent class. It is declared with key word override. In C#
Override method is normally used to override the parent public/protected methods in the child class or
is used to implement virtual or abstract method of parent class in the child class.

Examples of override method are already given above.


Static Method:
A static Method is a method which is automatically loaded in the memory at runtime. It is
declared with keyword Static in method name. These methods can be directly called in the application.

Main Method:
Main is very important keyword in any language like C#. A Method having a name “Main” is
the first method in the application which is executed by the compiler from the memory, once the
application is loaded in the memory. To the load the main method in the memory for the execution, it
must be declared static.

In C# programming the Main method is where program starts execution. It is the main entry point
of program that executes all the objects and invokes method to execute. There can be only one
Main method in C#. However, the C# Main method can be void or int return type. It must be
inside a class or struct and must be declared with static modifier. It is t he main place where a
program starts the execution and end. The Main method can have a parameter and these
parameters are kno wn as zero-indexed command line argument.

Оценить