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();

关于方法的总结:

定义方法的语法
方法的调用
值参数
引用参数:ref
输出参数:out
方法的重载
格式化文本

数组

1.什么是数组:

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

2.数组的作用:

3.使用数组

a、声明
数据类型[]数组名;
如:int[]arr;
初始化
数组名=new

遍历

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

代码:
namespace Day05
{
class Program
{
static void Main(string[] args)
{
#region 数组的使用
////声明
//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);
////最小值
#endregion

#region 课堂练习
////键盘输入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);
#endregion

#region 数组的常用属性和方法
////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");
//}

#endregion

#region 课堂练习
/*
* 建立一个太阳系行星数组,要求:
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();
}
}
}

005.数组、for、foreach的更多相关文章

  1. 了解PHP中的Array数组和foreach

    1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组    . 2.例子:一般的数组 这里,我 ...

  2. JavaScript中的数组遍历forEach()与map()方法以及兼容写法

    原理: 高级浏览器支持forEach方法 语法:forEach和map都支持2个参数:一个是回调函数(item,index,list)和上下文: forEach:用来遍历数组中的每一项:这个方法执行是 ...

  3. 处理数组的forEach map filter的兼容性

    处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback ...

  4. 数组的foreach方法和jQuery中的each方法

    /* * 数组的forEach方法: * 1.返回给回调的参数先是值,然后是下标 * 2.回调函数执行时内部的this指向window * */ /*var arr = [1,2,3,4,5]; ar ...

  5. ES6新增的常用数组方法(forEach,map,filter,every,some)

    ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...

  6. js数组的forEach方法能不能修改数组的值

    如果要使用数组的forEach()方法对其改值时,需要直接通过arr[i]这种方式来更改. 请看下面代码: // 数组改值 let arr = [1,3,5,7,9]; arr.forEach(fun ...

  7. PHP 数组遍历 foreach 语法结构

    foreach 语法结构用于遍历数组. foreach() PHP foreach() 语法结构用于遍历操作或输出数组,foreach() 仅能用于遍历数组或对象,当试图将其用于其它数据类型或者一个未 ...

  8. 数组遍历 forEach 方法

    数组的遍历 遍历数组,将数组中的所有元素都取出来. 使用for 循环执行数组的索引(length-1)相同的次数. var arr=["1", "5", &qu ...

  9. ES5 数组方法forEach

    ES6已经到了非学不可的地步了,对于ES5都不太熟的我决定是时候学习ES5了. 1.  js 数组循环遍历. 数组循环变量,最先想到的就是 for(var i=0;i<count;i++)这样的 ...

随机推荐

  1. (中等) CF 555E Case of Computer Network,双连通+树。

    Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...

  2. iOS之UITableView的上拉刷新

    #import "ViewController.h" #import "UITableView+PullRefresh.h" @interface ViewCo ...

  3. POJ 3187 Backward Digit Sums

    暴力DFS+验证. 验证如果是暴力检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理. #include<cstdio> #include<cstring& ...

  4. Linux 环境编译安装mysql (源码安装包)

    标注: Linux需要先配置网络yum源,确定yum能在线安装软件包,方便测试过程中安装部分依赖包.配置163网易提示的网络yum源参考博客  http://www.cnblogs.com/zoulo ...

  5. VS2010打开旧版本MFC工程无对话框

    解决方案: 左侧有个"资源视图",打开,里面就能找得到对话框,如果没有资源视图,就在菜单的视图选项里打开资源视图!

  6. tableview的reloadData应注意

    http://blog.csdn.net/ouyangtianhan/article/details/7835041 http://stackoverflow.com/questions/160715 ...

  7. Online Schema Change for MySQL

    It is great to be able to build small utilities on top of an excellent RDBMS. Thank you MySQL. This ...

  8. jstree使用小结(二)

    继续上一篇: 1.数据 按照官网的json数据返回格式: 有两种格式,我使用的格式如下: $('#jstree1').jstree({ 'core' : { 'data' : [ { "id ...

  9. sqlserver递归查询

    --递归查询 with cte as ( ' union all select k.id,k.Text, k.name,k.pid from Menu k inner join cte c on c. ...

  10. IOS开发根据字体大小等获取文字所占的高度

    Model *model = self.modelArr[indexPath.row]; //根据label文字获取CGRect NSMutableParagraphStyle *paragraphS ...