C Basic Syntax: Method Parameters Detailed

  • 2021-06-28 13:48:30
  • OfStack

Value parameter: a value parameter is equivalent to a local variable, when a value parameter is used, a new storage location is allocated, the actual parameter is copied to that location, and the copied value is passed to the method.Therefore, a value parameter can only take values into the method, but it cannot take out the method without affecting the value of the argument.

Reference parameters: When a reference parameter is used, a new storage location, In other words, will not be allocated. Reference parameters can bring values into and out of the method, thus affecting the value of the argument.As an example:


using System;

namespace prg1
{
  class Paramstest
  {  
    // Value parameter usage demonstration 
    public static void Transposition_1(int a, int b)
    {
      int temp = a;
      a = b;
      b = temp;
    }
    // Demonstration of using reference parameters 
    static void Transposition_2(ref int a,ref int b)
    {
      int temp = a;
      a = b;
      b = temp;
    }
    static void Main(string[] args)
    {
      int a = 25;
      int b = 30;
      // call Transposition_1
      Console.WriteLine(" call Transposition_1 before a={0},b={1}",a,b);
      Transposition_1(a,b);
      Console.WriteLine(" call Transposition_1 after a={0},b={1}", a, b);
      Console.WriteLine("====================\n");
      // call Transposition_2
      Console.WriteLine(" call Transposition_2 before a={0},b={1}", a, b);
      Transposition_2(ref a,ref b);
      Console.WriteLine(" call Transposition_2 after a={0},b={1}", a, b);
      Console.WriteLine("====================\n");
      Console.ReadKey();
    }
  }
}


Output Parameters: To guess from the surface meaning that it can only output parameters that cannot be entered into the method, we started following example validation 1 with the following code:


static void Transposition_2(ref int a,ref int b)
    {
      int temp = a;
      a = b;
      b = temp;
    }

The compiler will alert a that b has an unassigned error, so it is also intuitive to see that output parameters cannot take values into methods, only values into methods.From the example below, you can see that assignment of output parameters is done inside the method, so you must assign them ahead of time no matter where you use them, which is also the commonality of using any type of parameter.


//Use of output parameters
    static void Transposition_3(string name,out string FistName,out string LastName)
    {
      int i=name.Length;//Get the length of the string
      while(i>0)
      {
       char chparm=name[i-1];
       if (chparm == '.')
       {
         break;
       }
       i--;
      }
      FistName = name.Substring(0,i-1);
      LastName = name.Substring(i);

    }
// call Transposition_3
      string DoName,Nmark; 
      Transposition_3("rohelm.X",out DoName,out Nmark);
      Console.WriteLine("Domain Name of Myself: {0}",DoName);
      Console.WriteLine("The last name of my Domain Name: {0}",Nmark);


Parameter array: In short, the unit passed by the method is an array, and it can be a 1-dimensional or staggered array (like int[][]]), but not a multidimensional array (like;string[,]), one or more arguments can be formulated for the parameter array, where each argument is an expression, and the parameter array and the value parameter of the same type are exactly equivalent.For example:


class Prmarry
  {
    public static void Show(params string[] name)
    {
      Console.WriteLine("Array contains the number of elements: {0}", name.Length);
      Console.Write("elements of NameArray:");
      for (int i = 0; i < name.Length; i++)
      {
        Console.Write("{0,10}",name[i]);
      }
    }
  }
// call Show
      string[] NameArray = { "rohelm.X", "Boolean", "rrats" };
      Prmarry.Show(NameArray); 
      Console.ReadKey();

I don't know what to do either. My input method and compiler seem to be playing hide-and-seek. After a while, they won't support Chinese character input. I can really enter them in English, but I have no choice.

Here is the reference source for this log, which can be analyzed as a whole:


using System;

namespace prg1
{
  class Paramstest
  {  
    // Value parameter usage demonstration 
    public static void Transposition_1(int a, int b)
    {
      int temp = a;
      a = b;
      b = temp;
    }
    // Demonstration of using reference parameters 
    static void Transposition_2(ref int a,ref int b)
    {
      int temp = a;
      a = b;
      b = temp;
    }
    //Use of output parameters
    static void Transposition_3(string name,out string FistName,out string LastName)
    {
      int i=name.Length;//Get the length of the string
      while(i>0)
      {
       char chparm=name[i-1];
       if (chparm == '.')
       {
         break;
       }
       i--;
      }

      FistName = name.Substring(0, i - 1);
      LastName = name.Substring(i);
    }
    static void Main(string[] args)
    {
      int a = 25;
      int b = 30;
      // call Transposition_1
      Console.WriteLine(" call Transposition_1 before a={0},b={1}",a,b);
      Transposition_1(a,b);
      Console.WriteLine(" call Transposition_1 after a={0},b={1}", a, b);
      Console.WriteLine("====================\n");
      // call Transposition_2
      Console.WriteLine(" call Transposition_2 before a={0},b={1}", a, b);
      Transposition_2(ref a,ref b);
      Console.WriteLine(" call Transposition_2 after a={0},b={1}", a, b);
      Console.WriteLine("====================\n");
      // call Transposition_3
      string DoName,Nmark; 
      Transposition_3("rohelm.X",out DoName,out Nmark);
      Console.WriteLine("Domain Name of Myself: {0}",DoName);
      Console.WriteLine("The last name of my Domain Name: {0}"+"\n",Nmark);
      // call Show
      string[] NameArray = { "rohelm.X", "Boolean", "rrats" };
      Prmarry.Show(NameArray); 
      Console.ReadKey();
    }
  }
  class Prmarry
  {
    public static void Show(params string[] name)
    {
      Console.WriteLine("Array contains the number of elements: {0}", name.Length);
      Console.Write("elements of NameArray:");
      for (int i = 0; i < name.Length; i++)
      {
        Console.Write("{0,10}",name[i]);
      }
    }
  }
}



Related articles: