After the launch of Microsoft. NET, related articles on C # emerged one after another. As an important language for Microsoft to compete with JAVA, C # has many advantages. This article will select some important knowledge in C # For detailed introduction,
Chapter 1: Parameters
1. 1 IN Parameter
C:
Common Parameters
In Parameters
Out Parameter
Parameter Series
This chapter describes the last three methods.
In C language, you can pass the address parameter (real parameter) or the VAR indicator in DELPHI to sort data. In C # language, how is it done? The "in" keyword can help you. This keyword can be used to pass the value you want to return through parameters.
Namespace TestRefP
{
Using System;
Public class myClass
{
Public static void RefTest (ref int iVal1)
{
IVal1 + = 2;
}
Public static void Main ()
{
Int I = 3; // The variable needs to be initialized.
RefTest (ref I );
Console. WriteLine (I );
}
}
}
Note that the variable must be initialized first.
Result:
5
1. 2 OUT parameters
Do you want to return multiple values at a time? In C ++, this task is basically impossible. The "out" keyword in c # helps you easily complete the process. This keyword can return multiple values at a time through the parameter.
Public class mathClass
{
Public static int TestOut (out int iVal1, out int iVal2)
{
IVal1 = 10;
IVal2 = 20;
Return 0;
}
Public static void Main ()
{
Int I, j; // The variable does not need to be initialized.
Console. WriteLine (TestOut (out I, out j ));
Console. WriteLine (I );
Console. WriteLine (j );
}
}
Result:
0 10 20
1. 3 parameter Series
The parameter series allows multiple related parameters to be represented by a single series. In other words, the parameter series is the length of the variable.
Using System;
Class Test
{
Static void F (params int [] args ){
Console. WriteLine ("# parameter: {0}", args. Length );
For (int I = 0; I <args. Length; I ++)
Console. WriteLine ("args [{0}] = {1}", I, args [I]);
}
Static void Main (){
F ();
F (1 );
F (1, 2 );
F (1, 2, 3 );
F (new int [] {1, 2, 3, 4 });
}
}
The output result is as follows:
# Parameter: 0
# Parameter: 1
Args [0] = 1
# Parameter: 2
Args [0] = 1
Args [1] = 2
# Parameter: 3
Args [0] = 1
Args [1] = 2
Args [2] = 3
# Parameter: 4
Args [0] = 1
Args [1] = 2
Args [2] = 3
Args [3]