博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#语言学习--基础部分(十八)数组参数
阅读量:6908 次
发布时间:2019-06-27

本文共 3307 字,大约阅读时间需要 11 分钟。

一.

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的函数 
        }
    }
}

 

 

转载于:https://www.cnblogs.com/sharpenabc/archive/2012/08/30/2664661.html

你可能感兴趣的文章
mysql无故关闭_宝塔的mysql老是自己关闭停止
查看>>
arm ubuntu 编译boost_ROS在ARM上的编译 | 学步园
查看>>
websocket如何区分用户_看完让你彻底理解 WebSocket 原理,附完整的实战代码(包含前端和后端)...
查看>>
手机怎么进ph_转:15年经验的老师傅告诉你怎么解决电镀上常见的9个问题
查看>>
gdb条件断点的值一定会断吗_天天带着这个工具,你也不一定完全知道它的一切...
查看>>
怎么注册tk域名_域名注册后怎么做网站?有了域名如何搭建网站?
查看>>
routing zuul_Zuul网关
查看>>
见缝插针的人_菁华语学法 修六和敬 见缝插针 基本功训练(下)——修六和敬 见缝插针 基本功训练...
查看>>
java位运算符取反_Java逻辑运算符,位运算符
查看>>
lightgbm过去版本安装包_OLT版本升级操作指南
查看>>
idea中的java文件是j状态_JVM中必须要掌握的java的.class文件的加载过程
查看>>
docker 制作本地镜像_每天5分钟|轻松掌握开发必会的docker套路-制作自己的镜像...
查看>>
打包css合并_编写灵活、稳定、高质量的CSS代码的规范
查看>>
库 keil 编译很慢_CmBacktrace: ARM CortexM 系列 MCU 错误追踪库
查看>>
订阅多个主题_SpringBoot整合Redis,怎么实现发布/订阅?
查看>>
tcl自动保存结果expect_TCL/Expect读取配置文件内容
查看>>
runaction 旋转_Cocos Creator 中的动作系统那些事儿
查看>>
比亚迪汉搭载鸿蒙系统和麒麟芯片_官宣!搭载鸿蒙系统和鸿鹄芯片,华为荣耀在东莞发布首款“智慧屏”!...
查看>>
html接收model值_v-bind和v-model的区别
查看>>
java继承两个类_Java入门第十六课:如何用继承的方法定义类
查看>>