Skip to content

Instantly share code, notes, and snippets.

@kevinbrechbuehl
Created March 8, 2013 08:50
Show Gist options
  • Save kevinbrechbuehl/5115085 to your computer and use it in GitHub Desktop.
Save kevinbrechbuehl/5115085 to your computer and use it in GitHub Desktop.
Good and bad examples of using the var keyword in C#
// good design
var numbers = new int[] {1, 2, 3, 4};
var stringBuilder = new StringBuilder();
var letters = new List();
var keywords = new Dictionary();
// OK (but prefer explicit declaration)
var pages = 10; // int, long, what to expect?
var username = "Kevin";
var order = GetOrder(orderId); // ok if the type is Order, otherwise not
for (var x = 1; x < 10; x++)
// bad design
var settings = GetInboxSettings(); // not obvious at all
var userId = GetUserId(); // ambigous, is this guid, string, int or a custom UserId object?
Dictionary orders = new Dictionary(); // redundant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment