-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_PrimaryConstructor.cs
63 lines (49 loc) · 1.31 KB
/
02_PrimaryConstructor.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
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
//
// All exmaples are taken from MS Roslyn Codeplex page
// https://www.codeplex.com/Download?ProjectName=roslyn&DownloadId=824694
//
namespace CSharp6Samples_PrimaryConstructor
{
#region CSharp 5
public class CustomerV5
{
private string firstName;
private string lastName;
public CustomerV5 (string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public string FirstName
{
get { return firstName; }
}
public string LastName
{
get { return lastName; }
}
}
#endregion
#region CSharp 6
public class CustomerV6 (string firstName, string lastName)
{
public string First { get; } = ArgumentUtility.CheckNotNull (firstName, "firstName");
public string Last { get; } = ArgumentUtility.CheckNotNull (lastName, "lastName");
// a private field birthday is created and initialized with the value of paramter birthday
public CustomerV6 () : this ("Jane", "Doe")
{
// all other ctors must call the primary one
}
}
public static class ArgumentUtility
{
public static T CheckNotNull<T> (T value, string parameterName)
{
if ( value== null)
throw new ArgumentNullException (parameterName);
return value;
}
}
#endregion
}