You are still writing five-level if/else blocks to handle types and conditions. C# 8 brought switch expressions with property patterns, and they have been sitting there ever since, quietly judging you. Instead of checking properties one by one, you match against the whole shape of an object in one clean expression. --- var price = order switch { { Drink: "Espresso", ExtraShot: true } => 4.50, { Drink: "Latte", Size: <= 12 } => 3.75, { Size: >= 20 } => 6.00, _ => 2.50 }; --- Shipped in C# 8 (2019) and quietly upgraded every version since - relational patterns, nested patterns, you name it. Microsoft kept cooking on this one. Run it, break it, learn it: https://lnkd.in/eqSfeq5T #dotnet #csharp #programming #cleancode
C# switch expressions with property patterns for cleaner code
More Relevant Posts
-
Source Generators let the compiler write your boilerplate at build time - real C# files, zero reflection, zero runtime cost. They inspect your code during compilation and emit new source files that get compiled right alongside yours. It's like having an intern who only writes the tedious parts and never asks for a code review. // You write the model. The generator writes the rest: public partial class PizzaOrder { public string Topping { get; set; } } // This partial was never typed by a human: public override string ToString() => $"PizzaOrder {{ Topping = {Topping}, Qty = {Quantity}, Price = {Price:C} }}"; Microsoft introduced them in .NET 5 / C# 9 (2020) to kill reflection-based slowness and make things like `System.Text.Json` actually fast. Writing code that writes code - still feels like cheating, honestly. See it in action: https://lnkd.in/eb8S_rXV #dotnet #csharp #programming #sourcegenerators
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
-
-
Your API returns 10,000 rows and you're loading them into a `List<T>` before doing anything. Bold strategy. `IAsyncEnumerable<T>` lets you stream data asynchronously, processing each item as it arrives instead of waiting for the whole payload. It pairs with `await foreach` and it just works. --- await foreach (var tick in GetLiveTicker()) Console.WriteLine($"${tick.Symbol}: {tick.Price:F2}"); --- Shipped in C# 8 and .NET Core 3.0 back in 2019, it quietly solved the "why is my app eating 2GB of RAM" problem for a lot of teams. Run it, break it, learn it: https://lnkd.in/e5jMbUvr #dotnet #csharp #programming #asyncprogramming
To view or add a comment, sign in
-
-
💡𝐂#/.𝐍𝐄𝐓 𝐀𝐬𝐲𝐧𝐜 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐓𝐢𝐩 🚀 💎𝐇𝐨𝐰 𝐚𝐧𝐝 𝐰𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 ‘𝐚𝐬𝐲𝐧𝐜’ 𝐚𝐧𝐝 ‘𝐚𝐰𝐚𝐢𝐭’ 💡 '𝐚𝐬𝐲𝐧𝐜' and '𝐚𝐰𝐚𝐢𝐭' keywords introduced in C# 5.0 were designed to make it easier to write asynchronous code, which can run in the background while other code is executing. The "async" keyword marks a method asynchronous, meaning it can be run in the background while another code executes. ⚡ When using 𝐚𝐬𝐲𝐧𝐜 and 𝐚𝐰𝐚𝐢𝐭 the compiler generates a state machine in the background. 🔥 Let's look at the other high-level details in the example; 🔸 𝐓𝐚𝐬𝐤<𝐢𝐧𝐭> 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 = 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐀𝐬𝐲𝐧𝐜(); starts executing 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧. 🔸 Independent work is done on let's assume the Main Thread (Thread ID = 1) then 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is reached. 🔸 Now, if the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 hasn't finished and it is still running, 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() will return to its calling method, this the main thread doesn't get blocked. When the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is done then a thread from the ThreadPool (can be any thread) will return to 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() in its previous context and continue execution (in this case printing the result to the console). ✅ A second case would be that the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 has already finished its execution and the result is available. When reaching the 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 we already have the result so the code will continue executing on the very same thread. (in this case printing result to console). Of course this is not the case for in the example, where there's a 𝐓𝐚𝐬𝐤.𝐃𝐞𝐥𝐚𝐲(1000) involved. 🎯 𝐖𝐡𝐚𝐭 𝐝𝐨 𝐲𝐨𝐮 𝐭𝐡𝐢𝐧𝐤 𝐚𝐛𝐨𝐮𝐭 𝐚𝐬𝐲𝐧𝐜 𝐨𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬? #csharp #dotnet #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
💡𝐂#/.𝐍𝐄𝐓 𝐀𝐬𝐲𝐧𝐜 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐓𝐢𝐩 🚀 💎𝐇𝐨𝐰 𝐚𝐧𝐝 𝐰𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 ‘𝐚𝐬𝐲𝐧𝐜’ 𝐚𝐧𝐝 ‘𝐚𝐰𝐚𝐢𝐭’ 💡 '𝐚𝐬𝐲𝐧𝐜' and '𝐚𝐰𝐚𝐢𝐭' keywords introduced in C# 5.0 were designed to make it easier to write asynchronous code, which can run in the background while other code is executing. The "async" keyword marks a method asynchronous, meaning it can be run in the background while another code executes. ⚡ When using 𝐚𝐬𝐲𝐧𝐜 and 𝐚𝐰𝐚𝐢𝐭 the compiler generates a state machine in the background. 🔥 Let's look at the other high-level details in the example; 🔸 𝐓𝐚𝐬𝐤<𝐢𝐧𝐭> 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 = 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐀𝐬𝐲𝐧𝐜(); starts executing 𝐋𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧. 🔸 Independent work is done on let's assume the Main Thread (Thread ID = 1) then 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is reached. 🔸 Now, if the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 hasn't finished and it is still running, 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() will return to its calling method, this the main thread doesn't get blocked. When the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 is done then a thread from the ThreadPool (can be any thread) will return to 𝐃𝐨𝐒𝐨𝐦𝐞𝐭𝐡𝐢𝐧𝐠𝐀𝐬𝐲𝐧𝐜() in its previous context and continue execution (in this case printing the result to the console). ✅ A second case would be that the 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 has already finished its execution and the result is available. When reaching the 𝐚𝐰𝐚𝐢𝐭 𝐥𝐨𝐧𝐠𝐑𝐮𝐧𝐧𝐢𝐧𝐠𝐓𝐚𝐬𝐤 we already have the result so the code will continue executing on the very same thread. (in this case printing result to console). Of course this is not the case for in the example, where there's a 𝐓𝐚𝐬𝐤.𝐃𝐞𝐥𝐚𝐲(1000) involved. 🎯 𝐖𝐡𝐚𝐭 𝐝𝐨 𝐲𝐨𝐮 𝐭𝐡𝐢𝐧𝐤 𝐚𝐛𝐨𝐮𝐭 𝐚𝐬𝐲𝐧𝐜 𝐨𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬? #csharp #dotnet #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
Your regex runs on every request. It also recompiles on every request - unless you do something about it. `Regex.IsMatch` handles the quick yes/no check. Named groups let you pull out values by name instead of counting parentheses like it's 1999. And `[GeneratedRegex]`, shipped in .NET 7, compiles your pattern straight to IL at build time - zero runtime overhead, and your profiler finally stops yelling at you. --- var match = Regex.Match(log, @"user=(?<email>[\w.]+@[\w.]+)\s+action=(?<action>\S+)"); Console.WriteLine(match.Groups["email"].Value); // no index guessing --- Microsoft made regex fast by default. Only took until 2022. Try it yourself (no setup required): https://lnkd.in/eg68aQiZ #dotnet #csharp #programming #regex
To view or add a comment, sign in
-
-
⚔️ LINQ vs Loops — Same Result, Different Thinking Both approaches work. But they communicate very differently 👇 🧵 Loops (Imperative Style) var result = new List<string>(); foreach (var item in list) { if (item.IsActive) result.Add(item.Name); } • Step-by-step instructions 🛠️ • Focus on how • More room for mistakes ⚡ LINQ (Declarative Style) var result = list .Where(x => x.IsActive) .Select(x => x.Name); • Express intent 🎯 • Focus on what • Faster, cleaner & composable 🧠 The Real Difference Loops → execution thinking LINQ → intent thinking 💡 Code is read more than it’s written. LINQ optimizes for clarity. 🚀 Final Thought Loops tell the computer what to do. LINQ tells humans what you mean. And in real-world systems, clarity is what scales. #DotNet #CSharp #LINQ #Programming #SoftwareEngineering #CleanCode
To view or add a comment, sign in
-
💡 𝗖#/.𝗡𝗘𝗧 - 𝐑𝐞𝐭𝐮𝐫𝐧 𝐓𝐲𝐩𝐞 𝗧𝗶𝗽 🔥 💎 𝗥𝗲𝘁𝘂𝗿𝗻 𝗮𝗻 𝗲𝗺𝗽𝘁𝘆 𝗰𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻 𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝗼𝗳 𝗻𝘂𝗹𝗹 💡 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗺𝗮𝘁𝘁𝗲𝗿𝘀 Returning null from collection methods forces callers to add defensive null checks before every iteration. This creates code clutter and opens the door to NullReferenceException when those checks are forgotten. ✅ 𝗧𝗵𝗲 𝗯𝗲𝘁𝘁𝗲𝗿 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Return an empty collection instead. It's memory-efficient (singleton instance), allows safe iteration without null checks, and follows Microsoft's official Framework Design Guidelines for .NET 10. 🔥 𝗕𝗲𝘀𝘁 𝗽𝗿𝗮𝗰𝘁𝗶𝗰𝗲𝘀 𝗶𝗻 .𝗡𝗘𝗧 𝟭𝟬 ◾ For IEnumerable: Enumerable.Empty<T>() or []. ◾ For arrays: Array.Empty<T>() or [] ◾ For lists: new List<T>(). ◾ Modern C# 12+: Use collection expressions [] (IDE0301 analyzer recommends this). 🤔 𝗪𝗵𝗶𝗰𝗵 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 𝗱𝗼 𝘆𝗼𝘂 𝘂𝘀𝗲? #csharp #dotnet #programming #softwareengineering #softwaredevelopment
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
-
-
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
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