C# Getting Started 3

table of Contents

function

definition

Parameter array

Reference parameter

Output parameters

Function overloading


This section introduces functions

 

function

definition

static <returnType> <functionName>(<paramType> <paramName>,...)
{
    ......
    return returnValue;
}

esp: The function that executes a line of code can use a function introduced in C#6, the expression body method is used to realize it, use =>

static <returnType> <functionName>() => x*y

//等价
static <returnType> <functionName>(x,y)
{
    return x*y;
}

Parameter array

The parameter array is a special parameter. At the end of the function definition parameter, an indefinite number of parameters are allowed to call the function. Use the params keyword to define

static <returnType> <functionName>(<p1Type> <p1Name>,...,params <type>[] <name>)
{
    ...
    return <returnValue>;
}

//调用
<functionName>(<p1>,...,<val1>,<val2>,...)

//eg
static int sumVals(params int[] data)
{
    int sum = 0;
    foreach (int val in data)
        sum += val;
    return sum;
}
static void main(string[] args)
{
    int sum = sumVals(1,2,3,4,5);
    Console.Write(sum);
    
    return 0;
}

It should be noted that <val1>, <val2>, etc. are all values ​​of type type, which are used to initialize the name array. The number of parameters that can be specified is not limited, but they must be of type <type>

Reference parameter

Usually the passed parameters are passed by value and will not affect the original variable. But if you want to modify the passed parameters through the function, one way is to pass by reference, use the ref keyword to specify the parameters

static <returnType> <functionName>(ref <type> <val>)

Note:

  1. Pass by reference can only pass variables
  2. The referenced variable must be initialized, C# does not allow to assume that the ref parameter is initialized in the function that uses it
  3. In addition to specifying the variable name, pass the reference parameter with ref

Output parameters

To change the parameter value, in addition to passing by reference, you can also use the out keyword, which specifies that the parameter will return the value to the variable in the function call after the function is executed.

static <returnType> <functionName>(out <type> <val>)

//eg:调用
sum(out int[] data)

Note:

  1. Assigning an unassigned variable to the ref parameter is illegal, but it can be assigned to the out parameter
  2. When using the out parameter, it must be regarded as an unassigned variable, which is equivalent to reinitialization in the function
  3. Passing parameters need to use the out keyword

Function overloading

Function overloading allows the creation of multiple functions with the same name, and each function can use different parameter types. In this way, it is not necessary to explicitly specify which function to use, and the corresponding function can be executed according to the parameter type used. It is important to note that you cannot define two functions with the same name that only return different values.

static void f(out int v)
{
    v = 0;
    Console.Write($"v1{v}\n");
}

static void f(ref double v)
{
    v = 1;
    Console.Write($"v2{v}\n");
}

static void Main(string[] args)
{
    //调用哪个函数看参数类型
    //int sum = 5;
    double sum = 10.0;
    f(ref sum);
    Console.Write(sum);
}

 

Guess you like

Origin blog.csdn.net/wyzworld/article/details/112639369