码迷,mamicode.com
首页 > 编程语言 > 详细

C#值参数和引用参数,方法的重载,foreach,数组,以及ref和out的用法

时间:2016-10-22 00:21:31      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:try   index   sort   排序   成绩   声明   数组名   最小值   1.5   

1.方法的传输传递

值参数:传递的是副本

引用参数:自身 保留自定义的方法中对值的改变 形参影响实参
ref:对应的形参和实参都用ref修饰

输出参数:实参不用赋值,但是自定义方法内必须对此参数赋值!!! 把自定义方法产生的结果带回调用处
out:对应的形参和实参都用out修饰 必须在自定义方法中赋值

注:如果需要返回一个参数 使用return
如果需要反回多个参数 使用ref 或者 out

TryParse用法:自行判断转换是否成功 转换成功反回true 转换失败返回false

例: bool f = int.TryParse(Console.ReadLine(), out n2);
Console.WriteLine(f);

Console.ReadKey();
2.方法的重载(OverLoad)

定义:同一个类中 同名不同参数列表的方法 和返回类型无关 方法的重载(比如WriteLine方法一共有19中重载)

优点:同一个方法 根据参数列表的类型调用不同的重载方法 使用方便

例子:
自定义三个重载的方法
public static int Swap(int a,int b)
{
return a + b;
}
public static double Swap(double a,double b)
{
return a + b;
}
public static string Swap(string a,string b)
{
return a + b;
}
Main方法中的调用
int res1 = Swap(1, 2);
double res2 = Swap(1.5, 2.5);
string res3 = Swap("张三", "李四");
Console.WriteLine("结果分别是:\n{0}\n{1}\n{2}", res1, res2, res3);
Console.ReadKey();


3.递归方法
汉诺塔:经典递归问题
在方法内自己调用自己,必须要有递归结束的条件
举例:
定义一个方法,在Main中调用,输出从1加到100的和
public static int Su(int n) {

if (n==1)
{
return 1;
}
n= Su(n - 1)+n;
return n;
}
Main函数中调用
Console.WriteLine(Su(100));
Console.ReadKey();

数组

1.什么是数组:

a、是一种数据结构
b、大小固定 保存数据是同一数据类型
c、多个数据公用一个名字(数组名) 索引从0开始 到n-1
d、数组中的每一个数据称为数组元素 数组名[索引]
e、数组是引用数据类型

2.数组的作用:存储数据

3.数组的使用方法:

声明
数据类型[] 数组名;
如:int[] arr;
初始化
数组名 = new 数据类型[大小];
如:arr=new int[5];
赋值
arr[0]=85;
arr[1]=80;
arr[2]=77;

使用
Length:数组元素的个数

遍历

foreach(元素类型 变量 in 数据或集合名)
练习:
//1、循环输出所有行星名称
//2、用户录入一个行星名称,返回它在太阳系中的位置
//3、建立一个太阳系成员数组,第一个元素是“太阳”,将行星数组的内容复制到太阳系数组,然后遍历输出。

代码:
namespace Day05
{
class Program
{
static void Main(string[] args)
{
数组的使用
声明
int[] arr1;
初始化
arr1=new int[5];
声明并初始化
int[] arr2 = new int[5];
赋值
1.数组元素逐一赋值
arr1[0] = 80;
2.前三步合并
int[] arr3 = new int[5] { 1,2,3,4,5};
int[] arr3 = new int[] { 1,2,3,4,5};
int[] arr3;
arr3 = new int[5] { 1,2,3,4,5};
int[] arr3 = { 1,2,3,4,5};
3.动态赋值
for (int i = 0; i < 5; i++)
{
 Console.Write("请输入第{0}个同学的成绩:",(i+1));
 arr1[i] = int.Parse(Console.ReadLine());
}

使用
求和
double sum=0;
for (int i = 0; i < arr1.Length; i++)
{

 sum += arr1[i];
}
平均
double avg = sum / arr1.Length;
Console.WriteLine("和:"+sum);
Console.WriteLine("平均值:"+avg);

最大值
假设第一个元素为最高分 数组赋值后
int max = arr1[0];
for (int i = 1; i < arr1.Length; i++)
{
 if (arr1[i] > max)
 {
 max = arr1[i];
 }
}
Console.WriteLine("最高分:"+max);
最小值

课堂练习
键盘输入5个同学的成绩,分别计算总分、平均分、最高分、最低分

声明并初始化数组
int[] score = new int[5];
动态接收键盘输入(赋值) 累加
double sum = 0;//总分
for (int i = 0; i < score.Length; i++)
{
 Console.Write("请输入第{0}个学生的成绩:", (i + 1));
 score[i] = int.Parse(Console.ReadLine());
 sum += score[i];
}
double avg = sum / score.Length;
求最大值 最小值
int max = score[0];//最高分
int min = score[0];//最低分
for (int i = 1; i < score.Length; i++)
{
 if (score[i] > max)
 {
 max = score[i];
 }
 if(score[i]<min)
 {
 min = score[i];
 }
}

Console.WriteLine("考试成绩为:");
for (int i = 0; i < score.Length; i++)
{
 Console.Write(score[i] + "\t");
}

遍历 用于数组或集合 不能改变数组的值 不能给迭代变量重新赋值

foreach (int point in score)
{
 Console.Write(point+"\t");
}
Console.WriteLine("\n\n总分\t平均分\t最高分\t最低分");
Console.WriteLine("{0}\t{1}\t{2}\t{3}", sum, avg, max, min);

数组的常用属性和方法
Length:属性 数组所有维度上元素的总数
int[,] score = new int[3, 5]{{11,12,13,14,15},{21,22,23,24,25},{31,32,33,34,35}};
Console.WriteLine("数组元素的数量:{0}",score.Length);
Console.WriteLine("数组的维数:{0}",score.Rank);
方法
int[] arr = new int[] { 67,2,34,2,58};
排序 升序排序
Array.Sort(arr);
Console.WriteLine("排序:");
foreach (int a in arr)
{
 Console.Write(a+"\t");
}
反转
Array.Reverse(arr);
Console.WriteLine("\n反转:");
foreach (int a in arr)
{
Console.Write(a + "\t");
}
Console.WriteLine();
复制
int[] arr2=new int[6];
Array.Copy(arr,1,arr2,2,3);
Console.WriteLine("复制:");
foreach (int a in arr2)
{
 Console.Write(a + "\t");
}
Console.WriteLine();
查找
int index1 = Array.IndexOf(arr,2);
int index2 = Array.LastIndexOf(arr, 2);
Console.WriteLine(index1);
Console.WriteLine(index2);
清空
Array.Clear(arr,0,arr.Length);
foreach (int a in arr)
{
 Console.Write(a + "\t");
}

CopyTo
arr.CopyTo(arr2,1);
foreach (int a in arr2)
{
 Console.Write(a + "\t");
}

 课堂练习

/*
* 建立一个太阳系行星数组,要求:
1、循环输出所有行星名称
2、用户录入一个行星名称,返回它在太阳系中的位置
3、建立一个太阳系成员数组,第一个元素是“太阳”,将行星数组的内容复制到太阳系数组,然后遍历输出。
*/
string[] planets = new string[] { "金星","木星","水星","火星","土星","地球","天王星","海王星"};
//输出

foreach (string s in planets)
{
Console.Write(s+" ");
}
Console.WriteLine();
//查找
Console.Write("请输入要查找的行星的名字:");
string planet = Console.ReadLine();
int index = Array.IndexOf(planets,planet);
if (index != -1)
{
Console.WriteLine("位置:{0}", index);
}
else
{
Console.WriteLine("不存在");
}
//复制
string[] solar=new string[planets.Length+1];
solar[0] = "太阳";
planets.CopyTo(solar,1);
//遍历输出
foreach (string s in solar)
{
Console.Write(s+" ");
}
Console.WriteLine();
#endregion
Console.ReadKey();
}
}
}


C#值参数和引用参数,方法的重载,foreach,数组,以及ref和out的用法

标签:try   index   sort   排序   成绩   声明   数组名   最小值   1.5   

原文地址:http://www.cnblogs.com/harveylv/p/5986355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!