本文实例讲述了C#实现向函数传递不定参数的方法。分享给大家供大家参考。具体实现方法如下:
using System;class Min{ public int MinVla(params int [] nums){ int m; if (nums.Length == 0){ Console.WriteLine("Error: no arguments."); return 0; } m = nums[0]; foreach ( int val in nums){ if ( val < m){ m = val; } } return m; }}class ParamsDemo{ static void Main(){ Min ob = new Min(); int min; int a=10,b=20; min = ob.MinVla(a,b); Console.WriteLine("Minimum is " + min); min = ob.MinVla(a,b,-1); Console.WriteLine("Minimum is " + min); min = ob.MinVla(18,23,3,14,25); Console.WriteLine("Minimum is " + min); int [] args = {45,67,34,9,112,8}; min = ob.MinVla(args); Console.WriteLine("Minimum is " + min); }}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答