C # key Knowledge (i) (turn)

Source: Internet
Author: User
Detailed in Microsoft's. NET launched, about C # related articles have appeared, as Microsoft's important and Java to contend with the language, C # has many advantages. This article will select some of the important knowledge in the C # language in detail,

The first chapter: parameter

1. 1 in Parameter

Four kinds of parameter forms of C # species:
General parameters
In Parameter
Out parameters
Parameter series
This chapter will introduce the following three kinds of use.

In the C language, you can pass the address (that is, the argument) or the Delphi language through the Var indicator to pass address parameters to the data ordering operations, in the C # language, how to do it? The "in" keyword can help you. This keyword can pass the value you want to return by using the parameter.
Namespace TESTREFP
{
Using System;
public class MyClass
{

public static void Reftest (ref int iVal1)
{
IVal1 + 2;

}
public static void Main ()
{
int i=3; Variable needs to be initialized

Reftest (ref i);
Console.WriteLine (i);

}
}
}

It is important to note that variables need to be initialized first.

Results:

5



1. 2 Out parameters


Do you want to return more than one value at a time? in the C + + language This task is basically impossible to accomplish. The "Out" keyword in C # can help you do it easily. This keyword can return more than one value at a time by a 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);
}
}

Results:

0 10 20

1. 3 Parameter Series

The parameter series can make a number of related parameters represented by a single sequence, 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 ("\targs[{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 following are the output results:

# Parameters: 0
# Parameters: 1
Args[0] = 1
# Parameters: 2
Args[0] = 1
ARGS[1] = 2
# Parameters: 3
Args[0] = 1
ARGS[1] = 2
ARGS[2] = 3
# Parameters: 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.