Skip to content

Commit d559162

Browse files
committed
Add code solutions for Ch2, 7, 8
1 parent 499970e commit d559162

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1719
-0
lines changed

code/Chapter02/Chapter02.sln

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34414.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RefactoringDemos", "RefactoringDemos\RefactoringDemos.csproj", "{B8DC33F7-90A5-4525-9BEF-FEED0EA1C101}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeSnippetDemos", "CodeSnippetDemos\CodeSnippetDemos.csproj", "{9C47FCF9-9BB7-4982-8CC4-2CD3DA428DBB}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsolePlusTemplate", "ConsolePlusTemplate\ConsolePlusTemplate.csproj", "{17697E6C-DA68-4C9D-85F6-F2B197E3613F}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsolePlusProject", "ConsolePlusTemplate\content\ConsolePlusProject\ConsolePlusProject.csproj", "{883FE9A9-9C18-4AAD-9C0A-F1D995B870D6}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnippetDemos4Code", "SnippetDemos4Code\SnippetDemos4Code.csproj", "{327E331E-2832-4BD5-9840-9AD31479E727}"
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Any CPU = Debug|Any CPU
19+
Release|Any CPU = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{B8DC33F7-90A5-4525-9BEF-FEED0EA1C101}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{B8DC33F7-90A5-4525-9BEF-FEED0EA1C101}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{B8DC33F7-90A5-4525-9BEF-FEED0EA1C101}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{B8DC33F7-90A5-4525-9BEF-FEED0EA1C101}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{9C47FCF9-9BB7-4982-8CC4-2CD3DA428DBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{9C47FCF9-9BB7-4982-8CC4-2CD3DA428DBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{9C47FCF9-9BB7-4982-8CC4-2CD3DA428DBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{9C47FCF9-9BB7-4982-8CC4-2CD3DA428DBB}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{17697E6C-DA68-4C9D-85F6-F2B197E3613F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{17697E6C-DA68-4C9D-85F6-F2B197E3613F}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{17697E6C-DA68-4C9D-85F6-F2B197E3613F}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{17697E6C-DA68-4C9D-85F6-F2B197E3613F}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{883FE9A9-9C18-4AAD-9C0A-F1D995B870D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{883FE9A9-9C18-4AAD-9C0A-F1D995B870D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{883FE9A9-9C18-4AAD-9C0A-F1D995B870D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{883FE9A9-9C18-4AAD-9C0A-F1D995B870D6}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{327E331E-2832-4BD5-9840-9AD31479E727}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{327E331E-2832-4BD5-9840-9AD31479E727}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{327E331E-2832-4BD5-9840-9AD31479E727}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{327E331E-2832-4BD5-9840-9AD31479E727}.Release|Any CPU.Build.0 = Release|Any CPU
42+
EndGlobalSection
43+
GlobalSection(SolutionProperties) = preSolution
44+
HideSolutionNode = FALSE
45+
EndGlobalSection
46+
GlobalSection(ExtensibilityGlobals) = postSolution
47+
SolutionGuid = {E10616E1-D346-4CB7-A73E-334145C7731D}
48+
EndGlobalSection
49+
EndGlobal
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Using Include="System.Console" Static="true" />
12+
<Using Include="System.IO.Path" Static="true" />
13+
<Using Include="System.Environment" Static="true" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<CodeSnippets xmlns=
3+
"http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
4+
<CodeSnippet Format="1.0.0">
5+
<Header>
6+
<Title>Statically import common types globally</Title>
7+
</Header>
8+
<Snippet>
9+
<Code Language="XML">
10+
<![CDATA[<ItemGroup>
11+
<Using Include="System.Console" Static="true" />
12+
<Using Include="System.Environment" Static="true" />
13+
<Using Include="System.IO.Path" Static="true" />
14+
</ItemGroup>]]>
15+
</Code>
16+
</Snippet>
17+
</CodeSnippet>
18+
</CodeSnippets>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<CodeSnippets xmlns=
3+
"http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
4+
<CodeSnippet Format="1.0.0">
5+
<Header>
6+
<Title>Configure console for culture</Title>
7+
<Description>Define a static method for Program to configure the culture for the console.</Description>
8+
<Author>Mark J. Price</Author>
9+
<Shortcut>ccc</Shortcut>
10+
</Header>
11+
<Snippet>
12+
<Imports>
13+
<Import>
14+
<Namespace>System.Globalization</Namespace>
15+
</Import>
16+
</Imports>
17+
<Declarations>
18+
<Literal>
19+
<ID>defaultCulture</ID>
20+
<Default>en-US</Default>
21+
<ToolTip>The default culture when none is specified when calling the method.</ToolTip>
22+
</Literal>
23+
</Declarations>
24+
<Code Language="CSharp" Delimiter="~">
25+
<![CDATA[partial class Program
26+
{
27+
static void ConfigureConsole(string culture = "~defaultCulture~",
28+
bool useComputerCulture = false)
29+
{
30+
// To enable Unicode characters like Euro symbol in the console.
31+
OutputEncoding = System.Text.Encoding.UTF8;
32+
33+
if (!useComputerCulture)
34+
{
35+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
36+
}
37+
38+
WriteLine($"CurrentCulture: {CultureInfo.CurrentCulture.DisplayName}");
39+
}
40+
}]]>
41+
</Code>
42+
</Snippet>
43+
</CodeSnippet>
44+
</CodeSnippets>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Globalization;
2+
3+
partial class Program
4+
{
5+
static void ConfigureConsole(string culture = "en-US",
6+
bool useComputerCulture = false)
7+
{
8+
// To enable Unicode characters like Euro symbol in the console.
9+
OutputEncoding = System.Text.Encoding.UTF8;
10+
11+
if (!useComputerCulture)
12+
{
13+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
14+
}
15+
16+
WriteLine($"CurrentCulture: {CultureInfo.CurrentCulture.DisplayName}");
17+
}
18+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ConfigureConsole();
2+
WriteLine(CurrentDirectory);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<!-- The package metadata. Fill in the properties marked as TODO below -->
5+
<!-- Follow the instructions on https://learn.microsoft.com/en-us/nuget/create-packages/package-authoring-best-practices -->
6+
<PackageId>Packt.ConsolePlus.Template</PackageId>
7+
<PackageVersion>1.0</PackageVersion>
8+
<Title>Console App (Packt)</Title>
9+
<Authors>Mark J. Price;Packt Publishing</Authors>
10+
<Description>A console app with extra imported types and a method to configure the culture.</Description>
11+
<PackageTags>dotnet-new;templates;packt;console</PackageTags>
12+
<PackageProjectUrl>https://www.github.com/markjprice/tools-skills-net8</PackageProjectUrl>
13+
14+
<!-- Keep package type as 'Template' to show the package as a template package on nuget.org and make your template available in dotnet new search.-->
15+
<PackageType>Template</PackageType>
16+
<TargetFramework>net8.0</TargetFramework>
17+
<IncludeContentInPack>true</IncludeContentInPack>
18+
<IncludeBuildOutput>false</IncludeBuildOutput>
19+
<ContentTargetFolders>content</ContentTargetFolders>
20+
<NoWarn>$(NoWarn);NU5128</NoWarn>
21+
<NoDefaultExcludes>true</NoDefaultExcludes>
22+
<PackageReadmeFile>README.md</PackageReadmeFile>
23+
</PropertyGroup>
24+
25+
<PropertyGroup>
26+
<LocalizeTemplates>false</LocalizeTemplates>
27+
</PropertyGroup>
28+
29+
<ItemGroup>
30+
<PackageReference Include="Microsoft.TemplateEngine.Tasks" Version="*" PrivateAssets="all" IsImplicitlyDefined="true" />
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<Content Include="content\**\*" Exclude="content\**\bin\**;content\**\obj\**" />
35+
<Compile Remove="**\*" />
36+
</ItemGroup>
37+
38+
<ItemGroup>
39+
<None Include="README.md" Pack="true" PackagePath="" />
40+
</ItemGroup>
41+
42+
<ItemGroup>
43+
<Folder Include="content\" />
44+
</ItemGroup>
45+
46+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- Provide an overview of what your template package does and how to get started.
2+
Consider previewing the README before uploading (https://learn.microsoft.com/en-us/nuget/nuget-org/package-readme-on-nuget-org#preview-your-readme). -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json.schemastore.org/template",
3+
"author": "Mark J. Price",
4+
"classifications": [ "Console", "Packt" ],
5+
"identity": "Packt.ConsolePlus.CSharp",
6+
"name": "Packt Console+ App",
7+
"shortName": "consoleplus",
8+
"tags": {
9+
"language": "C#",
10+
"type": "project"
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Using Include="System.Console" Static="true" />
12+
<Using Include="System.Environment" Static="true" />
13+
<Using Include="System.IO.Path" Static="true" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Globalization;
2+
3+
partial class Program
4+
{
5+
static void ConfigureConsole(string culture = "en-US",
6+
bool useComputerCulture = false)
7+
{
8+
// To enable Unicode characters like Euro symbol in the console.
9+
OutputEncoding = System.Text.Encoding.UTF8;
10+
11+
if (!useComputerCulture)
12+
{
13+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
14+
}
15+
16+
WriteLine($"CurrentCulture: {CultureInfo.CurrentCulture.DisplayName}");
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ConfigureConsole(); // en-US by default.
2+
3+
// Alternatives:
4+
// ConfigureConsole("fr-FR"); // French in France.
5+
// ConfigureConsole(useComputerCulture: true); // Your local culture.
6+
7+
// Add your code below here:
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
[*.{cs,vb}]
3+
#### Naming styles ####
4+
5+
6+
# Naming rules
7+
8+
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
9+
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
10+
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
11+
12+
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
13+
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
14+
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
15+
16+
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
17+
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
18+
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
19+
20+
# Symbol specifications
21+
22+
dotnet_naming_symbols.interface.applicable_kinds = interface
23+
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
24+
dotnet_naming_symbols.interface.required_modifiers =
25+
26+
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
27+
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
28+
dotnet_naming_symbols.types.required_modifiers =
29+
30+
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
31+
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
32+
dotnet_naming_symbols.non_field_members.required_modifiers =
33+
34+
# Naming styles
35+
36+
dotnet_naming_style.begins_with_i.required_prefix = I
37+
dotnet_naming_style.begins_with_i.required_suffix =
38+
dotnet_naming_style.begins_with_i.word_separator =
39+
dotnet_naming_style.begins_with_i.capitalization = pascal_case
40+
41+
dotnet_naming_style.pascal_case.required_prefix =
42+
dotnet_naming_style.pascal_case.required_suffix =
43+
dotnet_naming_style.pascal_case.word_separator =
44+
dotnet_naming_style.pascal_case.capitalization = pascal_case
45+
46+
dotnet_naming_style.pascal_case.required_prefix =
47+
dotnet_naming_style.pascal_case.required_suffix =
48+
dotnet_naming_style.pascal_case.word_separator =
49+
dotnet_naming_style.pascal_case.capitalization = pascal_case
50+
dotnet_style_operator_placement_when_wrapping = beginning_of_line
51+
tab_width = 2
52+
indent_size = 2
53+
end_of_line = crlf
54+
dotnet_style_namespace_match_folder = true:suggestion
55+
56+
[*.cs]
57+
csharp_using_directive_placement = outside_namespace:silent
58+
csharp_prefer_simple_using_statement = true:suggestion
59+
csharp_prefer_braces = true:silent
60+
csharp_style_namespace_declarations = block_scoped:silent
61+
csharp_style_prefer_method_group_conversion = true:silent
62+
csharp_style_prefer_top_level_statements = true:silent
63+
csharp_indent_labels = one_less_than_current
64+
csharp_style_prefer_primary_constructors = true:suggestion
65+
csharp_style_expression_bodied_methods = false:silent
66+
csharp_style_expression_bodied_constructors = false:silent
67+
csharp_space_around_binary_operators = before_and_after
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IProduct
2+
{
3+
string? Description { get; set; }
4+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Product : IProduct
2+
{
3+
private string? description;
4+
5+
public Product()
6+
{
7+
}
8+
9+
public Product(string? description)
10+
{
11+
Description = description;
12+
}
13+
14+
public string? Description { get => description; set => description = value; }
15+
16+
public void Process(string name)
17+
{
18+
if (string.IsNullOrWhiteSpace(name))
19+
{
20+
throw new ArgumentException($"'{nameof(name)}' cannot be null or whitespace.", nameof(name));
21+
}
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Product product = new()
2+
{
3+
Description = "Seafood"
4+
};
5+
6+
WriteLine(product.Description);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Using Include="System.Console" Static="true" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<EditorConfigFiles Remove="C:\tools-skills-net8\Chapter02\RefactoringDemos\.editorconfig" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<None Include="C:\tools-skills-net8\Chapter02\RefactoringDemos\.editorconfig" />
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)