By 22/07/2015 14 Comments

Passing Parameters To Methods In C#

  • Passing parameters to methods in C# are optional but in real world programs we need to pass one or more parameters in methods.
  • When we pass parameter(s) in the methods that methods are called parametrized methods.
  • Actually parameters are local variables which receives values when methods are called and these values are called arguments.
  • Parameters make a method more dynamic.

General Syntax:

MethodName (parameter-list)
{
Method body
}

STATEMENT DESCRIPTION
MethodName Name of a method, this can be any valid meaningful name, a method is called through this name reference.
(parameter-list) List of optional parameters, this list can be one or more local variables separated by comma (,)

Example – 1, parametrized return type method in 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 parametrized method 
           which is passing 2 double type local variables as the parameters.
           Performs the addition of asp.net and javascript marks
           than return that expression to the main method.
        */
        public double GetObtainMarks(double aspMarks , double javascriptMarks)
        {
            // Local variables (parameters) are being assigned to instance fields.
            AspMarks = aspMarks;
            JavascriptMarks = javascriptMarks;
            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, passing 2 values as arguments and it is being assigned to a local variable (obtainMarks). 
            double obtainMarks = markSheet.GetObtainMarks(85, 70);

            // 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
        
       */
    }
}


Example – 2, a method is passing an array as its parameters:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        /* Following is the declaration of void type method DisplaySubjects() 
           which takes a string type array as its parameter. 
         */
        public void DisplaySubjects(string[] subjectNames)
        {
            // Loop through each array element and print.
            foreach (string extractSubjectNames in subjectNames)
                Console.WriteLine(extractSubjectNames);
        }
    }

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

            // String type array initialization.
            string[] subjectNames = { "C#", "Asp.net", "Html5", "Css3", "Jquery", "TypeScript"};

            /* Following statement calls a void type method DisplaySubjects() 
               and passes an array to it as its argument 
               which will be received to its parameter (string[] subjectNames). 
             */
            markSheet.DisplaySubjects(subjectNames);
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        C#
        Asp.net
        Html5
        Css3
        Jquery
        TypeScript
       */
    }
}


Example – 3, a method returns an array OR a method has an array as its return type:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        /* Following is an one-dimensional array 
           initialization as an array instance / field.
        */
        string[] SubjectNames = { 
                                 "C#", 
                                 "Asp.net", 
                                 "Html5", 
                                 "Css3", 
                                 "Jquery",
                                 "TypeScript"
                             };

        /* Following is a return type method declaration 
           which has an string type array as its return type. 
        */
        public string[] DisplaySubjects()
        {
            // Following return statement returns an array to the main method.
            return SubjectNames;
        }
    }
    
    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 string type array receives 
               an return type method DisplaySubjects() 
              which has an array as its return type.
            */
            string[] subjectNames = markSheet.DisplaySubjects();

            // Following foreach, loop through each array’s elements and print.
            foreach (string extractSubjectNames in subjectNames)
                Console.WriteLine(extractSubjectNames);
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        C#
        Asp.net
        Html5
        Css3
        Jquery
        TypeScript
       */
    }
}





Next Tutorial →


← 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 .