Home > 类库下载 > C# class library > C# optional parameters and named parameters

C# optional parameters and named parameters

高洛峰
Release: 2016-11-04 16:54:40
Original
2131 people have browsed it

Optional parameters

static void test1()
{
    func1("A");
    func1("B", 30);
    Console.ReadKey();
}
static void func1(string name, int age = 20)
{
    Console.WriteLine("Name: " + name + "\t Age: " + age);
}
Copy after login

Named parameters

static void test2()
{
    func2("C", 40);
    func2(age: 50, name: "D");
    Console.ReadKey();
}
static void func2(string name, int age)
{
    Console.WriteLine("Name: " + name + "\t Age: " + age);
}
Copy after login

Optional parameters combined with named parameters

static void test3()
{
    func3("E");
    func3("F", 10, false);
    func3("G", 15);
    func3("H", isMale: false);
    Console.ReadKey();
}
static void func3(string name, int age = 20, bool isMale = true)
{
    Console.WriteLine("Name: " + name + "\t Age: " + age + "\t IsMale: " + isMale);
}
Copy after login


Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template