What Is The Method In C#

Now we are going to learn about Method in C# language.
  • 2642

Introduction

In every object oriented programming we have work with objects because they are the basic building blocks of a program. Data and method create a objects. Method changes the state of the objects created. They are the dynamic part of the objects but data is the static part of objects. A method is a code block containing a series of statements. Methods must be declared with in a class and a structure, it is a good programming practice.

Proper use of method bring the following advantages:

  • It decrease the code duplication.
  • It also decrease the problem into small modules.
  • Improving quality of the coding.
  • Re-usability of code.
  • Encapsulate the information.

Method are declared inside the body of a class, normally after the declaration of data field. The general form of a method declaration is
Modifiers type methodname(formal-parameter-list)
{
Method-body
}

Method declaration has five parts:

  • Method name.
  • Type of value the method returns( type).
  • List of parameters (formal-parameter-list).
  •  Body of the method.
  •  Access label.

  • Example
    public class ABC
    {
    public void showinfo()
    {
    Console.WriteLine("this is base class");
    }
    }
    public class DEF
    {
    static void Main()
    {
    ABC ab=new ABC ();
    ab.showinfo();}}}

Output

method.jpg

© 2020 DotNetHeaven. All rights reserved.