In this article, we will learn about how to write clean C# code. As we know that writing code is not much difficult, but writing clean and reusable code is not easy in work pressure time. but If you write better code your senior will impress more, which can help you in your career growth.
This will come from practice do more and more. so here we will cover the following things,
- Use a Good IDE
- Use Meaningful Names
- Use Case Notation
- Reuse Code
- Design Patterns
- Remove Unused Code
- Method Chaining
- Async/Await
- Ternary Operator
- Null Coalescing Operator
- Prefer String Interpolation
- Avoid Large Parameters in method
- Log Exception
Use a Good IDE
First of all before start to write the code choose best IDE for your code development. In case of C# code writing we have Visual Studio, Visual Studio Code it's free provided by Microsoft. As this IDE provided code fomatting and intellisens to correct our code.
Use Meaningful Names
You know if we work on bigger project then according to business need we required a lot of local variable and class proporty names. That at time we confuse to decide better name. but we should tale time and analyze the best name for any varible, class name or method name, parameter as well.
so it should be selft explanatory.
Here i am giving some example for this.
- // Bad
- int i1;
- string s1;
- date d1;
- // Good
- int age;
- string name;
- date createdDate;
so that it will easy for other developer to understand the concept.
Use Case Notation
We should also follow that how Microsoft writing his code, so this is very easy to see just click on an inbuilt method to check for it's definiton, you will see a lot of method and proporty how microsoft define.
here is the example,
- //
- // Summary:
- // Information about the current routing path.
- public class RouteData {
- //
- // Summary:
- // Creates a new instance of Microsoft.AspNetCore.Routing.RouteData instance.
- public RouteData();
- //
- // Summary:
- // Creates a new instance of Microsoft.AspNetCore.Routing.RouteData instance with
- // values copied from other.
- //
- // Parameters:
- // other:
- // The other Microsoft.AspNetCore.Routing.RouteData instance to copy.
- public RouteData(RouteData other);
- //
- // Summary:
- // Creates a new instance of Microsoft.AspNetCore.Routing.RouteData instance with
- // the specified values.
- //
- // Parameters:
- // values:
- // The Microsoft.AspNetCore.Routing.RouteValueDictionary values.
- public RouteData(RouteValueDictionary values);
- //
- // Summary:
- // Gets the data tokens produced by routes on the current routing path.
- public RouteValueDictionary DataTokens {
- get;
- }
- //
- // Summary:
- // Gets the list of Microsoft.AspNetCore.Routing.IRouter instances on the current
- // routing path.
- public IList < IRouter > Routers {
- get;
- }
- //
- // Summary:
- // Gets the values produced by routes on the current routing path.
- public RouteValueDictionary Values {
- get;
- }
- //
- // Summary:
- // Creates a snapshot of the current state of the Microsoft.AspNetCore.Routing.RouteData
- // before appending router to Microsoft.AspNetCore.Routing.RouteData.Routers, merging
- // values into Microsoft.AspNetCore.Routing.RouteData.Values, and merging dataTokens
- // into Microsoft.AspNetCore.Routing.RouteData.DataTokens.
- // Call Microsoft.AspNetCore.Routing.RouteData.RouteDataSnapshot.Restore to restore
- // the state of this Microsoft.AspNetCore.Routing.RouteData to the state at the
- // time of calling Microsoft.AspNetCore.Routing.RouteData.PushState(Microsoft.AspNetCore.Routing.IRouter,Microsoft.AspNetCore.Routing.RouteValueDictionary,Microsoft.AspNetCore.Routing.RouteValueDictionary).
- //
- // Parameters:
- // router:
- // An Microsoft.AspNetCore.Routing.IRouter to append to Microsoft.AspNetCore.Routing.RouteData.Routers.
- // If null, then Microsoft.AspNetCore.Routing.RouteData.Routers will not be changed.
- //
- // values:
- // A Microsoft.AspNetCore.Routing.RouteValueDictionary to merge into Microsoft.AspNetCore.Routing.RouteData.Values.
- // If null, then Microsoft.AspNetCore.Routing.RouteData.Values will not be changed.
- //
- // dataTokens:
- // A Microsoft.AspNetCore.Routing.RouteValueDictionary to merge into Microsoft.AspNetCore.Routing.RouteData.DataTokens.
- // If null, then Microsoft.AspNetCore.Routing.RouteData.DataTokens will not be changed.
- //
- // Returns:
- // A Microsoft.AspNetCore.Routing.RouteData.RouteDataSnapshot that captures the
- // current state.
- public RouteDataSnapshot PushState(IRouter router, RouteValueDictionary values, RouteValueDictionary dataTokens);
- //
- // Summary:
- // A snapshot of the state of a Microsoft.AspNetCore.Routing.RouteData instance.
- public readonly struct RouteDataSnapshot {
- //
- // Summary:
- // Creates a new instance of Microsoft.AspNetCore.Routing.RouteData.RouteDataSnapshot
- // for routeData.
- //
- // Parameters:
- // routeData:
- // The Microsoft.AspNetCore.Routing.RouteData.
- //
- // dataTokens:
- // The data tokens.
- //
- // routers:
- // The routers.
- //
- // values:
- // The route values.
- public RouteDataSnapshot(RouteData routeData, RouteValueDictionary dataTokens, IList < IRouter > routers, RouteValueDictionary values);
- //
- // Summary:
- // Restores the Microsoft.AspNetCore.Routing.RouteData to the captured state.
- public void Restore();
- }
In summany use can say that for camel case use the first letter of the first word of the variable will be in lower case, then after that use first letter of every other word that in upper case. This is for while naming your local variables and method arguments.

Sabuj SenPosted Jun 7, 2021, 10:17 PM
Very nice article, very informatic every body should read before do code. But only question async is it really improve performance for STA. better to use parallel thread to improve the performance.
Pritesh MaturkarPosted Mar 15, 2021, 8:33 AM
Nice Article. It is helpful.