Learn C # (function one) a week

zhaozj2021-02-16  55

Learn C # (function one) a week

C # Talent Bird (QQ: 249178521)

1 Introduction

· C # does not support global functions

W All functions must be declared within the class

· Passive file and header file

W All functions must be declared when

Int notallowed () // error, C # has no global function

{

...

}

Sealed Class Methods

{

void inline ()

{...

}

void error ()

{...

}; // Error, function can not have end sections

Int alSoError (); // error, function must be implemented when it is declared

}

Like Java, C # does not allow global functions. All functions must be implemented within the class or structure. The function is a member of the class or structure, and the function is also called a method.

C # Allows the addition of end sections, for example:

Sealed Class Methods

{

...

}; / / Can have end sections

However, C # does not allow the end semicolon to be added in the declaration of the function, for example:

Sealed Class Methods

{

Void notallowed () {...}; // error, function can not have end semicolons

}

2. Declaration function

· Function parameter list

W Variation is separated by commas

w parameters must be named

W If the parameters are not omitted

Sealed Class Methods

{

Void error (float) // error, the parameter is not named

{...

}

Void Noerror (Float Delta)

{...

}

INT Error (Void) // error, does not allow VOID when there is no parameter

{...

}

Int noerror ()

{...

}

}

3. Value type parameters

· The general function parameters are a copy of the argument

W argument must be pre-assigned

W argument can be a constant type

Sealed Class ParameterPassing

{

Static Void Method (int parameter)

{

Parameter = 42;

}

Static void

Main

()

{

INT Arg = 0;

Console.write (arg); / / result is 0

Method (arg);

Console.write (arg); / / result is 0

}

}

(Note: For the convenience of the narrative, the words "parameters" appear in the future refer to the function parameters, that is, the so-called ginseng)

Function parameters that are not modified by REF or OUT are a value type parameter. The value parameters only exist when the function belongs in this parameter is called, and is initialized by the value transmitted when the invouse is used. When the function call ends, the value parameters do not exist.

Only the arguments that are pre-assigned can be passed to the value parameters, for example:

Int arg; // arg has not been assigned

Method (arg); / / error, the first value must be pre-assigned

The actor that passes to the function can be a pure number rather than a variable, for example:

Method (42);

Method (21 21);

转载请注明原文地址:https://www.9cbs.com/read-22170.html

New Post(0)