Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Kinds of Parameters in C#

    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 266
    Comment on it

    Parameters  are variables that are passed into a function which defines how something should be done. The values which are passed into the function are known as arguments.

     

    There are five methods of passing the parameter:-

    • Value type
    • Reference type
    • Output type
    • Optional parameter
    • Named parameter

    1. Value type- In value type parameter there is no need to give any keyword while passing the parameters.

    e.g of value type parameter:

    using System;
     
    namespace Value_Type
    {
      class Program
       {
         public static int square(int num)
          {
            return num * num;
          }
         static void Main(string[] args)
          {
            int val,number;
            number = 5;
            //Passing the copy value of number variable
            val = Program.square(number);
            Console.Write(val);
            Console.ReadLine();
          }
       }
    }

     

    2. Reference type- In reference type variable we need to provide the ref keyword with a parameter. In ref type the variable needs to be initialized before use.

    e.g of ref type parameter:

    class Refeg 
     { 
       static void Method(ref int i)
        { 
            // Rest the mouse pointer over i to verify that it is an int.
            // The following statement would cause a compiler error if i 
           // were boxed as an object. 
              i = i + 44;
        }
    
        static void Main() 
          { 
             int val = 1; Method(ref val); 
             Console.WriteLine(val);
    
    
                // Output: 45 
           } 
    }

     

    3. Output type- Output type parameter are declared by providing out keyword. In out type parameter both method calling and definition must use the out keyword.

    e.g of out type parameter is:

    class OutEg 
    {
     static void Method(out int i)
     { 
         i = 44;  
     }
      static void Main()
       { 
         int value; 
         Method(out value);
         // value is now 44
       } 
    }

     

    4. Optional parameter- It is new parameter introduced in c#. It allows us to neglect the predefined values.

    e.g of optional parameter:

     

    public int Sum(int a, int b, int c = 0, int d = 0); 
    /* c and d is optional */
     Sum(10, 20); //10 + 20 + 0 + 0 
     Sum(10, 20, 30); //10 + 20 + 30 + 0 
     Sum(10, 20, 30, 40); //10 + 20 + 30 + 40

     

    5.Named parameter- In this arguments are passed by name rather than value. This is also a new parameter introduced in c#.

    e.g of named parameter:

     

    public void CreateAccount(string name, string address = "unknown", int age = 0); CreateAccount("Sara", age: 30);
    CreateAccount(address: "India", name: "Sara");

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: