资讯动态

c#方法进阶-各种参数

发布时间:2026/10/20 5:11:24 来源:尧图企业网站定制
值参数值参数是一个变量为传进来的实际参数创建一个副本它有值类型和引用类型两种参数;传值参数-值类型class Program { static void Main(string[] args) { Student stu new Student(); int y 100; stu.AddOne(y); //101 改变的是副本的值 Console.WriteLine(y); //100 不改变变量的值 } } class Student { public void AddOne(int x) //参数不带修饰符且是值类型-值类型的传值参数 { x x 1; Console.WriteLine(x); } }传值参数-引用类型并且新创建对象传的是地址而不只是一个值创建副本是实线箭头不创建副本是虚线箭头想向外暴露数据用属性比字段更安全*引用类型变量存储的值是引用类型实例在堆内存中的地址引用类型变量以传值的形式传入方法“引用类型”是说这个变量的类型是类、接口和委托中的一种“以传值的形式”意思是传值参数传进来的值会在方法体内部创建一个副本即使在方法内对这个副本进行了各种计算也不会改变调用者的实参即不会影响方法体外面变量的值示例class Program { static void Main(string[] args) { Student stu new Student() {NameTim}; SomeMethod(stu); //Tom Console.WriteLine(stu.Name); //Tim } static void SomeMethod(Student stu) { stunew Student() {NameTom}; //少见无意义 Console.WriteLine(stu.Name); } } class Student { public string Name { get; set; } }若都是Tom如何区分GetHashCode是 C# 里所有类型都有的一个方法定义在object上作用是返回一个整数得到代表对象的唯一值用来在哈希表如Dictionary、HashSet里快速定位对象。class Program { static void Main(string[] args) { Student stu new Student() {NameTom}; SomeMethod(stu); //46104728,Tom Console.WriteLine({0},{1},stu.GetHashCode(),stu.Name); //12289376,Tom } static void SomeMethod(Student stu) { stunew Student() {NameTom}; Console.WriteLine({0},{1}, stu.GetHashCode(), stu.Name); } } class Student { public string Name { get; set; } }改变量名字快捷键Ctrl。传值参数-引用类型只操作对象不创建新对象如果值参数传的是引用类型变量那么由于引用类型存的是实例在堆内存中的地址即使主参数和方法体内的副本是相互独立的但是它们的值一样也即指向堆内存中的同一个实例所以如果在方法体中通过副本引用类型变量操作了这个实例那么在主调者中通过实参指向的实例也会被改变本来指向的都是同一个实例。上述这种通过传进来的参数修改引用的对象的值的情况是很少见的因为作为方法而言它的主要输出还是靠返回值这种修改参数所引用的对象的值的操作叫方法的副作用(side-effect平时编程时要避免副作用。class Program { static void Main(string[] args) { Student stu new Student() {NameTim}; UpdateObject(stu); //HashCode46104728,NameTom Console.WriteLine(HashCode{0},Name{1}, stu.GetHashCode(), stu.Name); //HashCode46104728,NameTom } static void UpdateObject(Student stu) { stu.Name Tom; //副作用 Console.WriteLine(HashCode{0},Name{1},stu.GetHashCode(),stu.Name); } } class Student { public string Name { get; set; } }引用参数引用形参是用 ref 修饰符声明的形参。和值参数的区别是引用参数不会为传进来的实际参数创建一个副本形参和实参的变量是同一个地址即共用同一个变量引用参数利用其副作用是打算来修改传进来的值在传进方法之前一定要赋值。引用参数-值类型class Program { static void Main(string[] args) { int y 1; IWantSideEffect(ref y); Console.WriteLine(y); //101 } static void IWantSideEffect(ref int x) { x x 100; } }引用参数-引用类型创建新对象class Program { static void Main(string[] args) { Student outterStu new Student() {NameTim}; Console.WriteLine(HashCode{0},Name{1}, outterStu.GetHashCode(), outterStu.Name); //HashCode 46104728,Name Tim Console.WriteLine(------------------------); IWantSideEffect(ref outterStu); //HashCode 12289376,Name Tom Console.WriteLine(HashCode{0},Name{1}, outterStu.GetHashCode(), outterStu.Name); //HashCode 12289376,Name Tom } static void IWantSideEffect(ref Student stu) { stu new Student() {NameTom}; Console.WriteLine(HashCode{0},Name{1},stu.GetHashCode(),stu.Name); } } class Student { public string Name { get; set; } }引用参数-引用类型不创建新对象只改变对象值class Program { static void Main(string[] args) { Student outterStu new Student() {NameTim}; Console.WriteLine(HashCode{0},Name{1}, outterStu.GetHashCode(), outterStu.Name); //HashCode 46104728,Name Tim Console.WriteLine(------------------------); IWantSideEffect(ref outterStu); //HashCode 46104728,Name Tom Console.WriteLine(HashCode{0},Name{1}, outterStu.GetHashCode(), outterStu.Name); //HashCode 46104728,Name Tom } static void IWantSideEffect(ref Student stu) { stu.NameTom; Console.WriteLine(HashCode{0},Name{1},stu.GetHashCode(),stu.Name); } } class Student { public string Name { get; set; } }与传值参数效果一样但内存机理不同输出参数输出参数不为传进来的实际参数创建一个副本输出参数和输入的实参指的是同一个位置不要求在传进方法之前要赋值在方法体之内一定要赋值。输出参数利用其副作用是打算拿输出参数当作值的输出通过输出参数来获得除了方法返回值额外的输出输出参数-值类型TryParse安全地将字符串转换为其他类型如int、double、DateTime、Enum等不抛出异常只告诉解析结果是true还是false。普通的如int.Parse会抛出异常。class Program { static void Main(string[] args) { Console.WriteLine(Please input first number:); string arg1Console.ReadLine(); double x 0; bool b1double.TryParse(arg1, out x); if (b1false) { Console.WriteLine(Input error); return; } Console.WriteLine(Please input second number:); string arg2 Console.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); } }class Program { static void Main(string[] args) { double x 100; bool b DoubleParse.TryParse(ABC, out x); if (b) { Console.WriteLine(x1); } else { Console.WriteLine(x); } } } class DoubleParse { public static bool TryParse(string input,out double result) { try { result Double.Parse(input); return true; } catch { result 0; //不要求在传进方法之前要赋值在方法体之内一定要赋值。 return false; } } }输出参数-引用类型class Program { static void Main(string[] args) { Student stu null; bool b StudentFactory.Creat(Tim, 22, out stu); if (b) { Console.WriteLine(Student {0},age is {1}.,stu.Name,stu.Age); } } } class Student { public int Age { get; set; } public string Name { get; set; } } class StudentFactory { public static bool Creat(string stuName, int stuAge, out Student result) { result null; if (string.IsNullOrEmpty(stuName)) { return false; } if (stuAge 80 || stuAge 20) { return false; } result new Student() {NamestuName,AgestuAge}; return true; } }数组参数只能有一个且位于列表中的最后一个class Program { static void Main(string[] args) { int result CalculateSum(1, 2, 3, 4, 5); Console.WriteLine(result); } static int CalculateSum(params int[] intArray) { int sum 0; foreach (int i in intArray) { sum i; } return sum; } }string.Split用于将字符串按指定的分隔符拆分成多个子字符串数组的方法。class Program { static void Main(string[] args) { string str Tim;Tom,Amy.Lisa; string[] result str.Split(;, ., ,); foreach (var name in result) { Console.WriteLine(name); } } }具名参数具具有参数的位置不再受约束调用一个方法的时候传进去的参数是带有名字的class Program { static void Main(string[] args) { //不具名调用,按参数列表顺序依次填写实参 PrintInfo(Tim,34); //具名调用提高代码可读性参数位置不再受参数列表顺序影响 PrintInfo(age: 34, name: Tim); } static void PrintInfo(string name,int age) { Console.WriteLine(Hello {0},you are {1}.,name,age); } }可选参数调用方法的时候这个参数可选可不选参数在声明时添加默认值在调用方法时就可以不用参数而使用默认值class Program { static void Main(string[] args) { PrintInfo(); } static void PrintInfo(string name Tim,int age34) //默认值 { Console.WriteLine(Hello {0},you are {1}.,name,age); } }扩展方法this参数无法对一个类型的源码进行修改的时候可以使用扩展方法为目标数据类型来追加方法class Program { static void Main(string[] args) { double x 3.1415926; double y x.Round(4); //只有一个参数x就是this double input Console.WriteLine(y); } } static class DoubleExtension //约定俗成命名 { public static double Round(this double input,int digits) { double resultMath.Round(input,digits); return result; } }LINQ语言集成查询Lambda 运算符在 C# 中就是等号加大于号(i i 10);i代表集合A中的每一个元素参数。读作 goes to 或者 变成表示“对于参数i执行后面的判断”。i 10判断条件返回值。整体意思是判断集合A中是否存在Any满足条件大于 10的元素。class Program { static void Main(string[] args) { Listint myListnew Listint() {11,12,13,6,15,16}; bool result myList.All(i i 10); Console.WriteLine(result); } }总结

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑