Calling a Method / Function without any Order or Arguments

Normally, we follow an order of argument to pass in a method / function. But, there is absolutely no need of maintaining the order, as it is already declared in the respective method(s). We can call the method by just using the declared variable name, with our desired order of passing the arguments using the following c# code snippet.

C# Code Snippet

namespace Method

{

    class Program

    {

        static void Main(string[] args)

        {

             Program program = new Program();

             var data = "Object Value";

            namespace Method

           {

              class Program

             {

                 static void Main(string[] args)

                 {

                    Program program = new Program();

                    var data = "Object Value";

                    program.MethodWithDifferentArguments(obj:data, num:1, str:"String Value");

                    Console.ReadLine();

                 }

                void MethodWithDifferentArguments(int num, string str, object obj)

                {

                     Console.WriteLine("Number : " + num.ToString());

                     Console.WriteLine("String : " + str);

                     Console.WriteLine("Object : " + obj.ToString ());

                 }

            }

       }

       Console.ReadLine();

    }

    void MethodWithDifferentArguments(int num, string str, object obj)

    {

       Console.WriteLine("Number : " + num.ToString());

       Console.WriteLine("String : " + str);

       Console.WriteLine("Object : " + obj.ToString ());

    }

  }

} 

Here, the “MethodWithDifferentArguments(obj:data, num:1, str: "String Value");” the operator “ : “ has been used to assign the value to the respective variable to pass the value. And the value is caught as per the argument variable declared in the method.

Output

 
Ebook Download
View all
Learn
View all