From eecba258b3f10ca84c55efce8109f012bfb954e5 Mon Sep 17 00:00:00 2001 From: Oli Sturm Date: Thu, 17 Feb 2022 16:07:37 +0000 Subject: [PATCH] initial --- samples/.editorconfig | 200 ++++++++++++++++++++++++++++++++++ samples/.gitignore | 159 +++++++++++++++++++++++++++ samples/.vscode/launch.json | 28 +++++ samples/.vscode/settings.json | 3 + samples/.vscode/tasks.json | 18 +++ samples/CS10/CS10.csproj | 10 ++ samples/CS10/Program.cs | 25 +++++ samples/CS7/CS7.csproj | 10 ++ samples/CS7/Program.cs | 39 +++++++ samples/CS8/CS8.csproj | 10 ++ samples/CS8/Program.cs | 41 +++++++ samples/CS9/CS9.csproj | 10 ++ samples/CS9/Program.cs | 43 ++++++++ 13 files changed, 596 insertions(+) create mode 100644 samples/.editorconfig create mode 100644 samples/.gitignore create mode 100644 samples/.vscode/launch.json create mode 100644 samples/.vscode/settings.json create mode 100644 samples/.vscode/tasks.json create mode 100644 samples/CS10/CS10.csproj create mode 100644 samples/CS10/Program.cs create mode 100644 samples/CS7/CS7.csproj create mode 100644 samples/CS7/Program.cs create mode 100644 samples/CS8/CS8.csproj create mode 100644 samples/CS8/Program.cs create mode 100644 samples/CS9/CS9.csproj create mode 100644 samples/CS9/Program.cs diff --git a/samples/.editorconfig b/samples/.editorconfig new file mode 100644 index 0000000..2b241a4 --- /dev/null +++ b/samples/.editorconfig @@ -0,0 +1,200 @@ +# Remove the line below if you want to inherit .editorconfig settings from higher directories +root = true + +# C# files +[*.cs] + +#### Core EditorConfig Options #### + +# Indentation and spacing +indent_size = 2 +indent_style = space +tab_width = 4 + +# New line preferences +end_of_line = crlf +insert_final_newline = false + +#### .NET Coding Conventions #### + +# Organize usings +dotnet_separate_import_directive_groups = false +dotnet_sort_system_directives_first = false + +# this. and Me. preferences +dotnet_style_qualification_for_event = false:silent +dotnet_style_qualification_for_field = false:silent +dotnet_style_qualification_for_method = false:silent +dotnet_style_qualification_for_property = false:silent + +# Language keywords vs BCL types preferences +dotnet_style_predefined_type_for_locals_parameters_members = true:silent +dotnet_style_predefined_type_for_member_access = true:silent + +# Parentheses preferences +dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:silent +dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:silent +dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent +dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:silent + +# Modifier preferences +dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent + +# Expression-level preferences +dotnet_style_coalesce_expression = true:silent +dotnet_style_collection_initializer = true:silent +dotnet_style_explicit_tuple_names = true:silent +dotnet_style_null_propagation = true:silent +dotnet_style_object_initializer = true:silent +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_prefer_compound_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent +dotnet_style_prefer_inferred_anonymous_type_member_names = true:silent +dotnet_style_prefer_inferred_tuple_names = true:silent +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent + +# Field preferences +dotnet_style_readonly_field = true:silent + +# Parameter preferences +dotnet_code_quality_unused_parameters = all:silent + +#### C# Coding Conventions #### + +# var preferences +csharp_style_var_elsewhere = false:silent +csharp_style_var_for_built_in_types = false:silent +csharp_style_var_when_type_is_apparent = false:silent + +# Expression-bodied members +csharp_style_expression_bodied_accessors = true:silent +csharp_style_expression_bodied_constructors = false:silent +csharp_style_expression_bodied_indexers = true:silent +csharp_style_expression_bodied_lambdas = true:silent +csharp_style_expression_bodied_local_functions = true:silent +csharp_style_expression_bodied_methods = false:silent +csharp_style_expression_bodied_operators = false:silent +csharp_style_expression_bodied_properties = true:silent + +# Pattern matching preferences +csharp_style_pattern_matching_over_as_with_null_check = true:silent +csharp_style_pattern_matching_over_is_with_cast_check = true:silent +csharp_style_prefer_switch_expression = true:silent + +# Null-checking preferences +csharp_style_conditional_delegate_call = true:silent + +# Modifier preferences +csharp_prefer_static_local_function = true:silent +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async + +# Code-block preferences +csharp_prefer_braces = true:silent +csharp_prefer_simple_using_statement = false:silent + +# Expression-level preferences +csharp_prefer_simple_default_expression = true:silent +csharp_style_deconstructed_variable_declaration = true:silent +csharp_style_inlined_variable_declaration = true:silent +csharp_style_pattern_local_over_anonymous_function = true:silent +csharp_style_prefer_index_operator = true:silent +csharp_style_prefer_range_operator = true:silent +csharp_style_throw_expression = true:silent +csharp_style_unused_value_assignment_preference = discard_variable:silent +csharp_style_unused_value_expression_statement_preference = discard_variable:silent + +# 'using' directive preferences +csharp_using_directive_placement = outside_namespace:silent + +#### C# Formatting Rules #### + +# New line preferences +csharp_new_line_before_catch = true +csharp_new_line_before_else = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_before_members_in_object_initializers = true +# Oli - need the following for proper formatting of +# switch expressions with property patterns. +# See https://github.com/dotnet/docs/issues/24018 +csharp_new_line_before_open_brace = object_collection_array_initializers +csharp_new_line_between_query_expression_clauses = true + +# Indentation preferences +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents = true +csharp_indent_case_contents_when_block = true +csharp_indent_labels = one_less_than_current +csharp_indent_switch_labels = true + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_after_comma = true +csharp_space_after_dot = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_after_semicolon_in_for_statement = true +csharp_space_around_binary_operators = before_and_after +csharp_space_around_declaration_statements = false +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_before_comma = false +csharp_space_before_dot = false +csharp_space_before_open_square_brackets = false +csharp_space_before_semicolon_in_for_statement = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_between_square_brackets = false + +# Wrapping preferences +csharp_preserve_single_line_blocks = true +csharp_preserve_single_line_statements = true + +#### Naming styles #### + +# Naming rules + +dotnet_naming_rule.interface_should_be_begins_with_i.severity = silent +dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface +dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i + +dotnet_naming_rule.types_should_be_pascal_case.severity = silent +dotnet_naming_rule.types_should_be_pascal_case.symbols = types +dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case + +dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = silent +dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members +dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case + +# Symbol specifications + +dotnet_naming_symbols.interface.applicable_kinds = interface +dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.interface.required_modifiers = + +dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum +dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.types.required_modifiers = + +dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method +dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected +dotnet_naming_symbols.non_field_members.required_modifiers = + +# Naming styles + +dotnet_naming_style.pascal_case.required_prefix = +dotnet_naming_style.pascal_case.required_suffix = +dotnet_naming_style.pascal_case.word_separator = +dotnet_naming_style.pascal_case.capitalization = pascal_case + +dotnet_naming_style.begins_with_i.required_prefix = I +dotnet_naming_style.begins_with_i.required_suffix = +dotnet_naming_style.begins_with_i.word_separator = +dotnet_naming_style.begins_with_i.capitalization = pascal_case diff --git a/samples/.gitignore b/samples/.gitignore new file mode 100644 index 0000000..fcaad07 --- /dev/null +++ b/samples/.gitignore @@ -0,0 +1,159 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# DNX +project.lock.json +artifacts/ + +# User-specific files +*.suo +*.user +*.sln.docstates +*.userprefs + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets +!packages/*/build/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.svclog +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +!Local.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer Visual Studio version. +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +# Leftovers from Windows and Mac + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac stuff +.DS_Store diff --git a/samples/.vscode/launch.json b/samples/.vscode/launch.json new file mode 100644 index 0000000..b3a0eca --- /dev/null +++ b/samples/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + //"program": "${workspaceFolder}/NewRecordWith/bin/Debug/net6.0/NewRecordWith.dll", + "program": "${fileDirname}/bin/Debug/net6.0/${relativeFileDirname}.dll", + "args": [], + "cwd": "${fileDirname}", + //"cwd": "${workspaceFolder}/NewRecordWith", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/samples/.vscode/settings.json b/samples/.vscode/settings.json new file mode 100644 index 0000000..cac0e10 --- /dev/null +++ b/samples/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.formatOnSave": true +} \ No newline at end of file diff --git a/samples/.vscode/tasks.json b/samples/.vscode/tasks.json new file mode 100644 index 0000000..7beb6fb --- /dev/null +++ b/samples/.vscode/tasks.json @@ -0,0 +1,18 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${fileDirname}/${relativeFileDirname}.csproj", + //"${workspaceFolder}/NewRecordWith/NewRecordWith.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + ] +} \ No newline at end of file diff --git a/samples/CS10/CS10.csproj b/samples/CS10/CS10.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/samples/CS10/CS10.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/samples/CS10/Program.cs b/samples/CS10/Program.cs new file mode 100644 index 0000000..e6165b8 --- /dev/null +++ b/samples/CS10/Program.cs @@ -0,0 +1,25 @@ +namespace CS10 { + class Program { + // Nested types, just like the C# 8 sample: + public class Address { + public string? City { get; set; } + public string? Country { get; set; } + } + + public class Customer { + // ... + public Address? Address { get; set; } + } + + static void Main(string[] args) { + // Nested property patterns, now shorter: + var customer = new Customer + { + Address = new Address { City = "Castle Douglas", Country = "UK" } + }; + if (customer is { Address.City: "Castle Douglas" }) { + Console.WriteLine("Dieser Kunde wohnt gleich um die Ecke"); + } + } + } +} diff --git a/samples/CS7/CS7.csproj b/samples/CS7/CS7.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/samples/CS7/CS7.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/samples/CS7/Program.cs b/samples/CS7/Program.cs new file mode 100644 index 0000000..32f2b1b --- /dev/null +++ b/samples/CS7/Program.cs @@ -0,0 +1,39 @@ +namespace CS7 { + public class DeadPerson { + public string Name { get; init; } = ""; + public virtual bool IsAlive => false; + } + + public class Zombie : DeadPerson { + public override bool IsAlive => true; + } + + class Program { + static void CheckZombie(DeadPerson p) { + switch (p) { + case Zombie z: + Console.WriteLine($"{z.Name} is a zombie, run!"); + break; + default: + Console.WriteLine($"Looks like {p.Name} is safe."); + break; + } + } + + static void Main(string[] args) { + string s = "This is a string"; + + if (s is string x) + Console.WriteLine($"Turns out s is the string '{x}'"); + + var d1 = new DeadPerson { Name = "Abe Lincoln" }; + var d2 = new Zombie { Name = "Windows 7" }; + + CheckZombie(d1); + CheckZombie(d2); + + Console.WriteLine("My heartfelt apologies in case anybody finds my zombie sample offensive."); + } + + } +} \ No newline at end of file diff --git a/samples/CS8/CS8.csproj b/samples/CS8/CS8.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/samples/CS8/CS8.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/samples/CS8/Program.cs b/samples/CS8/Program.cs new file mode 100644 index 0000000..d462afe --- /dev/null +++ b/samples/CS8/Program.cs @@ -0,0 +1,41 @@ +namespace CS8 { + class Program { + // This is a "switch expression" + static int CalcResult(int input) => input switch + { + 1 => 2, + 2 => 3, + _ => throw new ArgumentException("Weiter geht's nicht") + }; + + // Property Pattern: + bool IsJune(DateTime dt) => dt is { Month: 6 }; + + public class Address { + public string? City { get; set; } + public string? Country { get; set; } + } + + public class Customer { + // ... + public Address? Address { get; set; } + } + + static void Main(string[] args) { + // Positional pattern (using a tuple - hence "tuple pattern") + // Important: this works with any deconstructable type! + (string, string) personInfo = ("Oli", "Sturm"); + var (firstName, lastName) = personInfo; + // firstName ist jetzt "Oli" und lastName ist "Sturm" + + // Nested property patterns: + var customer = new Customer + { + Address = new Address { City = "Castle Douglas", Country = "UK" } + }; + if (customer is { Address: { City: "Castle Douglas" } }) { + Console.WriteLine("Dieser Kunde wohnt gleich um die Ecke"); + } + } + } +} diff --git a/samples/CS9/CS9.csproj b/samples/CS9/CS9.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/samples/CS9/CS9.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/samples/CS9/Program.cs b/samples/CS9/Program.cs new file mode 100644 index 0000000..37a9112 --- /dev/null +++ b/samples/CS9/Program.cs @@ -0,0 +1,43 @@ +namespace CS9 { + class Program { + // Relational patterns (>= operator et al.) + // Logical patterns ("or", also "and" and "not") + // Note: parens ( and ) can be used to determine precedence + bool IsPastMidYear(DateTime dt) => dt is { Month: >= 7 }; + bool IsThisYearOrLast(DateTime dt) => dt is { Year: 2022 or 2021 }; + bool IsSummer(DateTime dt) => dt is + { Month: 7 or 8 } or + { Month: 6, Day: >= 21 } or + { Month: 9, Day: < 21 }; + + // Class with "Deconstruct" implementation + class Order { + public int ItemCount { get; set; } + public double ItemPrice { get; set; } + public void Deconstruct(out int itemCount, out double itemPrice) { + itemCount = ItemCount; + itemPrice = ItemPrice; + } + } + + // Positional patterns with placeholders - note that + // the element lists must be "complete"! + static OrderValue OrderValueCategory(Order o) => + o switch + { + ( < 0, _) => throw new ArgumentException("Positive itemCounts please!"), + (_, < 0) => throw new ArgumentException("Positive itemPrices please!"), + ( >= 100, _) => OrderValue.ValuableDueToHighCount, + (_, >= 1000) => OrderValue.ValuableDueToHighItemPrice, + + // Assign variable names for further processing + var (c, p) when c * p > 1000 => OrderValue.ValuableDueToHighTotal, + + _ => OrderValue.NotValuable + }; + + + static void Main(string[] args) { + } + } +}