C #: learning the Params keyword

Source: Internet
Author: User

I saw Params today. I learned it without having used it before.

The Params keyword has the following features:

  • The Params keyword can be used to specify a variable number of parameters (the number of parameters is not specified ).
  • You can send a comma-separated list of parameters of the specified type in the parameter declaration or a parameter array of the specified type.
  • You can leave the parameter unspecified.
  • After the Params keyword in the method declaration, no other parameters are allowed, and only one Params keyword is allowed in the method declaration.

 

View code

 1 using system;
2 using system. Collections. Generic;
3 using system. LINQ;
4 using system. text;
5
6 namespace consoleapplication5
7 {
8. Public class myclass
9 {
10 public static void useparams (Params int [] list)
11 {
12 For (INT I = 0; I <list. length; I ++ ){
13 console. Write (list [I] + "");
14}
15 console. writeline ();
16}
17
18 public static void useparams2 (Params object [] list)
19 {
20 For (INT I = 0; I <list. length; I ++ ){
21 console. Write (list [I] + "");
22}
23 console. writeline ();
24}
25
26 static void main ()
27 {
28 // you can send a comma-separated list of arguments of the specified type.
29 // you can use a comma-separated sequence parameter to specify the type.
30 useparams (1, 2, 3, 4 );
31 useparams2 (1, 'A', "test ");
32
33 // A Params parameter accepts zero or more arguments.
34 // One Params parameter accepts zero or multiple parameters.
35 // The following calling statement displays only a blank line.
36 // The statement below shows only one blank line.
37 useparams2 ();
38
39 // an array argument can be passed, as long as the Array
40 // type matches the parameter type of the method being called.
41 // as long as the array type conforms to the parameter type of the method, it can be called
42 int [] myintarray = {5, 6, 7, 8, 9 };
43 useparams (myintarray );
44
45 object [] myobjarray = {2, 'B', "test", "again "};
46 useparams2 (myobjarray );
47
48 // The following call causes a compiler error because the object
49 // array cannot be converted into an integer array.
50 // useparams (myobjarray );
51
52 // The following call does not cause an error, but the entire
53 // integer array becomes the first element of the Params array.
54 useparams2 (myintarray );
55}
56}
57}
58

In fact, the usage of the Params keyword is quite flexible! You can try it later!

(This article references msdn: http://msdn.microsoft.com/zh-cn/library/w5zay9db (V = vs.100). aspx)

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.