Saturday, January 21, 2017

Use of var in C# -- We need to have some discipline

As C# language evolves, programmers need to have the discipline and keep the code as elegant as possible. Some new capabilities with "var" declaration could make the code less readable if you don't follow simple guidelines.

Here are a few examples with comments showing what is good and what is bad practice:


var customerInfo = RetrieveCustomerInfo(customerId);  // Bad, not readable
CustomerInfo customerInfo = RetrieveCustomerInfo(customerId);  //Good


var settings = GetInboxSettings();      // Bad, not readable
InboxSettings settings = GetInboxSettings();   // Good


Dictionary<string, List<string>> myDictionary = new Dictionary<string, List<string>>();   // redundant
var myDictionary = new Dictionary<string, List<string>>();    // OK



Almir M.

#Csharp #DotNet #programming #code #coding #SoftwareDevelopment #SoftwareEngineering 


No comments:

Post a Comment