Reflection in .NET carries a performance stigma, but newer versions may change that perception. Caching and precomputation can significantly reduce the performance gap in sensitive contexts. The results suggest reflection can be a reasonable choice when clarity and maintainability outweigh minor speed losses. This isn’t a green light for overuse, but a reminder that assumptions about performance often don’t hold when newer runtime optimizations are in play. Code clarity and long-term maintenance can benefit when patterns are chosen with actual data, not folklore. Choosing the right tool means balancing measurable facts with team understanding — not just code efficiency. #CSharp #DotNet #Programming #SoftwareDevelopment
Himanshu C.’s Post
More Relevant Posts
-
𝗠𝗼𝘀𝘁 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀 𝘂𝘀𝗲 𝗖#... But very few actually understand 𝘄𝗵𝗮𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝗮𝗳𝘁𝗲𝗿 𝗵𝗶𝘁𝘁𝗶𝗻𝗴 𝗥𝗨𝗡. This visual breaks it down step by step: • How C# code is compiled into IL • How the .NET runtime (CLR) manages execution • How JIT converts it into machine code If you’ve ever been 𝗰𝗼𝗻𝗳𝘂𝘀𝗲𝗱 about: – how your code actually runs – what CLR really does – or why performance behaves the way it does This will make things much clearer. Understanding this isn’t just theory — it helps you write better, faster, and more reliable code. 𝗪𝗵𝗮𝘁 𝗽𝗮𝗿𝘁 𝗼𝗳 𝗖# 𝗼𝗿 .𝗡𝗘𝗧 𝗱𝗼 𝘆𝗼𝘂 𝗳𝗶𝗻𝗱 𝗵𝗮𝗿𝗱𝗲𝘀𝘁 𝘁𝗼 𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱? #csharp #dotnet #programming #softwaredevelopment #backenddevelopment #developers #aspnetcore #fullstackdeveloper
To view or add a comment, sign in
-
-
Dependency Injection sounds advanced... but the idea is simple. Instead of creating objects manually, .NET creates and gives them when needed. Here’s what happens: 1️⃣ Service is registered 2️⃣ .NET stores it in container 3️⃣ Class asks for dependency in constructor 4️⃣ .NET provides the required object 5️⃣ App stays clean and easy to manage Why DI is powerful: ✅ Clean code ✅ Easy testing ✅ Loose coupling ✅ Better project structure If you use ASP.NET Core, you are already using DI. Do you use Scoped, Singleton, or Transient most? #dotnet #dependencyinjection #aspnetcore #csharp #backenddeveloper #softwaredeveloper #webapi #developer #coding #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
-
-
Why does it matter? .Count() — as an extension method on IEnumerable<T> — must walk the entire sequence to return a number. Even if the list has 10,000 elements, it checks all of them just to tell you "yes, there's at least one." .Any() short-circuits. It returns true the moment it finds the first element. On large collections or deferred LINQ queries, this is a meaningful performance gain. The nuance worth knowing: → If you're working with a concrete List<T> or array, .Count (the property, not the method) is O(1) — that's perfectly fine. → The trap is calling .Count() (the method) on an IEnumerable, which triggers full enumeration. → .Any(predicate) is also cleaner than .Where(...).Any() — same result, one less pass. #dotnet #csharp #linq #dotnetdeveloper #programming #cleancode #performancetips
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
-
-
🔥 Dependency Injection (DI) Clean architecture starts with loose coupling 🔌 DI helps inject dependencies instead of creating them. • Improves testability • Enables scalability • Built-in in .NET Core 👉 Constructor Injection or Method Injection — what do you prefer? #DotNet #DependencyInjection #CleanCode #Architecture #Backend #CSharp #Developers #Programming #Tech #Learning
To view or add a comment, sign in
-
-
C#/.NET Performance Tip - String vs Char Usage Many .NET developers may not be aware of a significant performance difference: Using StartsWith("s") is 5 times slower than using StartsWith('s'). When a string is passed, .NET must: → Allocate a string object → Compare character by character with culture rules In contrast, when a char is passed, .NET: → Directly compares the first character → Involves zero allocation and zero overhead This performance difference applies to: • StartsWith • EndsWith • Contains • IndexOf A small change can lead to a massive impact at scale. Benchmark results show: 1.008 ns (char) vs 5.340 ns (string). Keep this in mind for your next code ! #csharp #dotnet #performance #programming #tips #coding #softwaredevelopment #aspnetcore #developer #cleancode
To view or add a comment, sign in
-
-
🚀 Clean Code is More Important Than Clever Code Writing complex code that only you understand isn’t impressive. Writing clean code that your entire team can maintain? That’s real engineering. ❌ Clever code: 🔵 Short but confusing 🔵Too much abstraction 🔵Hard to debug 🔵Difficult for teams to maintain ✅ Clean code: ✔ Readable ✔ Predictable ✔ Simple to modify ✔ Easy to test 💡 In ASP.NET Core projects, this means: 🔵Thin controllers 🔵Proper service separation 🔵Meaningful method names 🔵Consistent API responses 🔵Clear project structure ⚡ Reality: Code is written once… but read hundreds of times. Optimize for readability, not ego. 💬 What matters more to you — clever code or maintainable code? #ASPNETCore #CleanCode #SoftwareEngineering #DotNet #BackendDevelopment #Programming
To view or add a comment, sign in
-
-
🚀 C# Switch Pattern Matching – Cleaner & Smarter Code Modern C# has completely transformed the way we use switch. No more long if-else chains. No more unreadable conditional blocks. With switch pattern matching, you can write expressive, concise, and powerful logic. 🔹 Example: public static string GetDiscount(decimal amount) => amount switch { < 1000 => "No Discount", >= 1000 and < 5000 => "10% Discount", >= 5000 => "20% Discount", _ => "Invalid" }; 💡 Why it’s powerful: ✔ Relational patterns (<, >=) ✔ Logical patterns (and, or, not) ✔ Type patterns ✔ Property patterns ✔ Much cleaner than traditional switch-case Modern C# isn’t just evolving — it’s becoming more expressive and developer-friendly. If you're working with .NET, mastering pattern matching is a must. #dotnet #csharp #programming #softwaredevelopment #backend #developers
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
Have been using a home-brewed ReflectionCache since 2010.