C# Elementary Tutorial-Functions

Definition and use of
function Define function
static void Write(){ Console.WriteLine("Text output from function."); } Use of function static void Main(){ Write(); }





The definition of the console application function is as follows:
static (){ return; } Parameter array Define a function to obtain the sum of numbers, but the number of numbers is uncertain. Solutions: 1. Define a function and pass the parameters to an array; 2. Define a function with an uncertain number of parameters. At this time, we will use the parameter array.







Except for the parameter array, the parameters of all functions are fixed, so when calling, the parameters must be passed

//A parameter array of type int is defined here. The difference between the parameter array and the array parameter (above) lies in the function call. When calling the function of the parameter array, we can pass any number of parameters, and then the compiler will Help us automatically group together into an array. If the parameter is the above array parameter, then we can manually create the array by ourselves.
//The parameter array is to help us reduce the process of creating an array

Array parameter: int【】array Use an array as a parameter
static int array(int [] array)
array (new int[] {1,2,3,4,5}) You need to construct the array
parameter array yourself : you can input Parameters are automatically converted into arrays
static int array(params int【】array)
array(1,2,3,4,5) Pass the number directly without constructing an array.

Structure function
Define the function in the structure to achieve the name
struct CustomerName{ public string firstName; public string lastName; public string GetName(){ return firstName+" "+lastName; } } The overload of the function overload suppose we have a function for To achieve the maximum value of an array static int MaxValue(int[] intArray){ return; } This function can only be used to process int arrays. If you want to process the double type, you need to define another function static double MaxValue(double[] doubleArray){ return; } The function name is the same but the parameters are different. This is called function overloading (the compiler uses different parameters to identify which function should be called) // The compiler will go according to the type of the actual parameter you pass Determine which function to call


















Guess you like

Origin blog.csdn.net/euphorias/article/details/104478576