C# is a modern, object-oriented programming language developed by Microsoft. It is designed for building a variety of applications that run on the .NET platform, combining the power of C++ with the simplicity of Visual Basic.
C# Anti-Patterns Overview
Using Exception Handling for Control Flow
TryParse
that are specifically designed for validation.Not Disposing IDisposable Objects
IDisposable
objects to release unmanaged resources. The using
statement ensures proper disposal even if exceptions occur.Excessive Use of Null Checks
?.
) and null coalescing operator (??
) for cleaner code.Not Using async/await Correctly
.Result
or .Wait()
as it can lead to deadlocks. Also avoid async void
except for event handlers as exceptions can’t be caught.Using Public Fields Instead of Properties
Not Using LINQ When Appropriate
Using Strings for Sensitive Data
SecureString
for sensitive data in memory and proper hashing for storage.Not Using C# Features Appropriately
Using Magic Strings and Numbers
Not Using Dependency Injection
Not Using IEnumerable<T> for Method Returns
IEnumerable<T>
instead of concrete collection types gives you more flexibility to change the implementation and prevents callers from modifying the collection.Not Using Proper Exception Handling
Not Using Object Initializers
Not Using Proper Collection Types
IEnumerable<T>
for simple iteration, List<T>
when you need to modify the collection, Dictionary<TKey, TValue>
for lookups, etc.Not Using Nullable Reference Types
Not Using Expression-Bodied Members
Not Using Pattern Matching
Using Mutable Collections for Public Properties
Not Using Tuple Deconstruction