C#中函数的使用
2024-09-02 00:35:02
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static int MaxValue(int[] intArray)
{
int maxVal = intArray[0];
for (int i = 1;i<intArray.Length;i++)
{
if (intArray[i] > maxVal)
{
maxVal = intArray[i];
}
}
return maxVal;
}
static void Main(string[] args)
{
int[] myArray = { 1, 3, 5, 8, 2, 10, 9, 100, 40 };
int maxVal = MaxValue(myArray);
Console.WriteLine("最大值为:{0}",maxVal);
Console.ReadKey();
}
}
}
params传参
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static int MaxValue(params int[] intArray)
{
int maxVal = intArray[0];
for (int i = 1;i<intArray.Length;i++)
{
if (intArray[i] > maxVal)
{
maxVal = intArray[i];
}
}
return maxVal;
}
static void Main(string[] args)
{
int maxVal = MaxValue(1,3,4,100,200,3);
Console.WriteLine("最大值为:{0}",maxVal);
Console.ReadKey();
}
}
}
out使用,类似于callback。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static int MaxValue(out int maxIndex,params int[] intArray)
{
int maxVal = intArray[0];
maxIndex = 0;
for (int i = 1;i<intArray.Length;i++)
{
if (intArray[i] > maxVal)
{
maxVal = intArray[i];
maxIndex = i;
}
}
return maxVal;
}
static void Main(string[] args)
{
int maxIndex;
int maxVal = MaxValue(out maxIndex,1,3,4,100,200,3);
Console.WriteLine("最大值为:{0}",maxVal);
Console.WriteLine("索引为:{0}", maxIndex);
Console.ReadKey();
}
}
}
最新文章
- Linux下安装Hadoop完全分布式(Ubuntu12.10)
- 安卓---Toast工具类,有点懒
- Java数据结构之回溯算法的递归应用迷宫的路径问题
- 获取Executor提交的并发执行的任务返回结果的两种方式/ExecutorCompletionService使用
- thinkphp 3.2 单入口 多模块 不能加载index控制器问题
- 高级UNIX环境编程11 线程
- 重复数据删除(De-duplication)技术研究(SourceForge上发布dedup util)
- mysql5.7.17安装问题
- [iOS]C语言技术视频-10-指针变量
- 20170410 --- Linux备课资料 --- vim的使用
- C# MessageBox.Show每隔3秒自动关闭
- php -- 断点调试 之 选择合适的xdebug
- C/C++基础----标准库几个工具库tuple,bitset,正则表达式,随机数,IO库
- css:清楚浮动
- [转]使用spring中的@Transactional注解时,可能需要注意的地方
- 使用vue模拟购物车小球动画
- scrapy定制爬虫-爬取javascript——乾颐堂
- ONTAK 2010 aut
- 设计模式03: Builder 生成器模式(创建型模式)
- Unicode(UTF-8, UTF-16)令人混淆的概念----我看完了 不错