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();
            }
  • 相关阅读:
    09.MyBatis的逆向工程
    08.MyBatis整合Log4j
    07.MyBatis中的关联查询
    06.动态SQL和foreach
    05.MyBtais两种取值符号以及输入参数和输出参数
    04.MyBatis别名的设置和类型转换器
    03.Mybatis优化
    02.Mybatis的动态代理方式实现增删改查
    01.MyBatis入门
    网络安全之网络安全(数通安全)0
  • 原文地址:https://www.cnblogs.com/liulun/p/1589706.html
Copyright © 2011-2022 走看看