When modeling scenarios with a fixed set of outcomes — such as parsing user input or handling HTTP requests — developers often rely on enums or inheritance, which can compromise clarity and type safety. C# 15 introduces union types as a more robust alternative. These allow you to define a closed set of types with implicit conversions and pattern matching, ensuring compile-time exhaustiveness. This means the compiler will catch missing cases, reducing runtime errors. For example, a Result<int, Error> type cleanly represents success or failure, aligning with functional programming patterns and improving code safety. I believe union types offer a design shift that makes C# more expressive and safer — especially for handling domain events or API responses. What do you think — how have you handled similar scenarios before union types? #CSharp #DotNet #Programming #SoftwareDevelopment
C# 15 Union Types Improve Code Safety and Clarity
More Relevant Posts
-
Strategy Pattern in C# -- Animated Code Walkthrough Ever used a giant switch statement to handle different behaviors? There's a much cleaner way. In this short animated video, I walk you through the Strategy Pattern -- one of the most practical design patterns for real .NET projects. What you'll learn: -- The problem with switch/if-else chains -- How interfaces solve it elegantly -- Real C# code example -- Before vs After comparison -- Key takeaways for production code The Strategy Pattern lets you swap algorithms at runtime without touching the calling code. Open/Closed Principle in action! Which design pattern do you use most? Comment below! #CSharp #DotNet #DesignPatterns #StrategyPattern #CleanCode #SoftwareEngineering #Programming #CodeWalkthrough #AspNetCore #TechTips
Strategy Pattern in C# - Animated Code Walkthrough
To view or add a comment, sign in
-
💡 𝗖#/.𝗡𝗘𝗧 - 𝗖𝗹𝗲𝗮𝗻 𝗖𝗼𝗱𝗲 𝗧𝗶𝗽 🔥 💎 𝗦𝘄𝗶𝘁𝗰𝗵 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 𝐯𝐬 𝗦𝘄𝗶𝘁𝗰𝗵 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 💡 𝗧𝗵𝗲 𝗧𝗿𝗮𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 The switch statement has been part of C# since its early versions, allowing you to evaluate an expression against multiple case values and execute code blocks. Each case requires explicit break statements to prevent fall-through, and the syntax can become verbose with complex logic. It's perfect when you need multi-line statements or side effects per case. 🔥 𝗧𝗵𝗲 𝗠𝗼𝗱𝗲𝗿𝗻 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Switch expressions were introduced in C# 8 and remain the recommended approach in C# 14. They offer a concise, functional-style syntax using the => operator to assign values directly. The _ discard pattern serves as the default case, and the compiler enforces exhaustiveness, reducing bugs. ✅ While both the 𝘀𝘄𝗶𝘁𝗰𝗵 𝘀𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 and the 𝘀𝘄𝗶𝘁𝗰𝗵 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 are used for similar purposes, the switch expression offers more concise syntax and greater flexibility for pattern matching and value assignment, making it a more powerful tool for modern C# development. 🤔 Which one do you prefer? #csharp #dotnet #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
Switch expressions in .NET… Something basic. Something I use regularly. But this time it hit differently. It’s a reminder of how often we overcomplicate things. Nested ifs. Repeated checks. Logic spread across methods. Not because it’s required… but because we didn’t pause long enough to simplify. Switch expressions do one thing well: they force structure… and structure reveals truth. Either your logic is clear… or it isn’t. There’s no hiding behind “it works”. Good systems aren’t built on complexity. They’re built on clarity.
💡 𝗖#/.𝗡𝗘𝗧 - 𝗖𝗹𝗲𝗮𝗻 𝗖𝗼𝗱𝗲 𝗧𝗶𝗽 🔥 💎 𝗦𝘄𝗶𝘁𝗰𝗵 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 𝐯𝐬 𝗦𝘄𝗶𝘁𝗰𝗵 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 💡 𝗧𝗵𝗲 𝗧𝗿𝗮𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 The switch statement has been part of C# since its early versions, allowing you to evaluate an expression against multiple case values and execute code blocks. Each case requires explicit break statements to prevent fall-through, and the syntax can become verbose with complex logic. It's perfect when you need multi-line statements or side effects per case. 🔥 𝗧𝗵𝗲 𝗠𝗼𝗱𝗲𝗿𝗻 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Switch expressions were introduced in C# 8 and remain the recommended approach in C# 14. They offer a concise, functional-style syntax using the => operator to assign values directly. The _ discard pattern serves as the default case, and the compiler enforces exhaustiveness, reducing bugs. ✅ While both the 𝘀𝘄𝗶𝘁𝗰𝗵 𝘀𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 and the 𝘀𝘄𝗶𝘁𝗰𝗵 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 are used for similar purposes, the switch expression offers more concise syntax and greater flexibility for pattern matching and value assignment, making it a more powerful tool for modern C# development. 🤔 Which one do you prefer? #csharp #dotnet #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
.𝗡𝗲𝘁 𝟭𝟬 - 𝗖# 𝟭𝟰 𝗝𝘂𝘀𝘁 𝗠𝗮𝗱𝗲 𝗣𝗿𝗼𝗽𝗲𝗿𝘁𝗶𝗲𝘀 𝗖𝗹𝗲𝗮𝗻𝗲𝗿 - 𝗳𝗶𝗲𝗹𝗱 𝗸𝗲𝘆𝘄𝗼𝗿𝗱 In older C# code, if we wanted to add logic inside a property, we usually had to create a separate private variable. With C# 14, we can now use the 𝗳𝗶𝗲𝗹𝗱 keyword inside a property accessor. It gives direct access to the compiler generated backing field, so we can keep the code shorter and cleaner. 𝗕𝗲𝗳𝗼𝗿𝗲 public class Student { 𝗽𝗿𝗶𝘃𝗮𝘁𝗲 𝗶𝗻𝘁 _𝗺𝗮𝗿𝗸𝘀; public int Marks { get => _marks; set => _marks = value < 0 ? 0 : value; } } 𝗪𝗶𝘁𝗵 𝗳𝗶𝗲𝗹𝗱 𝗶𝗻 𝗖# 𝟭𝟰 public class Student { public int Marks { get; set => 𝗳𝗶𝗲𝗹𝗱 = value < 0 ? 0 : value; } } 𝗪𝗵𝗮𝘁 𝗰𝗵𝗮𝗻𝗴𝗲𝗱? We no longer need this line: private int _marks; The compiler creates the backing field for us, and field lets us use it directly inside the property accessor. 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗶𝘀 𝘂𝘀𝗲𝗳𝘂𝗹 • Cleaner code. • Less boilerplate. • Same control over validation or simple logic. #dotnet #csharp #softwaredevelopment #dotnetdev #programming
To view or add a comment, sign in
-
-
Built a simple C++ Compiler from Scratch! Sharing my latest project - a simple yet functional C++ compiler built from the ground up using Flex, Bison, and ANTLR4. This project dives into the core phases of compilation and helped me strengthen my understanding of how programming languages actually work behind the scenes. Key Features: • Lexical Analysis using Flex • Syntax Parsing using Bison & ANTLR4 • Three Address Code (TAC) Generation • Basic Assembly Code Generation • Detailed logging of each compilation phase How it works: 1. Source code is tokenized into meaningful units 2. Tokens are parsed based on grammar rules 3. Intermediate code (TAC) is generated 4. Final pseudo-assembly code is produced What I learned: Building a compiler from scratch gave me hands-on experience with: • Language design fundamentals • Parsing techniques and grammar rules (Bison & ANTLR4) • Intermediate representations • Low-level code generation concepts 🛠️ Tech Stack: C++ | Flex | Bison | ANTLR4 | Makefile 📌 Example outputs include: ✔ Token logs from lexical analysis ✔ Three Address Code ✔ Assembly-like instructions This project is a small but meaningful step into the world of compilers and systems programming. 🔗 GitHub Repo: https://lnkd.in/ggkcdTyV I’d love to hear your thoughts or suggestions for improvement! #CPlusPlus #CompilerDesign #Flex #Bison #ANTLR4 #SystemsProgramming #SoftwareEngineering #LearningByBuilding #BIE #bioinformatics #BAU
To view or add a comment, sign in
-
-
C15 Union Types: finally, compiler-checked “either/or” results 🚨 Tired of returning object (or building awkward base classes) just to say “this method returns A *or* B”? C15 brings a real solution: union types. ✨ What clicking gets: ✅ How `union` declares a closed set of case types (even unrelated ones) ✅ Implicit conversions so assignments stay clean ✅ Exhaustive pattern matching—the compiler forces every case to be handled (no `_` / `default` safety blanket) ✅ A clear roadmap for broader exhaustiveness in C# 🧠 Example vibe: define `Pet(Cat, Dog, Bird)` once… then `switch` with confidence knowing every case is covered. If discriminated unions were missed in C#, this is the most C#-native take yet. 🔥 https://lnkd.in/dsu5t_8M #csharp #dotnet #programming #patternmatching #compilers
To view or add a comment, sign in
-
-
🔹 Polymorphism in C++ — Approach 2 (Runtime Polymorphism) This video is part of my "C++ OOP & Class Relationships Explained" series 👇 It also connects to my Design Patterns in C++ playlist. 💡 The idea: Using inheritance and virtual functions to decide behavior at runtime. 🔥 Pros: >Very flexible >Enables working with different types via a base class >Ideal for large, scalable systems ⚠️ Cons: >Slightly slower due to virtual dispatch >Some optimizations (like inlining) may not apply 📌 Takeaway: This is the standard polymorphism approach in most real-world applications. 🎥 Watch here: https://lnkd.in/d9yw555P 📚 Design Patterns playlist: https://lnkd.in/dH73xkFW 📚 Full source code, UML diagrams, and notes are available on GitHub: https://lnkd.in/d7EmTwzr #cpp #oop #polymorphism #softwaredesign #designpatterns #programming
To view or add a comment, sign in
-
𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 𝗶𝗻 𝗔𝗦𝗣.𝗡𝗘𝗧 𝗖𝗼𝗿𝗲: 𝗧𝗿𝗮𝗻𝘀𝗶𝗲𝗻𝘁 𝘃𝘀 𝗦𝗰𝗼𝗽𝗲𝗱 𝘃𝘀 𝗦𝗶𝗻𝗴𝗹𝗲𝘁𝗼𝗻 If you’re using Dependency Injection in ASP.NET Core, you’ve probably asked: • When should I use Transient? • Why is Scoped so common for DbContext? • When is Singleton actually safe? The answer lies in service lifetimes and choosing the wrong one can lead to: • memory leaks • threading issues • hard-to-debug bugs 𝗜’𝘃𝗲 𝗽𝘂𝘁 𝘁𝗼𝗴𝗲𝘁𝗵𝗲𝗿 𝗮 𝘀𝗶𝗺𝗽𝗹𝗲 𝘃𝗶𝘀𝘂𝗮𝗹 𝗲𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻 𝘁𝗵𝗮𝘁 𝗯𝗿𝗲𝗮𝗸𝘀 𝗱𝗼𝘄𝗻: • what each lifetime really means • how long instances live • when to use each one in real projects 📌 𝗥𝗲𝗮𝗱 𝘁𝗵𝗲 𝗳𝘂𝗹𝗹 𝗮𝗿𝘁𝗶𝗰𝗹𝗲 𝗵𝗲𝗿𝗲: Understanding this early can save you hours of debugging later. Which service lifetime do you use the most in your projects Scoped, Singleton, or Transient? 𝗪𝗮𝗻𝘁 𝗺𝗼𝗿𝗲 𝗖# & 𝗔𝗦𝗣.𝗡𝗘𝗧 𝗖𝗼𝗿𝗲 𝘁𝗶𝗽𝘀 𝗹𝗶𝗸𝗲 𝘁𝗵𝗶𝘀? #dotnet #aspnetcore #csharp #dependencyinjection #softwaredevelopment #backend #programming
To view or add a comment, sign in
-
-
In embedded C++, DI looks simple right up to the moment the object graph starts growing. Then it stops being a pattern discussion and becomes a lifetime problem. People often say, “Just use constructor injection.” And yes, that works. But as the system grows, dependency injection stops being only about how dependencies are passed in. It becomes about who creates them, who owns them, how long they live, and how much wiring the design can absorb. I am talking mainly about embedded-style C++ codebases. Not template-heavy compile-time DI, but systems that value explicit object graphs, dynamic polymorphism, predictable behavior, and code that is easy to debug, review, and maintain. In that world, injecting a dependency is rarely just “pass it into the constructor.” You are also deciding whether the dependency is owned or borrowed, whether unique_ptr stays at the composition root or spreads through the graph, whether you need factories to keep construction testable, and whether shared_ptr appears simply because composition got painful. That is why DI often feels harder in C++ than in languages with framework-managed lifecycles. In Java with Spring, the container creates objects, resolves dependencies, and manages lifecycle. In C++, those decisions stay in your code. They are explicit, local, and impossible to ignore. That is both the strength and the cost. C++ gives us precise control over lifetime. What gets harder is scaling that control as the object graph grows. In small systems, explicit wiring feels clean. In larger ones, it can turn into plumbing. In practice, many embedded C++ codebases end up mixing approaches. Constructor injection for clear dependencies. References for borrowed collaborators. unique_ptr where ownership is real. Factory seams where tests need substitution. And only occasionally a container or custom injector when wiring becomes too large to manage by hand. So for me, DI in embedded C++ is not mainly about patterns. It is about how much lifetime and wiring complexity a design can carry before the composition model starts fighting the codebase. How do you handle this in real projects? Do you keep unique_ptr at the composition root? Inject factories for tests? Build your own injector? Or just accept some plumbing as the price of clarity? #EmbeddedSystems #Cpp #SoftwareEngineering #SoftwareArchitecture #EmbeddedSoftware
To view or add a comment, sign in
-
-
Design Patterns remain the key skill for software engineers across the board. We all have the GoF (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) original book on our desks, irrespective of which programming languages (#python, #cplusplus, #java, #rust, etc.) we use. We may think that we know everything about this subject but Dmitri Nesteruk's 2nd edition of Design Patterns in Modern C++ is definitely worth reading: https://amzn.to/4mshTWV if we want to produce correct, efficient, readable, and idiomatic/effective code.
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development