Created
March 8, 2013 08:50
-
-
Save kevinbrechbuehl/5115085 to your computer and use it in GitHub Desktop.
Good and bad examples of using the var keyword in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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