




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、值参数声明时不带修饰符的形参是值形参。一个值形参对应一个局部变量,只是它的初 始值来自于该方法调用所提供的相应实参。快速替换变量Alt + Enterusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ParametersExampleclass Programstatic void Main(string args)Student Oldstu = new Student() Name = Tim ;So
2、meMethod(Oldstu);Console.WriteLine(0, 1, Oldstu.GetHashCode(),Oldstu.Name);static void SomeMethod(Student stu)stu = new Student() Name = Tom ;Console.WriteLine(0, 1, stu.GetHashCode(), stu.Name);class Studentpublic string Name get; set; using System;using System.Collections.Generic;using System.Linq
3、;using System.Text;using System.Threading.Tasks;namespace ParametersExampleclass Programstatic void Main(string args)Student stu = new Student() Name = Tim;UpdateObject(stu);Console.WriteLine(0, 1, stu.GetHashCode(), stu.Name);static void UpdateObject(Student stu)stu.Name = Tom;/ 副作用,side-effectCons
4、ole.WriteLine(0, 1, stu.GetHashCode(), stu.Name);class Studentpublic string Name get; set; 引用参数引用形参是用ref修饰符声明的实参,引用形参并不创建新的存储位置。引用形 参表示的存储位置恰是在方法调用中作为实参给出的那个变量所表示的存储位 置。变量在可以作为引用形参之前,必须先明确辅助。注意:引用参数并不创建变量的副本使用ref修饰符显示指出一一此方法的副作用是改变实际参数的值using System;using System.Collections.Generic;using System.Linq
5、;using System.Text;using System.Threading.Tasks;namespace ParametersExample class Program static void 5如 args)int y = 1;IWantSideEffect(ref y);static void IWantSideEffect(ref int x)/x*引用参数-引用类型,创建新对象using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thread
6、ing.Tasks;namespace ParametersExampleclass Programstatic void Main(string args)Student outterStu = new Student() Name = Tim ;Console.WriteLine(HashCode=0, Name=1, outterStu.GetHashCode(), outterStu.Name);Console.WriteLine();IWantSideEffect(ref outterStu);Console.WriteLine(HashCode=0, Name=1, outterS
7、tu.GetHashCode(), outterStu.Name);static void IWantSideEffect(ref Student stu)stu = new Student() Name = Tom ;Console.WriteLine(HashCode=0, Name=1, stu.GetHashCode(),stu.Name);class Student岫 string g 5引用参数-引用类型,不创建新对象只改变对象值此时与传值参数去掉(ref)在效果上并无不同,但机理不一样;前者的outterStu stu指针一致,后者为指针的指针一致using System;usi
8、ng System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ParametersExampleclass Programstatic void Main(string args)Student outterStu = new Student() Name = Tim ;Console.WriteLine(HashCode=0, Name=1,outterStu.GetHashCode(), outterStu.Name);Console.Writ
9、eLine();IWantSideEffect(ref outterStu);Console.WriteLine(HashCode=0, Name=1,outterStu.GetHashCode(), outterStu.Name);static void IWantSideEffect(ref Student stu)stu.Name = Tom;Console.WriteLine(HashCode=0, Name=1,stu.GetHashCode(),stu.Name);class Studentpublic string Name get; set; 输出形参用out修饰符声明的形参;
10、输出形参不需要创建新的存储位置;变量在可以作为 输出形参传递之前不一定需要明确赋值;在方法返回之前,该方法的每个输出形 参都必须明确赋值;注意:输出参数并不创建变量的副本方法体内必需要有对输出变量的赋值的操作用Out修饰符显示指出一一此方法的副作用是通过参数向外输出值从语义上讲一一ref是为了 “改变”,out是为了 “输出”输出参数一一值类型using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Parame
11、tersExampleclass Programstatic void Main(string args)Console.WriteLine(Please inout first number:);string arg1 = Console.ReadLine();double x = 0;bool b1 = double.TryParse(arg1, out x);if(b1 = false)Console.WriteLine(Input error!);return;Console.WriteLine(Please inout second number:);string arg2 = Co
12、nsole.ReadLine();double y = 0;bool b2 = double.TryParse(arg2, out y);if (b2 = false)Console.WriteLine(Input error!);return;double z = x + y;Console.WriteLine(0 + 1 = 2”, x, y, z);using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace
13、ParametersExampleclass Programstatic void Main(string args)f x = 100;bool b = DoubleParser.TryParse(ABC”, out x);:(b=true)_+1);elseerb_ i_tryResult = doub5catch板t=0;/”输出类型一一引用类型using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Pa
14、rametersExampleclass Programstatic void Main(string args)Student stu = null;bool b = StudentFactory.Create(Tim”, 34, out stu);if (b = true)Console.WriteLine(Student 0, age is 1.”, stu.Name, stu.Age);class Studentpublic int Age get; set; public string Name get; set; class StudentFactorypublic static
15、bool Create(string stuName, int stuAge, out Student result)result = null;if(string.IsNullOrEmpty(stuName)return false;if(stuAge80)return false;_,心5数组参数必需是形参列表中的最后一个,由params修饰举例:String.Format 方法和 String.Split 方法using System;using System.Collections.Generic;using System.Linq;using System.Text;using Sy
16、stem.Threading.Tasks;namespace ParametersExampleclass Programstatic void Main(string args)int result = CalculateSum(1, 2, 3);:_ 5)int sum = 0;一,5广+=-,广;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ParametersExample( class Pr
17、ogram1 M void 55,(s.string result = str.Split(;, ., ,);i(n”具名参数参数的位置不再受约束提高代码可读性using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ParametersExampleclass Program static void gs)P_ );g Prage)enam可选参数参数因为具有默认值而变得“可选”不推荐使用可选参数using S
18、ystem;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ParametersExampleclass Program static 用 5倾 args)PrinfInfo();static void PrinfInfo(string name = Tim, int age = 34)Ceige)扩展方法(this参数)方法必需是公有的、静态的,即被public static所修饰必需是形参列表中的第一个,由this修饰必需由一个静态类(一般类名为SomeTypeExtension)来统一收纳对SomeType类型的扩展方法举例:LINQ方法using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Task
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年中国锦纶切片行业竞争格局规划研究报告
- 2025-2030年中国铜矿采选行业发展状况及营销战略研究报告
- 2025-2030年中国蜂窝纸板市场运营状况及投资战略研究报告
- 2025-2030年中国药学教育发展模式及未来投资战略分析报告
- 2025-2030年中国聚碳酸酯pc行业运行状况规划分析报告
- 2025-2030年中国粗杂粮行业竞争格局及发展前景分析报告
- 2025-2030年中国空气污染治理设备市场经营状况及发展趋势分析报告
- 2025-2030年中国码垛机器人市场运行动态及发展前景分析报告
- 幼儿健康有营养的蔬菜教案(12篇)
- 中国传媒大学《电子与电工技术》2023-2024学年第二学期期末试卷
- 学生创新能力培养方案计划
- 各级人员及各岗位安全生产责任制度
- 2025年湖北省技能高考(建筑技术类)《建筑材料与检测》模拟练习试题库(含答案)
- 2024-2025学年第二学期教学教研工作安排表 第二版
- 七年级地理下册 9.2 巴西说课稿 (新版)新人教版
- 人行道道铺设施工方案
- 开展课外读物负面清单管理的具体实施举措方案
- 人体的免疫系统课件
- 六年级下学期开学第一课
- 体育原理课件
- 三界天人表格-
评论
0/150
提交评论