一.
1.不能仅使用params来重载方法
2.不允许out,ref和params同时使用
3.params数组必须是最后一个参数
4.一个没有params的方法的优先级要高于带params的方法
5.对于引起歧义的方法重载,编译器会报错.
eg:public static int Min(params int[] paramsList)
public statc int Min(int i,params int[] paramsList )
//complie-time error
Console Demo
Util.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParamsDemo
{
class Util
{
/* public static int Min(int[] paramList) { if (paramList == null || paramList.Length == 0) { throw new ArgumentException("Uintil.Min:Not enougn arguments"); } int currentMin = paramList[0]; foreach (int i in paramList) { if (i < currentMin) { currentMin = i; } } return currentMin; } */ public static int Min(
params int[] paramList)
{
if (paramList ==
null || paramList.Length ==
0)
{
throw new ArgumentException(
" Uintil.Min:Not enougn arguments ");
}
int currentMin = paramList[
0];
foreach (
int i
in paramList)
{
if (i < currentMin)
{
currentMin = i;
}
}
return currentMin;
}
}
}
2.Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParamsDemo
{
class Program
{
static void Main(
string[] args)
{
/* int[] intArray = new int[2] { 55, 66 }; int min=Util.Min(intArray); Console.WriteLine(min); Console.WriteLine("------------------------"); int[] intArraY1 = new int[8] { 11, 33, 55, 8, 22, 10, 77, 88 }; int min1 = Util.Min(intArraY1); Console.WriteLine(min1); */ int min1 = Util.Min(
100,
200,
300,
80,
90,
5,
90);
Console.WriteLine(min1);
}
}
}
二.
使用params object 数组
class Black{
public static void Hole(params object[] paramList)
}
代表Hole这个方法能接收任意参数
1,可能不给此方法传递任何参数
Black.Hole()
2.可以传递null参数
Black.Hole(null);
3.可以传递一个精确的数组
object[] array=new object(2);
array[0]="forty two";
array[1]=42;
Black.Hole(array);
4,可以传递不同为型的参数
Black.Hole("forty two",42);
//converted to Black.Hole(new object[]{"forty two",42});
Console Demo
Util.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParamsArray
{
class Util
{
public static int Sum(
params int[] paramsList)
// 任意长度的参数个数 {
Console.WriteLine(
" Using parameter list ");
if (paramsList ==
null)
{
throw new ArgumentException(
" Util.Sum:null parameters in paramsList ");
}
if (paramsList.Length ==
0)
{
throw new ArgumentException(
" Util.Sum:empty paramsList ");
}
int sumTotal =
0;
foreach (
int i
in paramsList)
{
sumTotal += i;
}
return sumTotal;
}
public static int Sum(
int parm1=
0,
int parm2=
0,
int parm3=
0,
int parm4=
0)
// 最多只能传递4个整型参数(可选参数类型) {
Console.WriteLine(
" Using optional parameters ");
int sumTotal = parm1 + parm2 + parm3 + parm4;
return sumTotal;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParamsArray
{
class Program
{
static void Main(
string[] args)
{
try {
DoWork();
}
catch (Exception ex)
{
Console.WriteLine(
" Exception:{0} ",ex.Message);
}
}
static void DoWork()
{
// Console.WriteLine(Util.Sum(null)); // Console.WriteLine(Util.Sum()); Console.WriteLine(Util.Sum(
10,
9,
8,
7,
6,
5,
4,
3,
2,
1));
Console.WriteLine(Util.Sum(
2,
4,
6,
8));
Console.WriteLine(Util.Sum(
2,
4,
6));
// 前三个参数赋值,后一个参数取默认值。此函数优先级高于带params的函数. Console.WriteLine(Util.Sum(
2,
4,
6,
8,
10));
// 调用的是带params的函数 }
}
}