输出参数-引用参数-具名参数-扩展方法

引用参数

修改一个参数名的时候 如何快速修改其他参数名:
“CTRL + 。”
如果给方法参数传引用之后,方法体内部有new关键字,表示重新创造了一个对象

Object.GetHashCode()方法可以获得每个对象的hashcoude,都不一样

ref 引用参数修饰符 -这里还是有一些内容的,不过根据需求来吧
out 输出参数修饰符

引用类型的输出参数例子

 class Program
    {
        static void Main(string[] args)
        {
            //声明一个学生实例
            Student stu = null;
            //工厂类的静态方法,返回bool值,输出参数为学生实例
            bool b = StudentFactory.CreateStu("tom", 34, out stu);
            if (b==true)
            {
                Console.WriteLine("student {0},age is {1}",stu.MyNmae,stu.MyAge);
            }

        }

    }

    class Student
    {
        public string MyNmae { get; set; }
        public int MyAge { get; set; }

    }

    class StudentFactory
    {
        public static bool CreateStu(string name, int age, out Student stu)
        {
            stu = null;
            if (name == null)
            {
                return false;
            }
            if (age<20 || age>80)
            {
                return false;
            }
            stu = new Student() { MyAge = age, MyNmae = name };
            return true;
        }
    }

数组参数 params 修饰符

具名参数

    class Program
    {
        static void Main(string[] args)
        {
            Some(Name:"tom",ID:12);
        }
        public static void Some(string Name,int ID)
        {
            Console.WriteLine("{0},{1}",Name,ID);
        }
    }

扩展方法(this参数)
方法必须是共有的、静态的,即被public static 所修饰
必须是形参列表中的第一个,由this修饰
必须由一个静态类(一般类名为SomeTypeExtension)来统一收纳对SomeType类型的扩展方法
举例 LINQ

    class Program
    {
        static void Main(string[] args)
        {
            double x = 3.141592;
            double y = x.Round(4);
            Console.WriteLine(y);
       
        }
    }
    static class DoubleExtension
    {
        public static double Round(this double input,int digits)
        {
            double result = Math.Round(input, digits);
            return result;
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值