zoukankan      html  css  js  c++  java
  • 参数为数组的方法

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication7
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int sum1;
    14             int[] intArray = { 1,2,3};
    15             sum1 = Test(intArray);
    16             Console.WriteLine("数组的和是:{0}",sum1);
    17             Console.ReadKey();
    18         }
    19         public static int Test(int[] numbers)
    20         {
    21             int sum = 0;
    22             for (int i = 0; i < numbers.Length; i++)
    23             {
    24                 sum += numbers[i];
    25             }
    26             return sum;
    27         }
    28     }
    29 }

     如果参数只是传入数组的个别元素,则可以这个样写:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication7
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             int sum1;
    14             int[] intArray = { 1,2,3};
    15             sum1 = Test(intArray[0],intArray[2]);
    16             Console.WriteLine("数组的和是:{0}",sum1);
    17             Console.ReadKey();
    18         }
    19         public static int Test(int a, int b)
    20         {
    21             int sum = a + b;
    22             return sum;
    23         }
    24     }
    25 }
  • 相关阅读:
    单例模式 & Init(allocWithZone:)
    Go 初体验
    Go 初体验
    beego 初体验
    beego 初体验
    beego 初体验
    beego 初体验
    beego 初体验
    beego 初体验
    beego 初体验
  • 原文地址:https://www.cnblogs.com/start-from-scratch/p/5056476.html
Copyright © 2011-2022 走看看