C # basic syntax: detailed explanation of method parameters

Source: Internet
Author: User

C # basic syntax: detailed explanation of method parameters
This article mainly introduces C # basic syntax: detailed explanation of method parameters. This article describes value parameters, reference parameters, output parameters, parameter arrays, and other parameter types, and provides code examples, for more information, see

 

 

● Value parameter: A value parameter is equivalent to a local variable. When a value parameter is used, a new storage location is allocated to copy the real parameter to this location, and pass the copy value to the method. Therefore, a value parameter can only bring a value into a method, but cannot bring out a method without affecting the value of a real parameter.

● Reference parameter: when a reference parameter is used, a new storage location will not be assigned. In other words, the reference parameter can bring the value into the method or the method, therefore, the value of the real parameter is affected. For example:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

Using System;

 

Namespace prg1

{

Class Paramstest

{

// Value parameter usage demonstration

Public static void Transposition_1 (int a, int B)

{

Int temp =;

A = B;

B = temp;

}

// Demo of referencing Parameters

Static void Transposition_2 (ref int a, ref int B)

{

Int temp =;

A = B;

B = temp;

}

Static void Main (string [] args)

{

Int a = 25;

Int B = 30;

// Call Transposition_1

Console. WriteLine ("Call Transposition_1 before a = {0}, B = {1}", a, B );

Transposition_1 (a, B );

Console. WriteLine ("Call Transposition_1 after a = {0}, B = {1}", a, B );

Console. WriteLine ("===============================\ n ");

// Call Transposition_2

Console. WriteLine ("Call Transposition_2 before a = {0}, B = {1}", a, B );

Transposition_2 (ref a, ref B );

Console. WriteLine ("after calling Transposition_2 a = {0}, B = {1}", a, B );

Console. WriteLine ("===============================\ n ");

Console. ReadKey ();

}

}

}


● Output parameters: Based on the meaning of the surface layer, we can only output parameters that cannot be input into the method. We will immediately follow the preceding example to verify and add the following code:

?

1

2

3

4

5

6

Static void Transposition_2 (ref int a, ref int B)

{

Int temp =;

A = B;

B = temp;

}

The compiler will remind a and B that an error is returned if no value is assigned. We can also intuitively see that the output parameter cannot bring the value into the method, but can only output the value to the method. From the example below, we can see that the value assignment operation is performed inside the method. Therefore, no matter where the output parameter is used, the value assignment must be performed in advance. This is also the commonality of using any type of parameter.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// Use of output parameters

Static void Transposition_3 (string name, out string FistName, out string LastName)

{

Int I = name. Length; // Get the length of the string

While (I> 0)

{

Char chparm = name [I-1];

If (chparm = '.')

{

Break;

}

I --;

}

FistName = name. Substring (0, I-1 );

LastName = name. Substring (I );

 

}

// Call Transposition_3

String DoName, Nmark;

Transposition_3 ("rohelm. X", out DoName, out Nmark );

Console. WriteLine ("Domain Name of Myself: {0}", DoName );

Console. WriteLine ("The last name of my Domain Name: {0}", Nmark );

 


● Parameter array: in short, the Unit transmitted by the method is an array, which can be a one-dimensional array or an staggered array (such as int [] []). but it cannot be a multi-dimensional array (such as; string [,]). You can create one or more real parameters for the parameter array. Each real parameter is an expression, in addition, the parameter array is equivalent to the value parameter of the same type. For example:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Class Prmarry

{

Public static void Show (params string [] name)

{

Console. WriteLine ("Array contains the number of elements: {0}", name. Length );

Console. Write ("elements of NameArray :");

For (int I = 0; I <name. Length; I ++)

{

Console. Write ("{0, 10}", name [I]);

}

}

}

// Call Show

String [] NameArray = {"rohelm. X", "Boolean", "rrats "};

Prmarry. Show (NameArray );

Console. ReadKey ();

I don't know what to do. My input method and compiler seem to be playing tricks. After a while, I won't support Chinese character input, and I can really enter it in English.

The following is the reference source code for this log, which can be analyzed as a whole:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

Using System;

 

Namespace prg1

{

Class Paramstest

{

// Value parameter usage demonstration

Public static void Transposition_1 (int a, int B)

{

Int temp =;

A = B;

B = temp;

}

// Demo of referencing Parameters

Static void Transposition_2 (ref int a, ref int B)

{

Int temp =;

A = B;

B = temp;

}

// Use of output parameters

Static void Transposition_3 (string name, out string FistName, out string LastName)

{

Int I = name. Length; // Get the length of the string

While (I> 0)

{

Char chparm = name [I-1];

If (chparm = '.')

{

Break;

}

I --;

}

 

FistName = name. Substring (0, I-1 );

LastName = name. Substring (I );

}

Static void Main (string [] args)

{

Int a = 25;

Int B = 30;

// Call Transposition_1

Console. WriteLine ("Call Transposition_1 before a = {0}, B = {1}", a, B );

Transposition_1 (a, B );

Console. WriteLine ("Call Transposition_1 after a = {0}, B = {1}", a, B );

Console. WriteLine ("===============================\ n ");

// Call Transposition_2

Console. WriteLine ("Call Transposition_2 before a = {0}, B = {1}", a, B );

Transposition_2 (ref a, ref B );

Console. WriteLine ("after calling Transposition_2 a = {0}, B = {1}", a, B );

Console. WriteLine ("===============================\ n ");

// Call Transposition_3

String DoName, Nmark;

Transposition_3 ("rohelm. X", out DoName, out Nmark );

Console. WriteLine ("Domain Name of Myself: {0}", DoName );

Console. WriteLine ("The last name of my Domain Name: {0}" + "\ n", Nmark );

// Call Show

String [] NameArray = {"rohelm. X", "Boolean", "rrats "};

Prmarry. Show (NameArray );

Console. ReadKey ();

}

}

Class Prmarry

{

Public static void Show (params string [] name)

{

Console. WriteLine ("Array contains the number of elements: {0}", name. Length );

Console. Write ("elements of NameArray :");

For (int I = 0; I <name. Length; I ++)

{

Console. Write ("{0, 10}", name [I]);

}

}

}

}

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.