[C# Tutorial 5] Methods and Constructors

0

After knowing about Classes and Objects lets learn about their insiders i.e Methods and constructors.

Usually while executions method is the one that get executed. While compilation the compiler understands the name of the methods and when that method is called at run time the code corresponding to that method is executed.

Syntax:

[method-modifier][attribute]return-type member-name([formal-parameter-list])
[method-body]

Method-modifier: It is an optional keyword used for specifying the visibility of the method. C Sharp supports five types of method-modifier as explained below

  1. Private: It is used for making the member private; a private member is accessible only to the class in which it is declared.
  2. Public: Members marked with public are accessible to any class.
  3. Protected: The members that marked with protected are accessible to the same class as well as to the classes that are derived from the original class.
  4. Internal: This keyword makes visibility of the member function or variable to the assembly in which the class is declared.
  5. Protected Internal: It is the combination of both the access modifiers that is internal and protected.  

 

Attributes: Attribute used are “virtual”,”static” or “abstract”.

Return-Type: This keyword is compulsory to  be declared with a method, it describes the data type of the variable that will be returned by the method when it is executed, If no value is to be returned then void keyword is used.

Member-name: It is an user-defined name unique name given to the method (sometimes it may be same, this case we will see in later tutorials) for calling. Usually, methods are given action names.

Parameter list: The variable that are to be given to the method when it will execute are declared as parameter with their data type, in the round brackets”()”. If no parameters are there then an empty round brackets are used.

Method-Body: It contains the code to be executed when a method is called. The code is written in the curly brackets that is “{}”, without semicolon. If no code is there, then an empty curly brackets are used with semicolon.

On the basis of calling a method, they can be classified in two types:

Static Methods: Static methods are declared using static keyword. These methods are not executed by calling via object or instance of a class. Rather they are called directly by their name from the same class. Syntax:                   static_method_name();

If static method is called from another class, then it uses the class name in which the method is defined with dot operator,

Syntax:                   Class_name.Static_Method_name();

Instance Methods: If static keyword is not used while declaring method than the method is called as Instance method. They are executed by calling via object.

Syntax:                   object.method_name();

Constructor: These are special methods declared, and are executed whenever a new Object is created on the class. Some of the important features of constructor are

  • They have same name as that of class name
  • They do not return any value after execution; hence they do not have any return type not even “void” is used.
  • They are automatically invoked when the object is created, and hence do not require any explicit call like normal method.
  • Constructors usually don’t have any logic built in it they are mainly used for initializing the variables of the class.

Syntax:  method-modifier class_name([parameter list]) {constructor body}

Example :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

class1.method1();       //Calling of Static Method using class name

class1 obj = new class1();

obj.method2();          //Calling of instance method using object

Console.Read();

}

}

class class1

{

int a;                       //Normal variable not accessible in static method

static int b;                // Static variable accessd in all the mehtods

public class1()               //Instance constructor

{

a = 100;

}

static class1()              //Static Constructor

{

b = 10;

}

public static void method1()  //static method

{

Console.WriteLine(“This is static method1 b = “+b);

}

public void method2()       //Instance Method

{

Console.WriteLine(“This is instance method2 a = “+a+ ” b =  ” + b);

}

}

}

 

Output :

In the above program the static method (method1) is called using class name (class1) and as this line is executed the static constructor (class1) is executed first and then the method. After returning back to main method, the object (obj) is created and then instance method (method2) is called through object (obj).

Thus, by now you might have understood about the methods and constructors, and in next tutorial we will see about the types of statements, any more suggestions or doubts regarding any C sharp tutorials, plz feel free to comment.

 

Share.

About Author

Leave A Reply