[C# Video] method overload, function overload, value transfer

Posted by ucbones on Mon, 17 Jan 2022 09:42:09 +0100

catalogue

heavy load

function overloading

ref value

heavy load

Overloading is simply a case where functions or methods have the same name but different parameter lists. Such functions or methods with the same name and different parameters are called overloaded functions or methods.

Definition of overload

If the function name is the same, the parameter list of the function is different (including the number of parameters and parameter types), and the return type can be the same or different

Overloading is a method that enables functions and operators to process different types of data or accept different numbers of parameters

Method overloading

It refers to the definition of multiple methods with the same name in a class, but each method is required to have different parameter types or the number of parameters

Specific specifications

  • The method name must be the same
  • The parameter table of a method must be different, including the type or number of parameters, so as to partition different method bodies
    • If the number of parameters is different, its parameter type is ignored
    • If the number of parameters is the same, the types of parameters must be different
  • The return types and modifiers of methods can be the same or different

give an example

        static void Main(string[]args)
        {
            AddNumber(1, 2);
            //You can see that a total of 3 methods are displayed,
            //Depending on the type of variable in each method, enter
          
        }
        //The return value type is int, the number of parameters is 2, and the parameter types are all int
        public static int AddNumber(int num1,int num2)
        {
            return num1 + num2;
        }

        //The return value type is double, the number of parameters is 2, and the parameter types are double
        public static double AddNumber(double num1, double num2)
        {
            return num1 + num2;
        }

        //The return value type is double, the number of parameters is 2, and the parameter types are double and int
        public static double AddNumber(double num1, int num2)
        {
            return num1 + num2;
        }

Returns an array

		static void Main(string[] args)
        {
            //Method sum max min

            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };//Define nums array and assign values
            int[] arrayNumbers = ArraySumAndMaxOrMin(nums);//Call the method of ArraySumAndMaxOrMin

            Console.WriteLine("The maximum value is{0}",arrayNumbers[0]);
            Console.WriteLine("The minimum value is{0}",arrayNumbers[1]);
            Console.WriteLine("Hewei{0}",arrayNumbers[2]);

            Console.ReadKey();
            //Returns multiple values of different types
        }
        public static int[]ArraySumAndMaxOrMin(int[] numbers)
        {
            int[] array = new int[3];//Declaration array
            array[0] = int.MinValue;
            array[1] = int.MaxValue;
            array[2] = 0;

            for (int i = 0; i < numbers.Length ; i++)
            {
                if (numbers[i] > array[0])//If the array value is less than the minimum value
                {
                    array[0] = numbers[i];//Assign an array value to the min variable
                }
                if (numbers[i]<array[1])//If the array value is greater than the maximum value
                {
                    array[1] = numbers[i];//Assign array values to the max variable
                }
                array[2] += numbers[i];//Sum

            }
            return array;
        }

function overloading

There are multiple constructors with different parameters

Specific specifications

Constructor overloading: it is a special method used to create an object. The method name is the same as the type. There is no return value, not even void

  • Constructors can have parameters. You can pass parameters when you create a new object
  • If you do not specify a constructor, the class has a default parameterless constructor
  • If a constructor is specified, there is no longer a default parameterless constructor
  • If you need a parameterless constructor, you need to write it yourself

give an example

		public Person(string name,char gender ,int age)
        {//The appearance of the constructor with parameters will kill the original default constructor without parameters.
            this._name = name;
            this._age = age;
            this._gender = gender;
        }
        //If you want to use a parameterless constructor at this point, you must
        //Overload of constructor
        public Person()
        {
        }

out return value

		static void Main(string[] args)
        {
            int number1 = 10;//Define variables

            //The value adopted by the parameter number1 of AddNumber is passed from the method
            int sum = AddNumber(out number1);

            //If the method parameter is decorated with out, the parameter must be assigned in the method
            //Adding out before the variable is to transfer the value in the method to the outside of the method
            Console.WriteLine(number1);//So now the value of number1 is 100
            Console.WriteLine(sum);//The total returned is 120
            Console.ReadKey();
        }
        public static int AddNumber(out int num1)
        {
            num1 = 100;//Define variables
            return num1 + 20;//Return to 100 + 20
        }

If there are parameters without out, the parameters with out must be assigned an initial value in the method. The parameters with out can no longer be assigned an initial value in the method

		static void Main(string[]args)
        {
            int num1;//Declare variable
            int num2 = 80;//If you assign an initial value to the variable, num2 does not add out, you can no longer assign an initial value to the method
            int numSum = Show(out num1, num2);//Call method

            Console.WriteLine(num1);
            Console.WriteLine(num2);
            Console.ReadKey();
            //Out modifies the parameter of the method. Outside the method, you can not assign a value to this parameter, but inside the method, you must give this out
            //The modifier parameter is assigned an initial value. In addition, the parameter plus out transfers the value of the parameter in this method to the outside of the method
        }
        public static int Show(out int number1,int number2)
        {
            number1 = 90;
            return number1 + 10;
        }

ref value

       static void Main(string[] args)
        {
            int number1 = 10;//Assign initial value to variable
            int number2 = 20;//Assign initial value to variable

            int sum = Show(ref number1, ref number2);//Call method

            Console.WriteLine("{0},{1},{2}", number1, number2, sum);  
            Console.ReadKey();
            //When ref is used, the variable must be given an initial value. It is different from out. Out can pass out the value in the method
            //ref can pass the value into the method and outside the method
        }
        public static int Show(ref int num1, ref int num2)
        {
            num1 = 100;
            num2 = 200;
            return num1 + num2;
        }