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();
            }
  • 相关阅读:
    【原创】颜色替换的递归算法
    【原创】Hacker学习发展流程图 V1.0
    【转载】基数排序
    【翻译】利用加速度求解位置的算法——三轴传感器
    js高级程序设计——笔记
    java中的多线程——进度1
    数据结构和算法——进度1
    java String字符串——进度1
    java中运算符——进度1
    jquery的插件机制
  • 原文地址:https://www.cnblogs.com/liulun/p/1589706.html
Copyright © 2011-2022 走看看