• 可选参数,程序暂停,属性的初始化,String.Format改进


    1.可选参数

    原来的写法:

    public static void Student(string Name,int age )
    {
             Console.WriteLine("我叫"+Name+",今年"+age+"岁。");
    }

    static void Main(string[] args)
    {
    Student("张三", 10);

    Console.ReadKey();
    }

    可选参数的写法:

    public static void Student(string Name="张三",int age=10 ) 注:如果在调用Student方法时不传递参数,那么Name的默认值是张三,age是10
    {
             Console.WriteLine("我叫"+Name+",今年"+age+"岁。");
    }

    static void Main(string[] args)
    {
             Student("张三", 10); 注:在调用方法时参数可以传递也可以不传递

             Console.ReadKey();
    }

    2.程序暂停

    要导入using System.Threading 命名空间

    Thread.Sleep(1000); 注:程序暂停的时间是以毫秒为单位

    3.属性的初始化

    原来的写法:

    public string Name { get; set; }
    public int Age { get; set; }
    public Program()
    {
    Name = "张三";
    Age = 10;
    }

    属性的初始化的写法:

    public string Name { get; set; } = "张三"; 注:在声明的同时初始化
    public int Age { get; set; } = 10; 注:在声明的同时初始化

    4.String.Format()

    原来的写法:

    string name = "张三";
    int age = 10;
    string s = string.Format("姓名是{0},年龄是{1}", name, age);
    Console.WriteLine(s);
    Console.ReadKey();

    改进后的写法:

    string name = "张三";
    int age = 10;
    string s = string.Format($"姓名是{name},年龄是{age}");
    Console.WriteLine(s);

  • 相关阅读:
    DES介绍
    jsp知识点
    浏览器地址传中文
    cookie
    null与“ ”的区别
    验证二叉查找树 · Validate Binary Search Tree
    二叉树中的最大路径和 · Binary Tree Maximum Path Sum
    最近公共祖先 · Lowest Common Ancestor
    平衡二叉树Balanced Binary Tree
    二叉树的最大/小/平衡 深度 depth of binary tree
  • 原文地址:https://www.cnblogs.com/zhang1999/p/7446599.html
Copyright © 2020-2023  润新知