-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
39 lines (33 loc) · 1.32 KB
/
Program.cs
File metadata and controls
39 lines (33 loc) · 1.32 KB
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
using System;
namespace DefaultLiteralExpressions
{
// Ref: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default#default-literal
class Program
{
static T[] InitializeArray<T>(int length, T initialValue = default)
{
if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length), "Array length must be nonnegative.");
}
var array = new T[length];
for (var i = 0; i < length; i++)
{
array[i] = initialValue;
}
return array;
}
static void Display<T>(T[] values) => Console.WriteLine($"[ {string.Join(", ", values)} ]");
static void Main(string[] args)
{
Func<string, bool> whereClause = default(Func<string, bool>);
Func<string, bool> whereClause2 = default;
Console.WriteLine(whereClause == null);
Console.WriteLine(whereClause2 == null);
Display(InitializeArray<int>(3)); // output: [ 0, 0, 0 ]
Display(InitializeArray<bool>(4, default)); // output: [ False, False, False, False ]
System.Numerics.Complex fillValue = default;
Display(InitializeArray(3, fillValue)); // output: [ (0, 0), (0, 0), (0, 0) ]
}
}
}