Skip to content

Commit

Permalink
Added primary constructor example (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMichaelis authored Sep 15, 2023
1 parent 03a877d commit f8520e4
Show file tree
Hide file tree
Showing 51 changed files with 118 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_26.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_29.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -12,7 +12,7 @@ public async Task UnassignedVariableThrowsError()
await CompilerAssert.CompileAsync(
new string[] {
CompilerAssert.GetTargetFileNameToCompileFromTestFileName(),
"Listing06.26.DefiningAConstructor.cs" },
"Listing06.28.DefiningAConstructor.cs" },
new string[] { "CS7036" });
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_29.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_30.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_32.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_33.Tests;

[TestClass]
public class EmployeeTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_33.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34.Tests;

[TestClass]
public class EmployeeTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35.Tests;

[TestClass]
public class EmployeeTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_36.Tests;

[TestClass]
public class EmployeeTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_38.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_39.Tests;

#if NET7_0_OR_GREATER
[TestClass]
Expand All @@ -13,8 +13,8 @@ public async Task NotSpecifyingRequiredMembersWithinTheObjectInitializer()
await CompilerAssert.CompileAsync(
new string[]{
CompilerAssert.GetTargetFileNameToCompileFromTestFileName(),
"Listing06.37.RequiredProperties.cs"},
"Listing06.38.RequiredProperties.cs"},
new string[] { "CS9035" });
}
}
#endif // NET7_0_OR_GREATER
#endif // NET7_0_OR_GREATER
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_40.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_41.Tests;

[TestClass]
public class NullabilityAttributesExaminedTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_41.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_42.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_42.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_43.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_43.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_44.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_46.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_47.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_47.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_48.Tests;

[TestClass]
public class DirectoryTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_50.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_51.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_54.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_55.Tests;

[TestClass]
public class DirectoryTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_55.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_56.Tests;

[TestClass]
public class ProgramTests
Expand Down
60 changes: 60 additions & 0 deletions src/Chapter06/Listing06.26.DefiningAPrimaryConstructor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_26;

#if NET8_0_OR_GREATER
public class Program
{
public static void Main()
{
Employee employee = new("Inigo", "Montoya");
employee.Salary = "Too Little";

System.Console.WriteLine(
$"{employee.FirstName} {employee.LastName}: {employee.Salary}");
}
}

#region INCLUDE
#region HIGHLIGHT
// Employee constructor
public class Employee(string firstName, string lastName)
{
public string FirstName { get; set; } = firstName;
public string LastName { get; set; } = lastName;
#endregion HIGHLIGHT
public string? Salary { get; set; } = "Not Enough";

#region EXCLUDE
public string? Title { get; set; }
public Employee? Manager { get; set; }

// Name property
public string FullName
{
get
{
return FirstName + " " + LastName;
}
set
{
// Split the assigned value into
// first and last names
string[] names = value.Split(' ');
if(names.Length == 2)
{
FirstName = names[0];
LastName = names[1];
}
else
{
// Throw an exception if the full
// name was not assigned
throw new System.ArgumentException(
$"Assigned value '{ value }' is invalid",
nameof(value));
}
}
}
#endregion EXCLUDE
}
#endregion INCLUDE
#endif // NET8_0_OR_GREATER
2 changes: 1 addition & 1 deletion src/Chapter06/Listing06.27.CallingAConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_27;

using Listing06_26;
using Listing06_28;

#region INCLUDE
public class Program
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Disabled pending introductin to object initializers
#pragma warning disable IDE0017

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_26;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Variable is declared but never used
#pragma warning disable CS0168

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_29;

using Listing06_26;
using Listing06_28;

#region INCLUDE
public class Program
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_29;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_30;

#region INCLUDE
public class Program
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_30;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_31;

using System.Collections.Generic;
using Listing06_26;
using Listing06_28;

#region INCLUDE
public class Program
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Non-nullable field is uninitialized. Consider declaring as nullable.
#pragma warning disable CS8618 // Pending a constructors

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_31;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_32;

#region INCLUDE
public class Employee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#pragma warning disable IDE0044
#pragma warning disable 649 // _Id is never assigned

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_32;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_33;

#region INCLUDE
public class Employee
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_33;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34;

#region INCLUDE
public class Employee
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_36;

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_36;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_37;

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if NET7_0_OR_GREATER

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_37;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_38;

#region INCLUDE
public class Book
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#if NET7_0_OR_GREATER

#if COMPILEERROR // EXCLUDE
using AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_37;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_38;
#endif // COMPILEERROR // EXCLUDE

using System.Runtime.CompilerServices;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_38;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_39;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if NET7_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_39;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_40;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_40;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_41;

#region INCLUDE
using System.Diagnostics.CodeAnalysis;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_41;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_42;

using System;
using System.Diagnostics.CodeAnalysis;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_42;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_43;

#region INCLUDE
public class Employee
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_42;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_43;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_43;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_44;

#region INCLUDE
public class Program
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_44;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_45;

#region INCLUDE
public class Employee
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_45;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_46;

#region INCLUDE
public class Employee
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_46;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_47;

#region INCLUDE
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_47;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_48;

using System;
using System.IO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_48;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_49;

using System;

Expand Down
Loading

0 comments on commit f8520e4

Please sign in to comment.