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();
            }
  • 相关阅读:
    Alpha、Beta、RC、GA版本的区别
    convertView
    Layouts
    Styles and Themes
    Android Studio
    Promise.race
    Base64
    NodeJS搭建HTTPS服务器
    CoreSeek
    Ping of Death
  • 原文地址:https://www.cnblogs.com/liulun/p/1589706.html
Copyright © 2011-2022 走看看