From 284a6e0c58dd361de1f237f835123447d8710e2e Mon Sep 17 00:00:00 2001 From: Ivan Pehsterski Date: Fri, 30 Jan 2026 17:28:08 +0200 Subject: [PATCH 1/7] docs: add overview documentation for design patterns in C# --- docs/csharp/fundamentals/object-oriented/design-patterns.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 docs/csharp/fundamentals/object-oriented/design-patterns.md diff --git a/docs/csharp/fundamentals/object-oriented/design-patterns.md b/docs/csharp/fundamentals/object-oriented/design-patterns.md new file mode 100644 index 0000000000000..e69de29bb2d1d From 0a6a1f69b7d8056911885a11e54544523cfe447f Mon Sep 17 00:00:00 2001 From: Ivan Pehsterski Date: Fri, 30 Jan 2026 18:12:25 +0200 Subject: [PATCH 2/7] docs: add overview documentation for design patterns in C# --- .../object-oriented/design-patterns.md | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/docs/csharp/fundamentals/object-oriented/design-patterns.md b/docs/csharp/fundamentals/object-oriented/design-patterns.md index e69de29bb2d1d..ac47cfbde09de 100644 --- a/docs/csharp/fundamentals/object-oriented/design-patterns.md +++ b/docs/csharp/fundamentals/object-oriented/design-patterns.md @@ -0,0 +1,145 @@ +--- +title: "Design patterns in C#" +description: Provides an overview of common design patterns and their use in C# applications. +ms.date: 01/30/2026 +helpviewer_keywords: + - "design patterns [C#]" + - "creational patterns" + - "structural patterns" + - "behavioral patterns" + - "software design" + - "object-oriented design" + - "C# architecture" +--- + +# Overview of design patterns in C# + +Design patterns are proven solutions to recurring problems in software design. Rather than providing finished code, a design pattern offers a general template that can be adapted to solve a particular design problem in a given context. + +In C#, design patterns are commonly used to improve code readability, maintainability, testability, and extensibility. They are especially valuable in large applications where change is expected over time. + +This article provides a high-level overview of the most commonly used design patterns and their purpose. + +## What is a design pattern + +A *design pattern* describes: + +- A common problem in software design +- The context in which the problem occurs +- A reusable solution structure +- The consequences and trade-offs of using that solution + +Design patterns do not depend on a specific programming language, but their implementation can vary depending on language features. C# provides strong support for design patterns through interfaces, inheritance, delegates, generics, and dependency injection. + +## Categories of design patterns + +Design patterns are traditionally grouped into three main categories. + +### Creational patterns + +Creational patterns focus on *object creation mechanisms*. They help control how and when objects are created, reducing tight coupling between code components. + +Common creational patterns include: + +- Factory Method +- Abstract Factory +- Builder +- Singleton +- Prototype + +These patterns are often used when object creation involves complex logic or when the exact type of object should be determined at runtime. + +## Structural patterns + +Structural patterns deal with *object composition*. They help define how classes and objects are combined to form larger structures while keeping the system flexible. + +Common structural patterns include: + +- Adapter +- Bridge +- Composite +- Decorator +- Facade +- Flyweight +- Proxy + +These patterns are useful when integrating legacy code, adding behavior dynamically, or simplifying complex subsystems. + +## Behavioral patterns + +Behavioral patterns focus on *communication and responsibility* between objects. + +Common behavioral patterns include: + +- Strategy +- Observer +- Command +- Iterator +- Mediator +- State +- Template Method +- Chain of Responsibility + +These patterns help reduce coupling by defining clear interaction rules between components. + +## Benefits of using design patterns + +Using design patterns can provide several advantages: + +- Improved code readability and structure +- Reduced coupling between components +- Better separation of concerns +- Easier testing and mocking +- Improved long-term maintainability + +Design patterns also make code easier to understand for other developers, as many patterns are widely recognized across the software industry. + +## Design patterns and SOLID principles + +Design patterns often work hand-in-hand with the SOLID principles: + +- **Single Responsibility Principle** encourages small, focused classes +- **Open/Closed Principle** promotes extension without modification +- **Liskov Substitution Principle** ensures safe polymorphism +- **Interface Segregation Principle** avoids large, monolithic interfaces +- **Dependency Inversion Principle** encourages abstraction over concretion + +Many patterns, such as Strategy, Factory Method, and Dependency Injection, naturally support these principles. + +## When not to use design patterns + +Design patterns should not be applied automatically. Overusing patterns can lead to unnecessary complexity. + +Avoid design patterns when: + +- The problem is simple +- The solution is unlikely to change +- The added abstraction provides no clear benefit + +Patterns should solve real problems, not be used for their own sake. + +## Design patterns in modern C# + +Modern C# features simplify many pattern implementations: + +- Records reduce boilerplate for immutable objects +- Dependency injection is built into ASP.NET Core +- Delegates and lambdas simplify behavioral patterns +- Pattern matching improves readability +- Minimal APIs reduce structural overhead + +These features often replace or simplify classic implementations found in older examples. + +## Next steps + +The following articles in this series describe individual design patterns with practical C# examples: + +- Creational patterns +- Structural patterns +- Behavioral patterns + +Each article explains when to use the pattern, how it works, and its advantages and disadvantages. + +## C# Language Specification + +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] \ No newline at end of file From 0a9deaf2e4be9b75f3e2043e55ef1722fac1471a Mon Sep 17 00:00:00 2001 From: Ivan Pehsterski Date: Fri, 30 Jan 2026 20:38:36 +0200 Subject: [PATCH 3/7] Add interface-based greeting example using switch expression --- .../Greeting/Greeting/Contracts/IGreet.cs | 9 ++ .../Greeting/Greeting/Greeting.csproj | 18 +++ .../Greeting/Greeting/Models/Greeter.cs | 21 +++ .../snippets/Greeting/Greeting/Program.cs | 20 +++ .../snippets/Greeting/Greeting/README.md | 131 ++++++++++++++++++ 5 files changed, 199 insertions(+) create mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs create mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj create mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs create mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs create mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/README.md diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs b/docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs new file mode 100644 index 0000000000000..130c4114caff7 --- /dev/null +++ b/docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs @@ -0,0 +1,9 @@ +using System; +namespace Greeting.Contracts +{ + public interface IGreet + { + string GetGreeting(int hour); + } +} + diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj b/docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj new file mode 100644 index 0000000000000..b6b233c01a3b0 --- /dev/null +++ b/docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs b/docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs new file mode 100644 index 0000000000000..805de7df23b32 --- /dev/null +++ b/docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs @@ -0,0 +1,21 @@ +using System; +using Greeting.Contracts; + +namespace Greeting.Models +{ + public class Greeter : IGreet + { + public string GetGreeting(int hour) + { + return hour switch + { + >= 5 and <= 11 => "Good morning!", + >= 12 and <= 17 => "Good afternoon!", + >= 18 and <= 22 => "Good evening!", + _ => "Good night!" + + }; + } + } +} + diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs b/docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs new file mode 100644 index 0000000000000..881932cad4a43 --- /dev/null +++ b/docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs @@ -0,0 +1,20 @@ +using Greeting.Contracts; +using Greeting.Models; + +namespace Greeting; +class Program +{ + static void Main(string[] args) + { + Console.Write("Enter time: "); + int hour = int.Parse(Console.ReadLine()); + + IGreet greet = new Greeter(); + + string greetingResult = greet.GetGreeting(hour); + + Console.WriteLine(greetingResult); + Console.ReadKey(); + } +} + diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/README.md b/docs/csharp/tutorials/snippets/Greeting/Greeting/README.md new file mode 100644 index 0000000000000..ef5f4c2523438 --- /dev/null +++ b/docs/csharp/tutorials/snippets/Greeting/Greeting/README.md @@ -0,0 +1,131 @@ +# Interface Greeting Example + +This project demonstrates a simple and clean use of **interfaces in C#** by implementing a greeting system based on the hour of the day. + +The goal of this example is educational — to show how interfaces help separate **abstraction from implementation** while keeping the code easy to read, test, and extend. + +--- + +## 📌 Overview + +The application asks the user to enter an hour (0–23) and prints an appropriate greeting in English: + +* **Good morning** +* **Good afternoon** +* **Good evening** +* **Good night** + +The greeting logic is defined through an interface and implemented using a concrete class. + +--- + +## 🧠 Key Concepts Demonstrated + +* Interface-based design +* Separation of concerns +* Polymorphism +* Switch expressions with pattern matching +* Clean and readable C# code + +--- + +## 🔌 Interface + +The interface defines a contract for greeting behavior: + +```csharp +public interface IGreet +{ + string GetGreeting(int hour); +} +``` + +This allows different implementations to be created without changing the code that uses them. + +--- + +## ⚙️ Implementation + +The greeting logic is implemented using a modern C# switch expression: + +```csharp +public class Greeter : IGreet +{ + public string GetGreeting(int hour) + { + return hour switch + { + >= 5 and <= 11 => "Good morning!", + >= 12 and <= 17 => "Good afternoon!", + >= 18 and <= 22 => "Good evening!", + _ => "Good night!" + }; + } +} +``` + +--- + +## ▶️ How It Works + +1. The user enters an hour between **0 and 23** +2. The program calls the greeting service through the interface +3. The appropriate greeting is returned and displayed + +```csharp +IGreetingService greetingService = new GreetingService(); +Console.WriteLine(greetingService.GetGreeting(hour)); +``` + +The program depends on the **interface**, not the concrete implementation. + +--- + +## ✅ Why Use an Interface Here? + +Using an interface provides several benefits: + +* The program is loosely coupled +* The greeting logic can be replaced or extended +* The code is easier to test +* The design follows the **Dependency Inversion Principle** + +For example, new implementations could be added later: + +* `GermanGreetingService` +* `FrenchGreetingService` +* `TimeBasedGreetingService` + +Without changing the main program logic. + +--- + +## 🧪 Testing Friendly Design + +Because the hour is passed as a parameter, the logic can be easily tested: + +```csharp +GetGreeting(9); // Good morning +GetGreeting(15); // Good afternoon +GetGreeting(21); // Good evening +``` + +No system time dependencies are required. + +--- + +## 🎯 Purpose + +This project is intended as a **learning example** for developers who are beginning with: + +* Interfaces in C# +* Clean architecture principles +* Writing maintainable and readable code + +It is suitable for documentation, tutorials, and beginner-friendly demonstrations. + +--- + +## 📄 License + +This project is provided for educational purposes and can be freely used in learning materials or documentation examples. From 16e892bd8a83c259d2cef4fbe60accc55816bce0 Mon Sep 17 00:00:00 2001 From: Ivan Peshterski Date: Mon, 2 Feb 2026 17:41:23 +0200 Subject: [PATCH 4/7] Fix markdownlint formatting (newline at EOF) --- .../object-oriented/design-patterns.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/csharp/fundamentals/object-oriented/design-patterns.md b/docs/csharp/fundamentals/object-oriented/design-patterns.md index ac47cfbde09de..47b44c68f25ee 100644 --- a/docs/csharp/fundamentals/object-oriented/design-patterns.md +++ b/docs/csharp/fundamentals/object-oriented/design-patterns.md @@ -2,7 +2,7 @@ title: "Design patterns in C#" description: Provides an overview of common design patterns and their use in C# applications. ms.date: 01/30/2026 -helpviewer_keywords: +helpviewer_keywords: - "design patterns [C#]" - "creational patterns" - "structural patterns" @@ -24,10 +24,10 @@ This article provides a high-level overview of the most commonly used design pat A *design pattern* describes: -- A common problem in software design -- The context in which the problem occurs -- A reusable solution structure -- The consequences and trade-offs of using that solution +- A common problem in software design +- The context in which the problem occurs +- A reusable solution structure +- The consequences and trade-offs of using that solution Design patterns do not depend on a specific programming language, but their implementation can vary depending on language features. C# provides strong support for design patterns through interfaces, inheritance, delegates, generics, and dependency injection. @@ -140,6 +140,7 @@ The following articles in this series describe individual design patterns with p Each article explains when to use the pattern, how it works, and its advantages and disadvantages. -## C# Language Specification +## C# Language Specification + +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] -[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] \ No newline at end of file From 2933ff5f047a2f8d6711d4a0925ff6d7faba8dff Mon Sep 17 00:00:00 2001 From: Ivan Peshterski Date: Mon, 2 Feb 2026 21:54:45 +0200 Subject: [PATCH 5/7] Delete file --- .../object-oriented/design-patterns.md | 146 ------------------ 1 file changed, 146 deletions(-) delete mode 100644 docs/csharp/fundamentals/object-oriented/design-patterns.md diff --git a/docs/csharp/fundamentals/object-oriented/design-patterns.md b/docs/csharp/fundamentals/object-oriented/design-patterns.md deleted file mode 100644 index 47b44c68f25ee..0000000000000 --- a/docs/csharp/fundamentals/object-oriented/design-patterns.md +++ /dev/null @@ -1,146 +0,0 @@ ---- -title: "Design patterns in C#" -description: Provides an overview of common design patterns and their use in C# applications. -ms.date: 01/30/2026 -helpviewer_keywords: - - "design patterns [C#]" - - "creational patterns" - - "structural patterns" - - "behavioral patterns" - - "software design" - - "object-oriented design" - - "C# architecture" ---- - -# Overview of design patterns in C# - -Design patterns are proven solutions to recurring problems in software design. Rather than providing finished code, a design pattern offers a general template that can be adapted to solve a particular design problem in a given context. - -In C#, design patterns are commonly used to improve code readability, maintainability, testability, and extensibility. They are especially valuable in large applications where change is expected over time. - -This article provides a high-level overview of the most commonly used design patterns and their purpose. - -## What is a design pattern - -A *design pattern* describes: - -- A common problem in software design -- The context in which the problem occurs -- A reusable solution structure -- The consequences and trade-offs of using that solution - -Design patterns do not depend on a specific programming language, but their implementation can vary depending on language features. C# provides strong support for design patterns through interfaces, inheritance, delegates, generics, and dependency injection. - -## Categories of design patterns - -Design patterns are traditionally grouped into three main categories. - -### Creational patterns - -Creational patterns focus on *object creation mechanisms*. They help control how and when objects are created, reducing tight coupling between code components. - -Common creational patterns include: - -- Factory Method -- Abstract Factory -- Builder -- Singleton -- Prototype - -These patterns are often used when object creation involves complex logic or when the exact type of object should be determined at runtime. - -## Structural patterns - -Structural patterns deal with *object composition*. They help define how classes and objects are combined to form larger structures while keeping the system flexible. - -Common structural patterns include: - -- Adapter -- Bridge -- Composite -- Decorator -- Facade -- Flyweight -- Proxy - -These patterns are useful when integrating legacy code, adding behavior dynamically, or simplifying complex subsystems. - -## Behavioral patterns - -Behavioral patterns focus on *communication and responsibility* between objects. - -Common behavioral patterns include: - -- Strategy -- Observer -- Command -- Iterator -- Mediator -- State -- Template Method -- Chain of Responsibility - -These patterns help reduce coupling by defining clear interaction rules between components. - -## Benefits of using design patterns - -Using design patterns can provide several advantages: - -- Improved code readability and structure -- Reduced coupling between components -- Better separation of concerns -- Easier testing and mocking -- Improved long-term maintainability - -Design patterns also make code easier to understand for other developers, as many patterns are widely recognized across the software industry. - -## Design patterns and SOLID principles - -Design patterns often work hand-in-hand with the SOLID principles: - -- **Single Responsibility Principle** encourages small, focused classes -- **Open/Closed Principle** promotes extension without modification -- **Liskov Substitution Principle** ensures safe polymorphism -- **Interface Segregation Principle** avoids large, monolithic interfaces -- **Dependency Inversion Principle** encourages abstraction over concretion - -Many patterns, such as Strategy, Factory Method, and Dependency Injection, naturally support these principles. - -## When not to use design patterns - -Design patterns should not be applied automatically. Overusing patterns can lead to unnecessary complexity. - -Avoid design patterns when: - -- The problem is simple -- The solution is unlikely to change -- The added abstraction provides no clear benefit - -Patterns should solve real problems, not be used for their own sake. - -## Design patterns in modern C# - -Modern C# features simplify many pattern implementations: - -- Records reduce boilerplate for immutable objects -- Dependency injection is built into ASP.NET Core -- Delegates and lambdas simplify behavioral patterns -- Pattern matching improves readability -- Minimal APIs reduce structural overhead - -These features often replace or simplify classic implementations found in older examples. - -## Next steps - -The following articles in this series describe individual design patterns with practical C# examples: - -- Creational patterns -- Structural patterns -- Behavioral patterns - -Each article explains when to use the pattern, how it works, and its advantages and disadvantages. - -## C# Language Specification - -[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - From 1cdcfea5dc498a4ad6a8b298182a19a796463cd8 Mon Sep 17 00:00:00 2001 From: Ivan Peshterski Date: Fri, 6 Feb 2026 20:20:14 +0200 Subject: [PATCH 6/7] Fix issue --- .../using-directive-errors.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md index c52b701cfc5c9..17d96dd760fcd 100644 --- a/docs/csharp/language-reference/compiler-messages/using-directive-errors.md +++ b/docs/csharp/language-reference/compiler-messages/using-directive-errors.md @@ -20,6 +20,9 @@ f1_keywords: - "CS8914" - "CS8915" - "CS8933" + - "CS8954" + - "CS8955" + - "CS8956" - "CS9130" - "CS9131" - "CS9132" @@ -44,6 +47,9 @@ helpviewer_keywords: - "CS8914" - "CS8915" - "CS8933" + - "CS8954" + - "CS8955" + - "CS8956" - "CS9130" - "CS9131" - "CS9132" @@ -71,6 +77,9 @@ That's be design. The text closely matches the text of the compiler error / warn - [**CS8085**](#restrictions-on-using-aliases): *Error: A 'using static' directive cannot be used to declare an alias.* - [**CS8914**](#global-using-directive): *Error: A global using directive cannot be used in a namespace declaration.* - [**CS8915**](#global-using-directive): *Error: A global using directive must precede all non-global using directives.* +- [**CS8954**](#global-using-directive): *Error: A global using directive must precede all non-global using directives.* +- [**CS8955**](#global-using-directive): *Error: A file-local type cannot be used in a global using directive.* +- [**CS8956**](#using-directive-placement): *Error: A using directive must precede all other elements defined in the namespace except extern alias declarations.* - [**CS9130**](#restrictions-on-using-aliases): *Error: Using alias cannot be a `ref` type.* - [**CS9131**](#restrictions-on-using-aliases): *Error: Only a using alias can be `unsafe`.* - [**CS9132**](#restrictions-on-using-aliases): *Error: Using alias cannot be a nullable reference type.* @@ -107,6 +116,10 @@ The compiler produces warning **CS8933**, **CS0105** or diagnostic **CS8019** fo Incorrectly combining a `using` directive with the `static`, `global`, or `unsafe` modifiers on a `using` directive are covered later in this article. +The compiler also produces **CS8956** when a using directive doesn't appear before other elements defined in a namespace (except extern alias declarations). + + + ## Using static directive The `using static` directive imports one type's members into the current namespace. The following example imports the methods from `System.Console`, such as `WriteLine` into the current namespace: @@ -141,6 +154,15 @@ Any `global using` directives must precede any non-global `using` directives in Furthermore, a `static global using` directive can't reference a [file-local](../keywords/file.md) type. +Violating these rules can also produce the following errors: + +### CS8954 +A global using directive must precede all non-global using directives. + +### CS8955 +A file-local type cannot be used in a global using directive. + + ## Alias qualifier The alias qualifier, [`::`](../operators/namespace-alias-qualifier.md), precedes a namespace alias, or follows the `global` alias. If you use `::` where `.` should be used to separate elements of a fully qualified name, the compiler emits one of **CS0431**, **CS0432**, **CS0687**, **CS7000*, or **CS8083**. From da8cc43f15fb353624a91ca642842d08fafb645e Mon Sep 17 00:00:00 2001 From: Ivan Pehsterski Date: Fri, 6 Feb 2026 20:27:16 +0200 Subject: [PATCH 7/7] Delete docs/csharp/tutorials/snippets/Greeting/Greeting directory --- .../Greeting/Greeting/Contracts/IGreet.cs | 9 -- .../Greeting/Greeting/Greeting.csproj | 18 --- .../Greeting/Greeting/Models/Greeter.cs | 21 --- .../snippets/Greeting/Greeting/Program.cs | 20 --- .../snippets/Greeting/Greeting/README.md | 131 ------------------ 5 files changed, 199 deletions(-) delete mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs delete mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj delete mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs delete mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs delete mode 100644 docs/csharp/tutorials/snippets/Greeting/Greeting/README.md diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs b/docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs deleted file mode 100644 index 130c4114caff7..0000000000000 --- a/docs/csharp/tutorials/snippets/Greeting/Greeting/Contracts/IGreet.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; -namespace Greeting.Contracts -{ - public interface IGreet - { - string GetGreeting(int hour); - } -} - diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj b/docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj deleted file mode 100644 index b6b233c01a3b0..0000000000000 --- a/docs/csharp/tutorials/snippets/Greeting/Greeting/Greeting.csproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - Exe - net6.0 - enable - enable - - - - - - - - - - - diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs b/docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs deleted file mode 100644 index 805de7df23b32..0000000000000 --- a/docs/csharp/tutorials/snippets/Greeting/Greeting/Models/Greeter.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using Greeting.Contracts; - -namespace Greeting.Models -{ - public class Greeter : IGreet - { - public string GetGreeting(int hour) - { - return hour switch - { - >= 5 and <= 11 => "Good morning!", - >= 12 and <= 17 => "Good afternoon!", - >= 18 and <= 22 => "Good evening!", - _ => "Good night!" - - }; - } - } -} - diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs b/docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs deleted file mode 100644 index 881932cad4a43..0000000000000 --- a/docs/csharp/tutorials/snippets/Greeting/Greeting/Program.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Greeting.Contracts; -using Greeting.Models; - -namespace Greeting; -class Program -{ - static void Main(string[] args) - { - Console.Write("Enter time: "); - int hour = int.Parse(Console.ReadLine()); - - IGreet greet = new Greeter(); - - string greetingResult = greet.GetGreeting(hour); - - Console.WriteLine(greetingResult); - Console.ReadKey(); - } -} - diff --git a/docs/csharp/tutorials/snippets/Greeting/Greeting/README.md b/docs/csharp/tutorials/snippets/Greeting/Greeting/README.md deleted file mode 100644 index ef5f4c2523438..0000000000000 --- a/docs/csharp/tutorials/snippets/Greeting/Greeting/README.md +++ /dev/null @@ -1,131 +0,0 @@ -# Interface Greeting Example - -This project demonstrates a simple and clean use of **interfaces in C#** by implementing a greeting system based on the hour of the day. - -The goal of this example is educational — to show how interfaces help separate **abstraction from implementation** while keeping the code easy to read, test, and extend. - ---- - -## 📌 Overview - -The application asks the user to enter an hour (0–23) and prints an appropriate greeting in English: - -* **Good morning** -* **Good afternoon** -* **Good evening** -* **Good night** - -The greeting logic is defined through an interface and implemented using a concrete class. - ---- - -## 🧠 Key Concepts Demonstrated - -* Interface-based design -* Separation of concerns -* Polymorphism -* Switch expressions with pattern matching -* Clean and readable C# code - ---- - -## 🔌 Interface - -The interface defines a contract for greeting behavior: - -```csharp -public interface IGreet -{ - string GetGreeting(int hour); -} -``` - -This allows different implementations to be created without changing the code that uses them. - ---- - -## ⚙️ Implementation - -The greeting logic is implemented using a modern C# switch expression: - -```csharp -public class Greeter : IGreet -{ - public string GetGreeting(int hour) - { - return hour switch - { - >= 5 and <= 11 => "Good morning!", - >= 12 and <= 17 => "Good afternoon!", - >= 18 and <= 22 => "Good evening!", - _ => "Good night!" - }; - } -} -``` - ---- - -## ▶️ How It Works - -1. The user enters an hour between **0 and 23** -2. The program calls the greeting service through the interface -3. The appropriate greeting is returned and displayed - -```csharp -IGreetingService greetingService = new GreetingService(); -Console.WriteLine(greetingService.GetGreeting(hour)); -``` - -The program depends on the **interface**, not the concrete implementation. - ---- - -## ✅ Why Use an Interface Here? - -Using an interface provides several benefits: - -* The program is loosely coupled -* The greeting logic can be replaced or extended -* The code is easier to test -* The design follows the **Dependency Inversion Principle** - -For example, new implementations could be added later: - -* `GermanGreetingService` -* `FrenchGreetingService` -* `TimeBasedGreetingService` - -Without changing the main program logic. - ---- - -## 🧪 Testing Friendly Design - -Because the hour is passed as a parameter, the logic can be easily tested: - -```csharp -GetGreeting(9); // Good morning -GetGreeting(15); // Good afternoon -GetGreeting(21); // Good evening -``` - -No system time dependencies are required. - ---- - -## 🎯 Purpose - -This project is intended as a **learning example** for developers who are beginning with: - -* Interfaces in C# -* Clean architecture principles -* Writing maintainable and readable code - -It is suitable for documentation, tutorials, and beginner-friendly demonstrations. - ---- - -## 📄 License - -This project is provided for educational purposes and can be freely used in learning materials or documentation examples.