zoukankan      html  css  js  c++  java
  • C#4.0新特性 可选命名参数

    Code
        class Program
        {
            
    static void PrintStudents(int id = -1string name = "*"int age = -1)
            {
                Console.WriteLine(
    "the student is id:{0} name:{1} age:{2}", id, name, age);
            }
            
    static void Main(string[] args)
            {
                PrintStudents(name: 
    "xland", id: 8);
                Console.ReadKey();
            }
        }

    如果有方法的多态的话,匹配方式如下

    以下函数输出6;也就是执行了第一个方法

    Code
            static void test3(int x)
            {
                Console.WriteLine(x);
            }
            
    static void test3(int x, int y = 6)
            {
                Console.WriteLine(x);
                Console.WriteLine(y);
            }
            
    static void Main(string[] args)
            {
                test3(
    6);
                Console.ReadKey();
            }

    如果有方法的重载的话

    如下,输出 8 6 6 

    这里与一般的重载有区别  需要注意

    Code
            public class test4
            {
                
    public virtual void test5(int a = 6)
                {
                    Console.WriteLine(a);
                }
            }
            
    public class test6 : test4
            {
                
    public override void test5(int a = 8)
                {
                    Console.WriteLine(a);
                }
            }
            
    static void Main(string[] args)
            {
                test6 a 
    = new test6();
                a.test5();
                test4 b 
    = new test6();
                b.test5();
                test4 c 
    = a as test4;
                c.test5();
                Console.ReadKey();
            }

    给方法传递参数的值的时候,可以直接从另一个方法获取值

    如下

    Code
            static void test7(int c,int b)
            {
                Console.WriteLine(b);
            }
            
    static int test8()
            {
                
    return 8;
            }
            
    static void Main(string[] args)
            {
                test7(
    1,9);
                test7(
    1,b:test8());
                Console.ReadKey();
            }
  • 相关阅读:
    如何高效的学习技术
    面试连环炮系列(二十三): StringBuffer与StringBuild的区别
    面试连环炮系列(二十二):常用的设计模式有哪些
    算法天天练709:字符串转小写
    面试连环炮系列(二十一):你们的项目怎么使用kafka
    算法天天练771:查找字符串出现的次数
    初次进入职场如何工作与学习
    面试连环炮系列(二十):TCP的滑动窗口协议是什么
    算法天天练334:字符串翻转
    面试连环炮系列(十九):分布式锁的实现方案
  • 原文地址:https://www.cnblogs.com/liulun/p/1589706.html
Copyright © 2011-2022 走看看