diff --git a/vs4win/Chapter05/Chapter05.sln b/vs4win/Chapter05/Chapter05.sln index 68d6e4b..410e108 100644 --- a/vs4win/Chapter05/Chapter05.sln +++ b/vs4win/Chapter05/Chapter05.sln @@ -3,22 +3,20 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.2.32210.308 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PacktLibrary", "PacktLibrary\PacktLibrary.csproj", "{9E796C3B-6FF6-4E0A-8B6D-A591B6FD1308}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PeopleApp", "PeopleApp\PeopleApp.csproj", "{DF7885A2-C9F1-4959-9D13-C72E5E77CCDA}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Namespaces", "Namespaces\Namespaces.csproj", "{AA8F156C-1105-4AB2-8C42-637990E44EB4}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PacktLibraryNetStandard2", "PacktLibraryNetStandard2\PacktLibraryNetStandard2.csproj", "{8E856A85-0B5E-4374-A35A-B53B94834DBA}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PacktLibraryModern", "PacktLibraryModern\PacktLibraryModern.csproj", "{1981F355-34EE-4934-ABA1-CA6119ED5D99}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {9E796C3B-6FF6-4E0A-8B6D-A591B6FD1308}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9E796C3B-6FF6-4E0A-8B6D-A591B6FD1308}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9E796C3B-6FF6-4E0A-8B6D-A591B6FD1308}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9E796C3B-6FF6-4E0A-8B6D-A591B6FD1308}.Release|Any CPU.Build.0 = Release|Any CPU {DF7885A2-C9F1-4959-9D13-C72E5E77CCDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {DF7885A2-C9F1-4959-9D13-C72E5E77CCDA}.Debug|Any CPU.Build.0 = Debug|Any CPU {DF7885A2-C9F1-4959-9D13-C72E5E77CCDA}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -27,6 +25,14 @@ Global {AA8F156C-1105-4AB2-8C42-637990E44EB4}.Debug|Any CPU.Build.0 = Debug|Any CPU {AA8F156C-1105-4AB2-8C42-637990E44EB4}.Release|Any CPU.ActiveCfg = Release|Any CPU {AA8F156C-1105-4AB2-8C42-637990E44EB4}.Release|Any CPU.Build.0 = Release|Any CPU + {8E856A85-0B5E-4374-A35A-B53B94834DBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E856A85-0B5E-4374-A35A-B53B94834DBA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E856A85-0B5E-4374-A35A-B53B94834DBA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E856A85-0B5E-4374-A35A-B53B94834DBA}.Release|Any CPU.Build.0 = Release|Any CPU + {1981F355-34EE-4934-ABA1-CA6119ED5D99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1981F355-34EE-4934-ABA1-CA6119ED5D99}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1981F355-34EE-4934-ABA1-CA6119ED5D99}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1981F355-34EE-4934-ABA1-CA6119ED5D99}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/vs4win/Chapter05/PacktLibrary/Book.cs b/vs4win/Chapter05/PacktLibrary/Book.cs deleted file mode 100644 index b212a2a..0000000 --- a/vs4win/Chapter05/PacktLibrary/Book.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Packt.Shared; - -public class Book -{ - // required is not working in .NET 7 Preview 1 - public /* required */ string? Isbn { get; set; } - public string? Title { get; set; } -} diff --git a/vs4win/Chapter05/PacktLibraryModern/Book.cs b/vs4win/Chapter05/PacktLibraryModern/Book.cs new file mode 100644 index 0000000..cfbfa51 --- /dev/null +++ b/vs4win/Chapter05/PacktLibraryModern/Book.cs @@ -0,0 +1,22 @@ +using System.Diagnostics.CodeAnalysis; // [SetsRequiredMembers] + +namespace Packt.Shared; + +public class Book +{ + public Book() { } // For use with initialization syntax. + + [SetsRequiredMembers] + public Book(string isbn, string title) + { + Isbn = isbn; + Title = title; + } + + // Needs .NET 7 or later as well as C# 11 or later. + public required string? Isbn { get; set; } + + public required string? Title { get; set; } + public string? Author { get; set; } + public int PageCount { get; set; } +} diff --git a/vs4win/Chapter05/PacktLibraryModern/PacktLibraryModern.csproj b/vs4win/Chapter05/PacktLibraryModern/PacktLibraryModern.csproj new file mode 100644 index 0000000..4c18759 --- /dev/null +++ b/vs4win/Chapter05/PacktLibraryModern/PacktLibraryModern.csproj @@ -0,0 +1,10 @@ + + + + net7.0 + enable + enable + preview + + + diff --git a/vs4win/Chapter05/PacktLibrary/BankAccount.cs b/vs4win/Chapter05/PacktLibraryNetStandard2/BankAccount.cs similarity index 100% rename from vs4win/Chapter05/PacktLibrary/BankAccount.cs rename to vs4win/Chapter05/PacktLibraryNetStandard2/BankAccount.cs diff --git a/vs4win/Chapter05/PacktLibrary/FlightPatterns.cs b/vs4win/Chapter05/PacktLibraryNetStandard2/FlightPatterns.cs similarity index 100% rename from vs4win/Chapter05/PacktLibrary/FlightPatterns.cs rename to vs4win/Chapter05/PacktLibraryNetStandard2/FlightPatterns.cs diff --git a/vs4win/Chapter05/PacktLibrary/PacktLibrary.csproj b/vs4win/Chapter05/PacktLibraryNetStandard2/PacktLibraryNetStandard2.csproj similarity index 69% rename from vs4win/Chapter05/PacktLibrary/PacktLibrary.csproj rename to vs4win/Chapter05/PacktLibraryNetStandard2/PacktLibraryNetStandard2.csproj index 95bce73..a2ca62b 100644 --- a/vs4win/Chapter05/PacktLibrary/PacktLibrary.csproj +++ b/vs4win/Chapter05/PacktLibraryNetStandard2/PacktLibraryNetStandard2.csproj @@ -2,10 +2,10 @@ netstandard2.0 - 10 - enable + preview + enable enable - + diff --git a/vs4win/Chapter05/PacktLibrary/Person.cs b/vs4win/Chapter05/PacktLibraryNetStandard2/Person.cs similarity index 100% rename from vs4win/Chapter05/PacktLibrary/Person.cs rename to vs4win/Chapter05/PacktLibraryNetStandard2/Person.cs diff --git a/vs4win/Chapter05/PacktLibrary/PersonAutoGen.cs b/vs4win/Chapter05/PacktLibraryNetStandard2/PersonAutoGen.cs similarity index 100% rename from vs4win/Chapter05/PacktLibrary/PersonAutoGen.cs rename to vs4win/Chapter05/PacktLibraryNetStandard2/PersonAutoGen.cs diff --git a/vs4win/Chapter05/PacktLibrary/Records.cs b/vs4win/Chapter05/PacktLibraryNetStandard2/Records.cs similarity index 100% rename from vs4win/Chapter05/PacktLibrary/Records.cs rename to vs4win/Chapter05/PacktLibraryNetStandard2/Records.cs diff --git a/vs4win/Chapter05/PacktLibrary/WondersOfTheAncientWorld.cs b/vs4win/Chapter05/PacktLibraryNetStandard2/WondersOfTheAncientWorld.cs similarity index 100% rename from vs4win/Chapter05/PacktLibrary/WondersOfTheAncientWorld.cs rename to vs4win/Chapter05/PacktLibraryNetStandard2/WondersOfTheAncientWorld.cs diff --git a/vs4win/Chapter05/PeopleApp/PeopleApp.csproj b/vs4win/Chapter05/PeopleApp/PeopleApp.csproj index 968b8d8..10f4b3f 100644 --- a/vs4win/Chapter05/PeopleApp/PeopleApp.csproj +++ b/vs4win/Chapter05/PeopleApp/PeopleApp.csproj @@ -8,7 +8,8 @@ - + + diff --git a/vs4win/Chapter05/PeopleApp/Program.cs b/vs4win/Chapter05/PeopleApp/Program.cs index cc928e0..8e9c6e2 100644 --- a/vs4win/Chapter05/PeopleApp/Program.cs +++ b/vs4win/Chapter05/PeopleApp/Program.cs @@ -161,8 +161,21 @@ WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); -Book book = new(); -book.Title = "C# 11 and .NET 7 - Modern Cross-Platform Development"; +/* +Book book = new() +{ + Isbn = "978-1803237800", + Title = "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals" +}; +*/ +Book book = new(isbn: "978-1803237800", + title: "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals"); + +book.Author = "Mark J. Price"; +book.PageCount = 821; + +WriteLine("{0}: {1} written by {2} has {3:N0} pages.", + book.Isbn, book.Title, book.Author, book.PageCount); sam.Children.Add(new() { Name = "Charlie", DateOfBirth = new(2010, 3, 18) }); sam.Children.Add(new() { Name = "Ella", DateOfBirth = new(2020, 12, 24) }); @@ -235,7 +248,7 @@ Passenger[] passengers = { new CoachClassPassenger { CarryOnKG = 0, Name = "Amit" }, }; -foreach (object passenger in passengers) +foreach (Passenger passenger in passengers) { decimal flightCost = passenger switch { diff --git a/vscode/Chapter05/Chapter05.code-workspace b/vscode/Chapter05/Chapter05.code-workspace index 05d45eb..c3c63f0 100644 --- a/vscode/Chapter05/Chapter05.code-workspace +++ b/vscode/Chapter05/Chapter05.code-workspace @@ -1,13 +1,16 @@ { "folders": [ { - "path": "PacktLibrary" + "path": "PacktLibraryNetStandard2" }, { - "path": "PeopleApp" + "path": "PacktLibraryModern" }, { "path": "Namespaces" + }, + { + "path": "PeopleApp" } ] } \ No newline at end of file diff --git a/vscode/Chapter05/PacktLibrary/Book.cs b/vscode/Chapter05/PacktLibrary/Book.cs deleted file mode 100644 index b212a2a..0000000 --- a/vscode/Chapter05/PacktLibrary/Book.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Packt.Shared; - -public class Book -{ - // required is not working in .NET 7 Preview 1 - public /* required */ string? Isbn { get; set; } - public string? Title { get; set; } -} diff --git a/vscode/Chapter05/PacktLibraryModern/Book.cs b/vscode/Chapter05/PacktLibraryModern/Book.cs new file mode 100644 index 0000000..cfbfa51 --- /dev/null +++ b/vscode/Chapter05/PacktLibraryModern/Book.cs @@ -0,0 +1,22 @@ +using System.Diagnostics.CodeAnalysis; // [SetsRequiredMembers] + +namespace Packt.Shared; + +public class Book +{ + public Book() { } // For use with initialization syntax. + + [SetsRequiredMembers] + public Book(string isbn, string title) + { + Isbn = isbn; + Title = title; + } + + // Needs .NET 7 or later as well as C# 11 or later. + public required string? Isbn { get; set; } + + public required string? Title { get; set; } + public string? Author { get; set; } + public int PageCount { get; set; } +} diff --git a/vscode/Chapter05/PacktLibraryModern/PacktLibraryModern.csproj b/vscode/Chapter05/PacktLibraryModern/PacktLibraryModern.csproj new file mode 100644 index 0000000..4c18759 --- /dev/null +++ b/vscode/Chapter05/PacktLibraryModern/PacktLibraryModern.csproj @@ -0,0 +1,10 @@ + + + + net7.0 + enable + enable + preview + + + diff --git a/vscode/Chapter05/PacktLibrary/BankAccount.cs b/vscode/Chapter05/PacktLibraryNetStandard2/BankAccount.cs similarity index 100% rename from vscode/Chapter05/PacktLibrary/BankAccount.cs rename to vscode/Chapter05/PacktLibraryNetStandard2/BankAccount.cs diff --git a/vscode/Chapter05/PacktLibrary/FlightPatterns.cs b/vscode/Chapter05/PacktLibraryNetStandard2/FlightPatterns.cs similarity index 100% rename from vscode/Chapter05/PacktLibrary/FlightPatterns.cs rename to vscode/Chapter05/PacktLibraryNetStandard2/FlightPatterns.cs diff --git a/vscode/Chapter05/PacktLibrary/PacktLibrary.csproj b/vscode/Chapter05/PacktLibraryNetStandard2/PacktLibraryNetStandard2.csproj similarity index 69% rename from vscode/Chapter05/PacktLibrary/PacktLibrary.csproj rename to vscode/Chapter05/PacktLibraryNetStandard2/PacktLibraryNetStandard2.csproj index 95bce73..a2ca62b 100644 --- a/vscode/Chapter05/PacktLibrary/PacktLibrary.csproj +++ b/vscode/Chapter05/PacktLibraryNetStandard2/PacktLibraryNetStandard2.csproj @@ -2,10 +2,10 @@ netstandard2.0 - 10 - enable + preview + enable enable - + diff --git a/vscode/Chapter05/PacktLibrary/Person.cs b/vscode/Chapter05/PacktLibraryNetStandard2/Person.cs similarity index 100% rename from vscode/Chapter05/PacktLibrary/Person.cs rename to vscode/Chapter05/PacktLibraryNetStandard2/Person.cs diff --git a/vscode/Chapter05/PacktLibrary/PersonAutoGen.cs b/vscode/Chapter05/PacktLibraryNetStandard2/PersonAutoGen.cs similarity index 100% rename from vscode/Chapter05/PacktLibrary/PersonAutoGen.cs rename to vscode/Chapter05/PacktLibraryNetStandard2/PersonAutoGen.cs diff --git a/vscode/Chapter05/PacktLibrary/Records.cs b/vscode/Chapter05/PacktLibraryNetStandard2/Records.cs similarity index 100% rename from vscode/Chapter05/PacktLibrary/Records.cs rename to vscode/Chapter05/PacktLibraryNetStandard2/Records.cs diff --git a/vscode/Chapter05/PacktLibrary/WondersOfTheAncientWorld.cs b/vscode/Chapter05/PacktLibraryNetStandard2/WondersOfTheAncientWorld.cs similarity index 100% rename from vscode/Chapter05/PacktLibrary/WondersOfTheAncientWorld.cs rename to vscode/Chapter05/PacktLibraryNetStandard2/WondersOfTheAncientWorld.cs diff --git a/vscode/Chapter05/PeopleApp/PeopleApp.csproj b/vscode/Chapter05/PeopleApp/PeopleApp.csproj index 968b8d8..10f4b3f 100644 --- a/vscode/Chapter05/PeopleApp/PeopleApp.csproj +++ b/vscode/Chapter05/PeopleApp/PeopleApp.csproj @@ -8,7 +8,8 @@ - + + diff --git a/vscode/Chapter05/PeopleApp/Program.cs b/vscode/Chapter05/PeopleApp/Program.cs index cc928e0..8e9c6e2 100644 --- a/vscode/Chapter05/PeopleApp/Program.cs +++ b/vscode/Chapter05/PeopleApp/Program.cs @@ -161,8 +161,21 @@ WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); -Book book = new(); -book.Title = "C# 11 and .NET 7 - Modern Cross-Platform Development"; +/* +Book book = new() +{ + Isbn = "978-1803237800", + Title = "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals" +}; +*/ +Book book = new(isbn: "978-1803237800", + title: "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals"); + +book.Author = "Mark J. Price"; +book.PageCount = 821; + +WriteLine("{0}: {1} written by {2} has {3:N0} pages.", + book.Isbn, book.Title, book.Author, book.PageCount); sam.Children.Add(new() { Name = "Charlie", DateOfBirth = new(2010, 3, 18) }); sam.Children.Add(new() { Name = "Ella", DateOfBirth = new(2020, 12, 24) }); @@ -235,7 +248,7 @@ Passenger[] passengers = { new CoachClassPassenger { CarryOnKG = 0, Name = "Amit" }, }; -foreach (object passenger in passengers) +foreach (Passenger passenger in passengers) { decimal flightCost = passenger switch {