Code Reviews CSharp Tips

From Logic Wiki
Jump to: navigation, search


Avoid deep nested class definitions try to maintain around 3 levels


Using Static Classes instead of Singleton.


Moving nested classes to another file with the namespace and class definition will make code simpler.


virtual -> override methods.


Use auto property when there is no business logic involved. (type prop and hit TAB twice)

public string FirstName {get; set;]

If your html (cshtml) page is in <meta charset="utf-8"> you need to save that page in utf-8 format using save as.


use compatible meta tag to tell the browser which version needed to render this page.

<meta http-equiv="X-UA-Compatible" Content="IE=edge;chrome=1">  

instead of using on page styles and scripts with @section styles moving these codes to individual files makes them cached by the browsers


instead of delivering our own jquery or other scripts in BundleConfig.cs adding another parameter to bundles add will be more efficient. First it will look for google and if the site is down it look local path.

bundles.UseCdn = true;
bundles.Add(new ScriptBundle("~/bundles/jquery" , //ajax.gogleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" )
   .Include("~/Scripts/jquery-{version}.js"))

using // means it can be either http or https


TIP :

to see the list of google CDNs please go to

https://developers.google.com/speed/libraries/devguide


No code in view !


instead of using

@using CodeReview.Models; 

on every view page it needed. We can put this line to web.config file under Views folder

<namespaces>
  <add namespace="CodeReview.Models" />