参数进阶、扩展方法

在这里插入图片描述

传值参数

在这里插入图片描述
在这里插入图片描述

值类型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nMPF33GO-1675952809492)(assets/image-20220623141557-rmomxla.png)]

(https://assets.b3logfile.com/siyuan/1628875479353/assets/image-20220623140506-zre6cqg.png)(ssets/image-20220623141557-rmomxla.png)]

在这里插入图片描述

方法内传进来的是副本

stu.AddOne(y);​打出来是101,Console.WriteLine(y);​打出来是100。

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student() { Name = "Tim" };
            SomeMethod(stu);
            Console.WriteLine(stu.Name);
            Console.ReadKey();
        }

        static void SomeMethod(Student stu)
        {
            stu = new Student { Name = "Tom" };
            Console.WriteLine(stu.Name);
        }
    }
 
    class Student
    {
        public string Name { get; set; }


    }



}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-40ryHvKw-1675952745272)(https://assets.b3logfile.com/siyuan/1628875479353/assets/image-20220623140331-8h81pbn.png)]

在这里插入图片描述

只更新对象,不创建新对象

在这里插入图片描述

引用参数

在这里插入图片描述

在这里插入图片描述

值类型引用参数

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            int y = 1;
            IWantSideEffect(ref y);
            Console.WriteLine(y);

        }

        static void IWantSideEffect(ref int x)
        {
            x = x + 100; 
        }
        } 
    }

打印结果为101

引用类型引用参数

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            Student outterSu = new Student() { Name = "Tim" };
            Console.WriteLine("HashCode={0},Name={1}",outterSu.GetHashCode(),outterSu.Name);
            Console.WriteLine("-----------------");
            IWantSideEffect(ref outterSu);//方法体内
            Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);//方法体外
            Console.ReadKey();
        }

        static void IWantSideEffect(ref Student stu)
        {
            stu = new Student() { Name="Tom"};
            Console.WriteLine("HashCod={0},Name={1}",stu.GetHashCode(),stu.Name);
        }
        }
    class Student
    {
        public string Name { get; set; }
    }
    }

打印结果为:

HashCode=46104728,Name=Tim
-----------------
HashCod=12289376,Name=Tom
HashCode=12289376,Name=Tom

引用类型,不创建对象只改变对象值

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            Student outterSu = new Student() { Name = "Tim" };
            Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);
            Console.WriteLine("-----------------");
            IWantSideEffect(ref outterSu);//方法体内
            Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);//方法体外
            Console.ReadKey();
        }

        static void IWantSideEffect(ref Student stu)
        {
            stu.Name = "Tom";
            Console.WriteLine("HashCod={0},Name={1}", stu.GetHashCode(), stu.Name);
        }
    }
    class Student
    {
        public string Name { get; set; }
    }
}

打印结果为:

HashCode=46104728,Name=Tim
-----------------
HashCod=46104728,Name=Tom
HashCode=46104728,Name=Tom

虽然结果不应,但是HashCod 说明一直在操作同一个对象,

如果把ref关键字去掉,变为

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            Student outterSu = new Student() { Name = "Tim" };
            Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);
            Console.WriteLine("-----------------");
            IWantSideEffect(outterSu);//方法体内
            Console.WriteLine("HashCode={0},Name={1}", outterSu.GetHashCode(), outterSu.Name);//方法体外
            Console.ReadKey();
        }

        static void IWantSideEffect(tudent stu)
        {
            stu.Name = "Tom";
            Console.WriteLine("HashCod={0},Name={1}", stu.GetHashCode(), stu.Name);
        }
    }
    class Student
    {
        public string Name { get; set; }
    }
}


打印结果与加了ref​一样,但是内存机理不一样,这种传值函数创建了副本,这种方式static void IWantSideEffect(tudent stu)​中的stu​和Student outterSu = new Student() { Name = "Tim" }​中的outterSu​的地址不一样,但是这两个变量存储了同一个实例在堆上的地址。

而加上ref以后,stu ​outterSu的地址是一样的,并且也是存储的对象在堆内存上的地址。

输出参数

值类型输出参数

在这里插入图片描述

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please input the 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 input the 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);
            Console.ReadKey();
        }
    }
     
}

案例2:

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            double x = 0;
            bool b = DoubleParser.TryParse("7920",out x);
            if (b == true)
            {
                Console.WriteLine(x+1);          
            }
            Console.ReadKey();
        }
    }
    class DoubleParser
    {
        public static bool TryParse(string input, out double result)
        {
            try
            {
                result = double.Parse(input);
                return true;
            }
            catch 
            {
                result = 0;
                return false;
            }
        }
    }
     
}

引用类型输出参数

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            Student stu = null;
            bool b = StudentFactory.Create("Tim",50,out stu);
            if (b==true)
            {
                Console.WriteLine("Student{0},age is{1}",stu.Name,stu.Age);
            }
            Console.ReadKey();
        }
    }
    class Student
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }
    class StudentFactory
    {
        public static bool Create(string stuName, int stuAge,out Student result)
        {
            result = null;
            if (string.IsNullOrEmpty(stuName))
            {
                return false;
            }
            if (stuAge<20||stuAge>80)
            {
                return false;
            }
  47==          result = new Student() { Name = stuName, Age=stuAge};
            return true;
        }
    }
   
}

数组参数

在这里插入图片描述

案例一:

在这里插入图片描述

案例二:

在这里插入图片描述

两个案例的结果是一样的,只不过,第二个用params修饰后,可以不声明数组,直接在方法内输入数组。

在这里插入图片描述

WriteLine里面的重载就有params

分割参数

在这里插入图片描述

具名参数

不具名调用

在这里插入图片描述

方法的形参是什么类型,什么顺序,那么调用时的实参要一一对应。

具名参数

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            PrintInfo(name: "Tim", age: 40);

            Console.ReadKey();
        }
        static void PrintInfo (string name,int age )
    {
            Console.WriteLine("Hello{0},you are {1}",name,age);
}

}

     
}

具名参数优点:

  • 提高代码可读性,可以一眼看出参数是什么
  • 参数顺序不受方法形参顺序约束

具名参数应该属于参数的使用方法,不是种类。

可选参数

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            PrintInfo();

            Console.ReadKey();
        }
        static void PrintInfo (string name="Tim",int age=20 )
    {
            Console.WriteLine("Hello{0},you are {1}",name,age);
}

}

     
}

打印结果为

HelloTim,you are 20

扩展方法

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            var x = 3.14159;
            var y = Math.Round(x, 4);//精确小数位数
            Console.WriteLine(y);
            Console.ReadKey();
        }


    }


}

打印结果改为:3.1416

double类型是没有Round方法的,只能调用Math,还可以使用扩展方法,扩展方法必须放在静态类里面。

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            var x = 3.14159;
            var y =x.Round(4);//注意者接收了一个参数,第一个参数已经在.操作符前传入
            Console.WriteLine(y);
            Console.ReadKey();
        }


    }
    static class DoubleExtention
    {
        public static double Round(this double input, int digits)//注意this关键字
        {
            double result = Math.Round(input, digits);
            return result;
  
        }
    }

}

linq语句也可以用扩展方法

linq要引用using System.Linq;

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
using System.Linq;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            List<int> OneList = new List<int>() {14,15,16};
            bool result = Test(OneList);
            Console.WriteLine(result);
            Console.ReadKey();

        }
        static bool Test(List<int>intList)

        {
            foreach (var item in intList)
            {
                if (item<=10)
                {
                    return false;
                }
            }
            return true;
        }


    }

    }

注意13行的变化

using System;
using System.Collections.Generic;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections;
using System.Linq;
//using System.Windows.Forms;
namespace NamespaceEncasement

{

    class Program
    {
        static void Main(string[] args)
        {
            List<int> OneList = new List<int>() { 14,15,16};
            bool result = OneList.All(x=>x>3);
            Console.WriteLine(result);
            Console.ReadKey();

        }
        static bool Test(List<int>intList)

        {
            foreach (var item in intList)
            {
                if (item<=10)
                {
                    return false;
                }
            }
            return true;
        }


    }

    }

第十三行的All()就是扩展方法,是存在IEnumerable中的一个扩展方法

各种参数 总结

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值