Param array parameters

Source: Internet
Author: User

Source: http://mcdelfino.blog.51cto.com/2058744/661211

The param array allows us to write only one method to accept variable parameters. this technique is a parameter array, which is essentially a parameter declared using the params keyword. in addition, you can declare arrays such as params int [] list and parameter groups of the object type. The parameters can be of any type ~

Util class

  1. # Region Using directives
  2. Using System;
  3. # Endregion
  4.  
  5. Namespace ParamsArray
  6. {
  7. Class Util
  8. {
  9. Public static int Sum (params int [] paramList)
  10. {
  11. If (paramList = null)
  12. {
  13. Throw new ArgumentException ("Util. Sum: null parameter list ");
  14. }
  15.  
  16. If (paramList. Length = 0)
  17. {
  18. Throw new ArgumentException ("Util. Sum: empty parameter list ");
  19. }
  20.  
  21. Int sumTotal = 0;
  22. Foreach (int I in paramList)
  23. {
  24. SumTotal + = I;
  25. }
  26. Return sumTotal;
  27. }
  28.  
  29. Public static void Everyone (params object [] paramobject)
  30. {
  31. If (paramobject = null)
  32. {
  33. Throw new Exception ("Util. Everyone: null parameter list ");
  34. }
  35.  
  36. If (paramobject. Length = 0)
  37. {
  38. Throw new Exception ("Util. Everyone: empty parameter list ");
  39. }
  40.  
  41. Foreach (object I in paramobject)
  42. {
  43. Console. WriteLine (I );
  44. }
  45. }
  46. }
  47. }

Program class

  1. # Region Using directives
  2.  
  3. Using System;
  4. Using System. Collections. Generic;
  5. Using System. Text;
  6.  
  7. # Endregion
  8.  
  9. Namespace ParamsArray
  10. {
  11. Class Program
  12. {
  13. Static void Entrance ()
  14. {
  15. Console. WriteLine (Util. Sum (10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ));
  16. }
  17.  
  18. Static void Entrance01 ()
  19. {
  20. Console. WriteLine (Util. Sum (); // The length is 0.
  21. }
  22.  
  23. Static void Entrance02 ()
  24. {
  25. Console. WriteLine (Util. Sum (null); // The array is null.
  26. }
  27.  
  28. Static void Entrance03 ()
  29. {
  30. Util. Everyone ("dkjf", 45," 654 ", 451.5 );
  31. }
  32.  
  33. Static void Main ()
  34. {
  35. Try
  36. {
  37. Entrance ();
  38. Entrance01 ();
  39. }
  40. Catch (Exception ex)
  41. {
  42. Console. WriteLine ("Exception: {0}", ex. Message );
  43. }
  44.  
  45. Try
  46. {
  47. Entrance02 ();
  48. }
  49. Catch (Exception ex)
  50. {
  51. Console. WriteLine ("Exception: {0}", ex. Message );
  52. }
  53.  
  54. Try
  55. {
  56. Entrance03 ();
  57. }
  58. Catch (System. Exception ex)
  59. {
  60. Console. WriteLine ("Exception: {0}", ex. Message );
  61. }
  62. }
  63. }
  64. }

After each try statement is executed, the internal part of the try statement after the Exception will not be executed again, so another try statement should be written.

The statement execution result is as follows:

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.