您的位置:首页 > 编程语言 > C#

参数数组和普通数组

2016-07-19 23:01 323 查看

参数数组和普通数组

举一个数组求和的例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _011_参数数组
{
class Program
{
static int Sum(int[] num)
{
int sum = 0;
foreach(int tem in num)
{
sum += tem;
}
return sum;
}
static int Sum2(params int[] num)
//参数数组关键字 params
{
int sum = 0;
foreach (int tem in num)
{
sum += tem;
}
return sum;
}
static void Main(string[] args)
{
int[] sss = new int[] { 23, 23, 123, 123, 123 };
Console.WriteLine(Sum(sss));
Console.WriteLine(Sum2(22, 56, 7, 4, 34, 34));
//可以直接填入数字,无需定义数组
Console.ReadKey();

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C#