版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《土力学与地基基础》-第七章
- 交通枢纽安全设施专项维修计划
- 2026年CCNP认证技能考核及答案
- 企业劳动合同管理全流程操作手册
- 小学古诗词必背名句分类
- 汽车销售合同范本及理赔流程
- 部编版四年级语文课文专项教案设计
- 课件玩耍安全教案
- 现代仓储管理流程优化与案例分析
- 高效项目管理进度控制技巧
- 2025国开《中国古代文学(下)》形考任务1234答案
- 肺部感染中医护理
- 租地合同协议书合同
- 《肺炎的CT表现》课件
- 粮食仓储设施建设维修资金申请报告
- 脑器质性精神障碍护理查房
- 中考英语听力命题研究与解题策略省公开课金奖全国赛课一等奖微课获奖课件
- 物联网智能家居设备智能控制手册
- 2023-2024学年湖北省武汉市东西湖区五年级(上)期末数学试卷(含答案)
- 怀化市2024-2025学年高一上学期期末地理试题(含答案解析)
- 全国班主任比赛一等奖《班主任经验交流》课件
评论
0/150
提交评论