By 22/07/2015 11 Comments

Methods In C#

  • In object oriented programming generally methods in C#¬†are called behavior, sometime it is also called an action.
  • They are declared inside a class so these are class members, codes, methods or functions.
  • Actually a method or function is a block of code, which contains logic and performs a particular action or task (perform only one task).
  • A method operates, access and modifies data or fields (variables) which is defined in a class.
  • In object oriented programming a method must be declared outside main method but called in main method.

General Syntax:

access-modifier return-type MethodName (parameter-list)
{
Method body
}

STATEMENT DESCRIPTION
access-modifier There are four types of access modifiers public, private, protected and internal. They provide the access level or visibility of class members such as (data or variables and methods) and decides these can be accessed outside from a class or not? Here an access modifier is an optional, if you do not specify, C# use an access modifier private by default with all members (variables and methods) and any member with private access modifier cannot be accessed outside of its class so we use public modifier here.
return-type This indicates the return type of a method that means what type of data that method can returns. It can be return any value or primitive data types such as string, an int, bool etc. as well as reference type such as an object or a class, an array, string etc. If method does not return any specific type that method must has a keyword (void) which indicates that method returns nothing it is simply a void type method and it is called a procedure.
MethodName MethodName indicates the name of a method, this name is used to call or access a particular method. Method name should be any meaningful name.
(parameter-list) It is a sequence of an optional data type variables or reference types separated by comma (,), actually these are local variables of different data or primitive types. When we pass these local variables from any method these are called parameters or parameter list. This parameter list or sequence of local variables receive values as an arguments when method is called and pass these values in it as an arguments. If a method has no parameters, then that is called empty parameter list. If a method has parameters, then that is called parametrized method. The number of arguments or values must be matched with the number of method parameters and also its data type.
{ This curly bracket indicates the starting scope or definition of a method.
Method body This is method body, any particular task which do you want to perform by this method can be written or declared here.
} This curly bracket indicates the ending scope or definition of a method.


Example – 1, Following program describes the use of void type method in a Mark sheet program:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // Following is the declaration of instance variables / fields.
        public string Asp;
        public string Javascript;

        public double AspMarks;
        public double JavascriptMarks;

        /* Following is the declaration of void type method 
           which performs the addition of asp.net and javascript marks,
           this method has empty parameter-list. 
        */
        public void GetObtainMarks()
        {
            Console.WriteLine("Total obtain marks :{0}", (AspMarks + JavascriptMarks));
        }
    }

    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // A Mark sheet object is being created.
            MarkSheet markSheet = new MarkSheet();

            // Following statements initialize instance fields by a reference variable and also print them.
            Console.WriteLine("Subject: {0} ", markSheet.Asp = "Asp.net");
            Console.WriteLine("Asp.net Marks: {0}", markSheet.AspMarks = 85);
            Console.WriteLine("Subject: {0}", markSheet.Javascript = "Javascript");
            Console.WriteLine("Javascript Marks: {0}", markSheet.JavascriptMarks = 70);

            // The method GetObtainMarks() is called through an object reference variable.
            markSheet.GetObtainMarks();
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        Subject: Asp.net
        Asp.net Marks: 85
        Subject: Javascript
        Javascript Marks: 70
        Total obtain marks :155
        
       */
    }
}


Example – 2, Following program describes the use of return type method in a Mark sheet program:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // Following is the declaration of instance variables / fields.
        public string Asp;
        public string Javascript;
        public double AspMarks;
        public double JavascriptMarks;

        /* Following is the declaration of return type method 
           which has double type as its return type.
           Performs the addition of asp.net and javascript marks
           than return that expression to the main method.
           This method has empty parameter-list.
        */
        public double GetObtainMarks()
        {
            return AspMarks + JavascriptMarks;
        }
    }

    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // A Mark sheet object is being created.
            MarkSheet markSheet = new MarkSheet();

            // Following statements initialize instance fields by a reference variable and also print them.
            Console.WriteLine("Subject: {0} ", markSheet.Asp = "Asp.net");
            Console.WriteLine("Asp.net Marks: {0}", markSheet.AspMarks = 85);
            Console.WriteLine("Subject: {0}", markSheet.Javascript = "Javascript");
            Console.WriteLine("Javascript Marks: {0}", markSheet.JavascriptMarks = 70);

            // The method GetObtainMarks() is called and it is being assigned to a local variable (obtainMarks). 
            double obtainMarks = markSheet.GetObtainMarks();

            // A local variable (obtainMarks) is printed.
            Console.WriteLine("Total obtain marks :{0}", obtainMarks);
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        Subject: Asp.net
        Asp.net Marks: 85
        Subject: Javascript
        Javascript Marks: 70
        Total obtain marks :155
        
       */
    }
}


REMEMBER:

  • Return type methods must use keyword return to return value or expressions.
  • Return method must be assigned to a variable which variable must has the same data type as return type method has.
  • Return type methods are more useful than void type methods because we can do more activities with return type methods.
  • Return keyword can be used in both types of methods (void, return type), return keyword which is used in void type method does not return value, it only causes the termination of method, at the other hand return keyword which is used in return type methods must returns values or expressions to caller of the method (main method). Remember when return statement executes, the compiler bypass all the code bellow return statement.
  • Return type method can returns any type of data such as arrays, objects, enumerations, property, structures etc.






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .