💥 Stop the NullReferenceException — C# Nullable Reference Types NullReferenceException is the #1 runtime crash in C#. And 99% of the time — it's completely preventable. Here's how to eliminate it 💥 The Problem string name = GetName(); // could be null int len = name.Length; // 💥 CRASH at runtime You won't know it's null until production blows up. ✅ Enable Nullable in C# 8+ Add this to your .csproj: <Nullable>enable</Nullable> Now the compiler warns you before the crash happens. Bugs caught at compile time, not runtime. ❓ string? vs string string name = "Ahmad"; // guaranteed non-null ✅ string? name = null; // explicitly says "this can be null" 🛡️ Null-Safe Operators — Use These Daily user?.Name // null if user is null — no crash name ?? "Anonymous" // fallback if null name ??= "Default" // assign only if null ⚠️ Null Forgiving Operator — Handle with Care string name = GetName()!; // tells compiler "trust me" Use this ONLY when you are 100% certain the value is not null. Otherwise you're just hiding the problem. 💡 Enabling nullable annotations is one of the easiest wins in any C# codebase. It takes 5 minutes to enable and saves hours of debugging. Have you enabled nullable in your projects? #CSharp #DotNet #BackendDevelopment #SoftwareEngineering #CleanCode #Programming #ASPNET
Prevent NullReferenceException in C# with Nullable Reference Types
More Relevant Posts
-
If you're still writing if-else chains in C#, you're missing one of the best features added in the last 5 years. Pattern matching with switch expressions makes your code shorter, safer, and faster. Here's the evolution: 𝗦𝘁𝗮𝗴𝗲 𝟭 — Old way (if-else): → 15 lines of nested if-else → Easy to forget a case → Hard to read 𝗦𝘁𝗮𝗴𝗲 𝟮 — Classic switch: → Better than if-else → Still verbose → No way to return values directly 𝗦𝘁𝗮𝗴𝗲 𝟯 — Switch expression: → Single expression → Returns a value → Compiler enforces exhaustiveness → Pattern matching on types, properties, and tuples Real example I use all the time: Instead of this: → if order is null → throw exception → else if order.Status == "Pending" → process → else if order.Status == "Cancelled" → reject → else → log warning You write: var result = order switch { null => throw new ArgumentNullException(), { Status: "Pending" } => Process(order), { Status: "Cancelled" } => Reject(order), _ => LogWarning(order) }; 3 lines instead of 15. Same logic. Clearer intent. What pattern matching unlocks: • Property patterns — match on object properties • Tuple patterns — match on multiple values at once • Relational patterns — use >, <, >= directly in switches • List patterns — match on collections (C# 11+) • Type patterns — combined with deconstruction The compiler also warns you when you miss a case. Your bugs become impossible to ship. If your codebase is full of long if-else chains, you have an easy refactoring win waiting. What C# feature changed how you write code? #dotnet #csharp #patternmatching #cleancode #programming #softwareengineering
To view or add a comment, sign in
-
-
#Day-5 of 30 Days/30 Posts challenge. 👉 upgrading the C++ advanced concepts.... 💢 Understanding the nuances of polymorphism is what separates a good C++ developer from a great one. While both Function Overloading and Function Overriding allow for multiple behaviors under the same function name, they serve entirely different purposes in system design. 🔹 Here is a breakdown of how to distinguish and master these two pillars of C++. 1. Function Overloading (Static Polymorphism) Overloading allows you to define multiple functions with the same name in the same scope, provided they have different signatures (parameters). It’s about flexibility in inputs. ▫️ When it happens: Compile-time. The Rule: Functions must differ by the number or type of arguments. Example: Having multiple log() functions—one for std::string and another for int error codes. 2. Function Overriding (Dynamic Polymorphism) Overriding occurs when a derived class provides a specific implementation for a virtual function declared in a base class. It’s about flexibility in behavior. ▫️ When it happens: Runtime. The Rule: The function signature must be identical to the base class version. The Modern Standard: Since C++11, always use the override keyword. It tells the compiler to double-check that you are actually overriding a base function, preventing silent errors if the signatures drift. Key Comparison : Feature FunctionOverloading FunctionOverriding ▫️ Binding Static (Early Binding) Dynamic (Late Binding) ▫️ Scope Same class or scope Base and Derived classes ▫️ Inheritance Not required Mandatory ▫️ Signature Must be different Must be identical Pro-Tip for Scalable Architecture: Overriding is essential for the Open/Closed Principle. By defining a common interface in a base class and overriding it in specialized versions, you can add new functionality to your system without modifying existing code that relies on the base pointer. In high-performance or financial systems, remember that overriding comes with the small overhead of a virtual table (vtable) lookup. If performance is extremely critical and the types are known at compile-time, consider templates (CRTP) as a static alternative to overriding. Which one do you find most critical in your current projects? Let's discuss in the comments! 👇 #CPlusPlus #SoftwareEngineering #ProgrammingTips #OOP #ModernCPP #CodingStandards #TechLeadership
To view or add a comment, sign in
-
-
C++26 will introduce 𝗿𝗲𝗳𝗹𝗲𝗰𝘁𝗶𝗼𝗻 and we are all very excited about it. But there currently exist many 𝗹𝗶𝗯𝗿𝗮𝗿𝗶𝗲𝘀 that offer reflection for C++, how is that possible? Most of them use the __𝗣𝗥𝗘𝗧𝗧𝗬_𝗙𝗨𝗡𝗖𝗧𝗜𝗢𝗡__ compiler trick. This trick has been around for quite a while in modern compilers like GCC, Clang and MSVC and it’s very useful for this situation. It basically prints a string with 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝘀𝗶𝗴𝗻𝗮𝘁𝘂𝗿𝗲 information at compile time. With some 𝘁𝗲𝗺𝗽𝗹𝗮𝘁𝗲 𝗺𝗲𝘁𝗮𝗽𝗿𝗼𝗴𝗿𝗮𝗮𝗺𝗶𝗻𝗴 techniques (like really heavy machinery) it is actually possible to achieve a functional reflection for pre-C++26 standards. It’s limited, but kind of works for lot of situations. The trick itself is 𝗻𝗼𝘁 𝗳𝘂𝗹𝗹𝘆 𝗽𝗼𝗿𝘁𝗮𝗯𝗹𝗲 and must be used carefully, as every compiler produces different output formats. Luckily, some developers have already squeezed this into heavily templated, ready-to-use libraries that do all the hard work for you. C++26 reflection will likely replace these approaches with a 𝘀𝘁𝗮𝗻𝗱𝗮𝗿𝗱𝗶𝘇𝗲𝗱 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻. Until then, this is what “reflection” in modern C++ looks like in practice. reflect-cpp: https://lnkd.in/ejaVevUa magic enum: https://lnkd.in/eK4tPPPe #cpp #c #cplusplus #modernc #moderncpp #cpp17 #cpp20 #cpp23 #cpp26 #coding #code #programming #reflection #array #pointer #arithmetic #typesafety #tech #future #embedded #software #firmware #cleancode #distributed #systems #compiler #gcc #clang #msvc
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
-
💡𝐂#/.𝐍𝐄𝐓 𝐀𝐬𝐲𝐧𝐜 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐓𝐢𝐩 🚀 💎𝐇𝐨𝐰 𝐚𝐧𝐝 𝐰𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 ‘𝐚𝐬𝐲𝐧𝐜’ 𝐚𝐧𝐝 ‘𝐚𝐰𝐚𝐢𝐭’ 💡 '𝐚𝐬𝐲𝐧𝐜' 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
-
-
Hot take: C# Records and Pattern Matching killed half my service layer code. And I'm not sorry. Before C# 9, I was writing: Separate DTO classes with 15 properties Manual null checks everywhere Verbose if-else chains that made reviewers cry Now? record UserResponse(int Id, string Name, string? Email); var message = user switch { { Role: "admin" } => "Welcome, Admin 👑", { IsVerified: false } => "Please verify your email", { Email: null } => "No email on file", _ => "Welcome back!" }; That's it. That's the whole thing. Records give you immutability, value equality, and clean syntax — out of the box. Pattern matching gives you readable branching logic that actually reflects business intent. Combined? You write less code, make fewer mistakes, and your PRs stop getting 40 comments. The devs still writing massive if-else chains in 2025 aren't wrong — they just haven't felt what clean C# can be yet. The language evolved. Our patterns should too. What feature changed how you write C# the most? Drop it below 👇 #dotnet #csharp #softwaredevelopment #cleancode #programming
To view or add a comment, sign in
-
Valgrind - A Practical Tool for Debugging and Memory Analysis in C/C++ While working with C/C++, debugging memory-related issues can be difficult. Problems like memory leaks, invalid access, and uninitialized values are often not visible during normal execution. One tool that significantly improves this process is Valgrind. What is Valgrind? Valgrind is a programming tool used for memory debugging, leak detection, and profiling. It helps identify issues such as: • Memory leaks • Invalid reads/writes • Use of uninitialized memory • Improper memory deallocation Why it matters In real-world systems, small memory issues can lead to crashes, undefined behavior, or performance degradation. Valgrind provides a clear report of where and why these issues occur, making debugging more structured. Quick Start Guide 1. Compile your program with debug symbols: g++ -g program.cpp -o program 2. Run Valgrind: valgrind --leak-check=full ./program 3. Useful flags: --track-origins=yes # Tracks origin of uninitialized values --show-leak-kinds=all # Detailed leak information --verbose # More detailed output Key Insight Debugging is not just about fixing visible errors. It is about understanding how memory behaves during execution. Tools like Valgrind help bridge that gap and make debugging more reliable and systematic. If you are working with C/C++, integrating Valgrind into your workflow can significantly improve code quality. #Cplusplus #Debugging #Valgrind #MemoryManagement #SoftwareEngineering #SystemProgramming #BackendDevelopment #DeveloperTools #Programming #TechLearning
To view or add a comment, sign in
-
-
C# 15 is adding something a lot of people have been asking about. It's not a library. It's not a pattern. It's a new feature in the language itself. Union types landed in C# 15 with .NET 11 Preview 2. If you've ever written a wrapper class, a OneOf hack, or an abstract base just to say "this thing is either A, B, or C," you know why this matters. C# has never had a native way to say "this value is exactly one of these specific types." So we improvised. Discriminated union libraries. Object casting with runtime checks. It worked, but it was ceremony hiding intent. C# 15 changes that with a first-class union keyword. public union Result(Success, ValidationError, NotFoundError); That's it. The compiler now knows Result can only ever be one of those three. No inheritance required. No shared base class. No open-ended extension by other code. Here's the part that really matters. Pattern matching, of all cases, is compiler-enforced. If you forget to handle a case in a switch expression, it's a compile error. Not a runtime surprise at 2am. No default. No discard. No swallowing the bad case. F# has had discriminated unions forever. TypeScript uses union types daily. C# is late to the game, but the implementation looks thought out. It's native, it integrates with existing pattern matching, and it doesn't require a runtime library to make it work. This is the feature that makes error-handling and result modeling in C# genuinely expressive without external dependencies. It will put pressure on old patterns such as abstract base classes used as "closed" hierarchies are going to appear messy and cluttered. What patterns have you been using to simulate this in C#? Will this replace it or complement it? #DotNet #CSharp #CSharpMonday #TonyTechTip #DeveloperProductivity
To view or add a comment, sign in
-
Why don’t more C++ developers talk about Return Value Optimization (RVO)? This optimization has existed in C++ since 1997, yet many developers still assume returning objects is expensive. Let’s take a simple case. A function creates an object and returns it: MyClass Myfun() { return MyClass obj; } Without optimization, you might imagine this happening: • create obj inside Myfun() • copy it when returning to main() • destroy the local object That sounds like extra work, right? Here’s the interesting part. Modern C++ compilers apply Return Value Optimization (RVO). Instead of: create → copy → destroy The compiler simply constructs the object directly in the caller’s memory. So the object is effectively created inside main() from the start. No extra copy. No unnecessary destruction. A subtle optimization — but one that makes returning objects cheap and idiomatic in C++. Small compiler optimizations like this are why understanding how C++ actually works under the hood matters. Did you learn about RVO early, or much later in your C++ journey? #CPP #SoftwareEngineering #Programming #SystemsProgramming #Developers
To view or add a comment, sign in
-
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