C # Explanation of key knowledge (1)

Source: Internet
Author: User

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]



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.