💡 C# Tip: Global Usings — Stop Repeating the Same using Statements In many C# projects, the same 5–10 namespaces appear at the top of nearly every file. using System; using System.Collections.Generic; using System.Linq; It works, but it creates boilerplate and visual noise. Since C# 10 (.NET 6+), we have a cleaner solution: 👉 Global Usings 📌 What Are Global Usings? Global usings allow you to declare a namespace once and make it available across the entire project. Instead of repeating imports in every file, you write them one time. Example: global using System; global using System.Collections.Generic; global using System.Linq; The compiler treats them as if they exist at the top of every file in the project. 📂 How to Use Them Create a dedicated file, commonly named: 📄 GlobalUsings.cs Add your global imports there: global using System; global using System.Linq; global using System.Collections.Generic; global using MyCompany.MyProject.Domain; Now those namespaces are automatically available everywhere. ⚡ Why This Matters In projects with 50+ files, global usings: ✔ Reduce repetitive boilerplate ✔ Make files cleaner and easier to read ✔ Centralize common dependencies ✔ Improve consistency across the codebase ⚠ The Trap to Avoid Do not turn global usings into a dumping ground. ❌ Adding everything globally makes dependencies unclear. Best practice: 🔹 Use global usings only for very common namespaces 🔹 Keep rare namespaces local 🔹 Maintain a single GlobalUsings.cs file for discoverability 📌 Best Practices ✨ Use global using Namespace; for shared namespaces 📂 Keep them in a dedicated file like GlobalUsings.cs 📊 Apply only to namespaces used in most files 🧩 Local using statements can still be used when needed 💡 Final Thought Global usings are a small feature with a big readability impact in large .NET projects. Cleaner files → easier navigation → better maintainability. #CSharp #DotNet #ASPNetCore #CleanCode #SoftwareEngineering #BackendDevelopment #Programming 🚀
C# Tip: Simplify Global Usings in .NET 6
More Relevant Posts
-
Most developers think LINQ is just cleaner syntax. It’s not. LINQ is an execution pipeline — and if you don’t understand how it works, your elegant one-liners can silently turn into performance bottlenecks in production. 💬 Let’s be honest: What’s the worst LINQ misuse you’ve seen in real systems? 📩 I break down System Design, Backend Performance, .NET internals, and real-world engineering patterns: 🔔 Subscribe: https://lnkd.in/gi3YVNBg 🌐 Deep dives: https://lnkd.in/eJE6DWrq #dotnet #csharp #linq #softwareengineering #backenddevelopment #systemdesign #performance #cleancode #programming #developers #unpopularopinion
To view or add a comment, sign in
-
🚀 Spring Boot WebFlux & Reactive Programming — Key Concepts Explained Modern applications must handle thousands of concurrent users while remaining responsive and efficient. Traditional thread-per-request architectures quickly reach their limits. This is where Spring WebFlux and Reactive Programming come in. I created this infographic to summarize the most important concepts developers should understand when working with WebFlux and Project Reactor. 🔎 Key ideas covered: • Non-blocking architecture for highly scalable APIs • The two fundamental reactive types: Mono and Flux • Building reactive pipelines using operators like map, filter, and flatMap • Backpressure to control data flow between producers and consumers • Thread management with Schedulers • Understanding publishOn vs subscribeOn • Reactive error handling strategies Reactive programming changes the way we design backend systems. Instead of blocking threads while waiting for I/O operations, applications remain responsive and can handle far more concurrent requests with fewer resources. 👇 I hope this visual summary helps developers understand the core principles of WebFlux faster. What’s your experience with Reactive Programming in Spring Boot? #SpringBoot #WebFlux #ReactiveProgramming #Java #BackendDevelopment #SoftwareEngineering #Microservices
To view or add a comment, sign in
-
-
🚀 Understanding Dependency Injection (DI) in C# Dependency Injection is one of the most important design patterns every .NET developer should know. Instead of creating dependencies inside a class, DI allows you to inject them from outside — making your code cleaner, flexible, and easier to manage. 👉 Why use Dependency Injection? ✔️ Promotes loose coupling between components ✔️ Makes unit testing easier with mocking ✔️ Improves code maintainability ✔️ Enhances scalability and flexibility 💡 In modern .NET applications, DI is built-in and widely used with services like: Transient Scoped Singleton Once you start using DI properly, you'll notice your code becoming more modular and easier to extend. 👨💻 Clean architecture starts with better dependency management! #csharp #dotnet #dependencyinjection #softwarearchitecture #programming #coding #deepakmalra
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
-
Ever Wonder What Happens After You Run Your .NET Code? 🤔 👉 I like explaining this in a simple way for beginners because once you understand how CLR works, .NET becomes much easier to learn and debug. When you hit Run, your code doesn’t directly execute as machine code. First, your C# (or VB.NET / F#) code gets compiled into something called CIL (Common Intermediate Language), which is kind of like an intermediate step. Then comes the real hero 👉 CLR (Common Language Runtime) CLR takes care of everything behind the scenes: ✔️ Loads your code (Class Loader) ✔️ Converts CIL → Native Machine Code using JIT Compiler ✔️ Manages memory with Garbage Collection ✔️ Handles security, exceptions, and performance Finally, your app runs smoothly on your system 💻 That’s why .NET is so powerful; you focus on writing code, and CLR handles the heavy lifting. Simple flow: Source Code → CIL → CLR → Native Code → Application Running Once you understand this, debugging and performance tuning make way more sense. #DotNet #CLR #CSharp #ASPNetCore #WebAPI #BackendDeveloper #FullStackDeveloper #SoftwareEngineering #Programming #Coding #Developers #TechCommunity #LearnToCode #ProgrammingBasics #CodeNewbie #BeginnersGuide #TechForBeginners #SystemDesign #ApplicationArchitecture #Microservices #CleanArchitecture #JIT #GarbageCollection #Runtime #DotNetCore #MicrosoftTech #Azure #CloudComputing #DevCommunity #CodingLife #TechExplained
To view or add a comment, sign in
-
-
💡 𝐂# 𝐓𝐢𝐩: 𝐢𝐬 𝐯𝐬 𝐚𝐬 - 𝐤𝐧𝐨𝐰 𝐰𝐡𝐢𝐜𝐡 𝐨𝐧𝐞 𝐭𝐨 𝐮𝐬𝐞 𝐚𝐧𝐝 𝐰𝐡𝐞𝐧 Type checking and casting in C# looks simple on the surface. Most developers use is and as interchangeably without thinking twice. That's where bugs and unnecessary allocations sneak in. 🔹 What is is? is checks whether an object is compatible with a given type. It returns a boolean. Since C# 7, it also supports pattern matching - so you can check and bind in one step with is SomeType variable. 🔹 What is as? as attempts a cast and returns null if it fails. It never throws. It only works with reference types and nullable value types - you cannot use it with non-nullable value types like int or struct. 🔹 When does this matter? Using as without a null check after is a silent bug waiting to happen. You get a null reference instead of an exception, and the failure surfaces somewhere completely unrelated. Using is with pattern matching eliminates this class of bugs entirely. 🔹 The trap The old pattern - as followed by a null check - still works, but it's verbose and easy to forget the check. The double-cast pattern - is check followed by a separate explicit cast - is worse: you're doing the type check twice. ✦ Use is with pattern matching (is SomeType t) as the default - it's one operation, null-safe, and scope-limited ✦ Use as only when you explicitly want to handle a failed cast as null rather than a mismatch ✦ Never use is check followed by a direct cast (T)obj - that's two type checks and an exception risk if something changes ✦ as cannot be used with non-nullable value types - the compiler will stop you, but know why ✦ In switch expressions and switch statements, prefer type patterns over manual is/as chains ♻️ Repost to help your team write safer type-checking code! 📌 Save this for the next time you're reviewing a PR with a suspicious cast Subscribe to my weekly .NET newsletter 🚀 https://lnkd.in/drjE3rjP Follow Remigiusz Zalewski for more daily .NET tips 👇 #csharp #dotnet #aspnetcore #softwaredevelopment #programming #coding
To view or add a comment, sign in
-
-
🧠 record vs class in C# — do you know when to use which? This trips up a lot of devs, especially when moving to modern .NET. ✅ Use record when your type is data — DTOs, API responses, value objects. You get value equality, immutability, and with expressions for free. ✅ Use class when your type has behavior — services, repositories, domain logic. Full OOP, mutable state, dependency injection. The simple rule: If it carries data → record If it does work → class I switched all my API response models to records in our .NET 8 project and it cleaned up so much comparison and mapping code. What's your pattern? Drop it below 👇 #CSharp #DotNet #SoftwareEngineering #CleanCode #100DaysOfCode
To view or add a comment, sign in
-
-
Understanding the difference between Value Types and Reference Types in C# is essential for writing efficient and bug-free code. Value Types: - Store the actual data directly in memory (stack). - When assigned to another variable, a copy is created. - Example: int a = 10; int b = a; b = 20; - Result: a = 10 b = 20 - Each variable is independent. - Faster access due to stack allocation. Reference Types: - Store a reference (pointer) to the data in memory (heap). - When assigned, both variables point to the same object. - Example: var list1 = new List { 1, 2, 3 }; var list2 = list1; list2.Add(4); - Result: list1.Count = 4 list2.Count = 4 - Both variables share the same data. - Changes affect all references. Key takeaway: If you see “unexpected changes” in your data, chances are you're dealing with reference types. Mastering this concept helps you avoid side effects, especially when working with collections, APIs, and complex objects. #csharp #dotnet #softwareengineering #programming #developers #codingtips #backend #fullstack
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
-
More from this author
Explore related topics
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