-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
50 lines (40 loc) · 1.21 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
namespace NullCoalescing
{
/// <summary>
/// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
/// </summary>
class Program
{
static void Main(string[] args)
{
string? name = null;
name = name ?? "Hello";
name ??= "Hello"; // C# 8.0
MessageWriter(null, "Recover Message");
MessageWriter("Hello", "Recover Message");
var city = new City
{
Name = null
};
Console.WriteLine(city);
//CharacterCount(null);
Console.WriteLine(CharacterCount("Hello"));
}
static void MessageWriter<T>(T message, T recoverMessage)
{
Console.WriteLine(message ?? recoverMessage);
}
static int CharacterCount(string name)
{
_ = name ?? throw new ArgumentException(nameof(name));
return name.Length;
}
}
class City
{
public string Name { get; set; }
public override string ToString() => this.Name;
//public override string ToString() => this.Name ?? string.Empty;
}
}