C Language Tutorial Methods and Classes 2 .

Mondo Education Updated on 2024-01-31

3.3 Output Parameters

Similar to referential parameters, output parameters do not open up new memory areas. The difference from a referential parameter is that:

You don't need to initialize a variable before calling a method.

Output-type parameters are used to pass the data returned by the method.

When you call a method that contains a reference parameter, you need to use the out keyword to declare the parameter as the output parameter when passing the parameter. After the method returns, the passed variable is considered initialized.

Let's compare the parameters and output parameters as examples.

static void squareref( ref int x )

x = x * x;

static void squareout( out int x )

x = 8;

x = x * x;

static void main()

int i = 3, j ;

squareref(ref i);

squareout(out j);

console.writeline("i = , j = ", i, j);

The output of the function is:

i = 9, j = 64

Analyse. From the example above, we can see the difference between the reference parameter and the output parameter. The squareref method uses a reference parameter method, while the squareout method uses the output parameter method, and both methods do exactly the same thing, both of which are squared. Just initialize the value of x to 8 in the squareout method.

3.4 Array parameters

Arrays can also be used as arguments to methods, which are used to represent arguments called array arguments, array arguments can only be placed at the end of the argument list, and array arguments can only be one-dimensional arrays.

Declaring an array argument requires the use of the params keyword

using system;

class test;

console.writeline("thesum:"+sum(var));console.writeline("the sum:"+sum(10,20,30,40,50));

The program is compiled and executed output:

the sum:15 the sum:150

3.5. Variables: Duration and range of action

The duration of a variable refers to the amount of time that the variable exists in memory.

The scope of a variable refers to the places in the program where the variable can be referenced. Some variables can be referenced throughout the program, while others can only be referenced in a limited part of the program.

The following example illustrates the scope of an instance variable and a local variable:

using system;

class test

public int s = 1;

public void methoda()

int s = 23;

console.writeline("s in methoda is :"+ s+"after entering methoda" );

s;console.writeline("s in methoda is :"+ s+"before le**ing methoda" );

public void methodb()

static void main()

The output of the program is as follows:

s in methoda is :23after entering methoda

s in methoda is :24before le**ing methoda

s in methodb is :1after entering methodb

s in methodb is :10after entering methodb

3.6 Recursion

Recursion is an important concept, in real life, we may encounter such a problem: a complex problem can be broken down into simple problems, and the solution method of a simple problem is the same as that of a complex problem, and we know the solution method of a simple problem, which is why we can use recursion to solve a complex problem.

We know that the factorial in mathematics is denoted as n!, which is calculated as:

n! = n * n-1)*(n-2)*…1

Now let's consider factorial evaluation with the idea of recursion, and a factorial can be written as:

n! = n *(n-1)!

3.7 Overloading of methods

In the C language, you can define several methods with the same name in a class, as long as they have different parameters, which is called method overloading.

In the example below, method overloading is used to calculate the square of int and double data.

public int square(int x)

return x*x;

public double square(double x)

To emphasize, the method of overloading must meet at least one of the following conditions:

The number of parameters is different.

The parameter types are different.

The order of the parameters is different.

4. c namespace

Namespaces provide a way to organize related classes and other types. Unlike files or components, namespaces are a logical combination, not a physical combination. Multiple classes that are not in the same file can be co-contained in a namespace, creating a logical structure.

To create a namespace, you need to use the namesapce keyword

4.1 Use of Namespace

Although the use of namespaces makes the class better organized, it also introduces the problem that the full name of the class is very long, which is very inconvenient to use. c allows shorthand to the full name of the class, c states that before using the class, the namespace in which the class is located needs to be listed at the top of the file, and the namespace name needs to be preceded by the using keyword, so that the relative name can be used to refer to the class instead of its absolute name. In the following two examples, the first uses the full name of the class, and the second uses the relative name of the class:

system.int32 a;Use your full name.

using system;

int32 a;

4.2 Aliases for namespaces

Another use of the using keyword is to assign aliases to classes and namespaces, if the namespace name is very long, has been used several times in **, but the name of the namespace is included in the using directive, you can assign an alias to the namespace, the syntax is as follows:

using alias = namespace name;

5. The use of class methods

5.1 Console class methods

The console class represents the standard input, output, and error streams for a console application.

5.2 math method

I'm a tech creator

Related Pages