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
Source Generators in C# for Efficient Code
More Relevant Posts
-
Your compiler has been secretly capable of writing code for you this whole time. Source Generators are a Roslyn feature that run during compilation and inject code directly into your build - no reflection, no runtime overhead, no excuses. You write a partial class, the generator writes the other half. By the time your app runs, it's all just plain compiled C#. Spooky fast. Microsoft shipped them in .NET 5 (2020), mostly to fix the "why is JSON serialization slow" complaints. Spoiler: it was reflection. It's always reflection. --- // You write this: public partial class PizzaOrder { ... } // The generator writes this at compile time: public string Describe() => $"{Quantity}x {Topping} - your arteries called"; --- Try it yourself (no setup required): https://lnkd.in/emee3pA8 #dotnet #csharp #programming #roslyn
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
-
-
💻 Great .NET insights from Steven Giesel. This post dives into practical development concepts with clear explanations and real code examples—always a great resource for developers looking to sharpen their skills. 👉 https://lnkd.in/gp8vTSkr #dotnet #SoftwareDevelopment #Programming #DevCommunity
To view or add a comment, sign in
-
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
To view or add a comment, sign in
-
-
Most developers think: “If the code works… then it’s fine.” But in reality: Working code ≠ Good code. In this video, I walk through a real .NET scenario: - Fetching users by department - Building a report using LINQ - Returning the result as a file Then I show: ❌ A version that works but has hidden problems ✔️ A corrected version using best practices We cover: - IEnumerable vs IQueryable - Why ToList() placement matters - String vs StringBuilder - Filtering in memory vs database - Clean and scalable code structure 💡 Key takeaway: Your code might work today… but bad practices will hurt you when your data grows. Watch the video In comments and see the difference yourself 👇 #dotnet #csharp #webapi #backend #softwareengineering #cleanCode #programming
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
-
-
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
-
-
🚀 C# 15 Union Types: Eliminating Type Complexity in .NET 11 Microsoft's latest breakthrough simplifies complex type scenarios by eliminating manual unwrapping. Union Types in C# 15 make code more readable and reliable. Key challenges solved: • Complex API responses requiring multiple return types • Conditional logic with multiple failure modes • Validation results handling in applications • Database query results with mixed outcomes Why this matters: • Compile-time exhaustiveness - Missing a case? That's a compile error. • Zero-cost performance - Stored just like regular types. • Self-documenting code - The type signature tells the story. • Type-safe patterns - No more object casting nightmares. Real-World Example: Before: object result = GetValue(); if (result is string str) { ... } else if (result is int num) { ... } Now: var result = GetValue(); // returns string | int switch (result) { case string str: ... case int num: ... } Adoption details: - Available in .NET 11 Preview 2 - Requires Visual Studio 2026 Insider or later - Production ready with next month's final release The compiler ensures you handle all cases perfectly. 🔗 Practical implications for every .NET developer #CSharp15 #DotNet11 #Programming #SoftwareEngineering
To view or add a comment, sign in
-
.NET 11 Preview 3 just dropped last week. And it's a big one. Here's everything you need to know. 𝗖# 𝟭𝟱 𝗨𝗻𝗶𝗼𝗻 𝗧𝘆𝗽𝗲𝘀: The most requested feature in C# history is now available. Declare a closed set of types with exhaustive pattern matching. Compiler prevents you from forgetting a case. 𝗥𝘂𝗻𝘁𝗶𝗺𝗲 𝗔𝘀𝘆𝗻𝗰 𝗩𝟮: Live stack traces now show actual async method names instead of state machine gibberish. Profilers and debuggers finally see what you see. 𝗙𝗶𝗹𝗲-𝗕𝗮𝘀𝗲𝗱 𝗔𝗽𝗽𝘀 — 𝗠𝘂𝗹𝘁𝗶-𝗙𝗶𝗹𝗲: Single-file apps from .NET 10 can now span multiple files. Your script-like C# project can grow without converting to a full .csproj. 𝗝𝗜𝗧 𝗕𝗼𝘂𝗻𝗱𝘀 𝗖𝗵𝗲𝗰𝗸 𝗘𝗹𝗶𝗺𝗶𝗻𝗮𝘁𝗶𝗼𝗻: RyuJIT now eliminates redundant bounds checks on consecutive index-from-end access and i + constant patterns. Less branching in tight loops. 𝗥𝗲𝗴𝗲𝘅 𝗔𝗻𝘆𝗡𝗲𝘄𝗟𝗶𝗻𝗲: New RegexOptions.AnyNewLine flag recognizes every Unicode newline sequence. Including \r\n as atomic. 𝗘𝗙 𝗖𝗼𝗿𝗲 𝗢𝗻𝗲-𝗦𝘁𝗲𝗽 𝗠𝗶𝗴𝗿𝗮𝘁𝗶𝗼𝗻𝘀: dotnet ef database update --add now scaffolds AND applies a migration in one command. Perfect for containers and .NET Aspire. 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 𝗔𝗿𝗴𝘂𝗺𝗲𝗻𝘁𝘀: Pass capacity, comparers, and config inline with collection expressions using with() syntax. The timeline: • Preview 4-7 → May to August • Release Candidate → September-October • GA → November 2026 (STS, 24 months support) .NET 11 is shaping up to be one of the most significant releases since .NET 5 unified the platform. Which Preview 3 feature are you most excited about? #dotnet #dotnet11 #csharp15 #preview #programming #softwareengineering
To view or add a comment, sign in
-
-
📘 C# 15, Union Types, and the ApiResult Monad C# continues to evolve by adopting ideas long proven in functional programming. In my latest article, I explore how C# 15 union types enable cleaner domain modeling and how they naturally support a composable ApiResult monad for explicit, type‑safe error handling. Union types are drafted and for the upcoming .NET 11 SDK release in November this year as part of C# 15 and will make a lot of the earlier attempts to do more and more functional programming in C# a lot easier and is a long awaited feature of C# to use it as a functional programming language. Union types - They are still available in .NET 11 Update 2 and most likely will ship with .NET 11 final release version (RTM) The article walks through: - Discriminated unions as a first‑class C# feature - Called union types in C# 🎯 - Eliminating null and exception‑driven control flow - Building composable APIs with monadic Map / Bind semantics shown in the ApiResult monad - If you’re interested in type safety, exhaustiveness, and functional design in modern .NET, this is worth a look. 🔗 Read the article here : https://lnkd.in/esqbFEKG 🧠 Functional ideas, now first‑class citizens in C#. Github repo links to read the code is also shown in the article #csharp #dotnet11 #csharp15 #functionalprogramming #monads
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