Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
AnswerRe: In visual studio do you know what the name of this control ? Pin
BillWoodruff17-Sep-15 0:11
professionalBillWoodruff17-Sep-15 0:11 
GeneralRe: In visual studio do you know what the name of this control ? Pin
Member 245846717-Sep-15 23:26
Member 245846717-Sep-15 23:26 
AnswerRe: In visual studio do you know what the name of this control ? Pin
ngoj17-Sep-15 2:43
ngoj17-Sep-15 2:43 
GeneralRe: In visual studio do you know what the name of this control ? Pin
Pete O'Hanlon17-Sep-15 2:55
mvePete O'Hanlon17-Sep-15 2:55 
GeneralRe: In visual studio do you know what the name of this control ? Pin
ngoj17-Sep-15 3:14
ngoj17-Sep-15 3:14 
QuestionDeclarations using var Pin
JBHowl16-Sep-15 1:54
JBHowl16-Sep-15 1:54 
AnswerRe: Declarations using var Pin
Eddy Vluggen16-Sep-15 2:16
professionalEddy Vluggen16-Sep-15 2:16 
AnswerRe: Declarations using var PinPopular
Richard Deeming16-Sep-15 2:29
mveRichard Deeming16-Sep-15 2:29 
When the type is obvious, using var means you don't have to repeat the type:
C#
var someDictionary = new Dictionary<string, List<Expression<Func<IEmployee, bool>>>>(StringComparer.OrdinalIgnoreCase);
// vs:
Dictionary<string, List<Expression<Func<IEmployee, bool>>>> someDictionary = new Dictionary<string, List<Expression<Func<IEmployee, bool>>>>(StringComparer.OrdinalIgnoreCase);



When the type is not immediately obvious, but isn't integral to understanding the code, then it's a matter of personal preference or style guidelines:
C#
using (var dbReader = dbCommand.ExecuteReader())
// vs:
using (SqlDataReader dbReader = dbCommand.ExecuteReader())

(In this example, if you change the type of dbCommand, the var sample would just work, but the SqlDataReader would generate a compiler error until you changed the type of dbReader as well.)


In most cases where the type is not obvious, you should try to avoid using var:
C#
var list = GetFoo();
// vs:
List<Customer> list = GetFoo();

However, proper method and variable names would make the code clearer:
C#
var listOfCustomers = GetListOfCustomers();



And finally, if you're using anonymous types, then you have to use var:
C#
var listOfNames = listOfCustomers.Select(c => new { c.FirstName, c.Surname });

(Anonymous types are the reason var was introduced.)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Declarations using var Pin
Eddy Vluggen16-Sep-15 9:32
professionalEddy Vluggen16-Sep-15 9:32 
GeneralRe: Declarations using var Pin
PIEBALDconsult16-Sep-15 11:53
mvePIEBALDconsult16-Sep-15 11:53 
AnswerRe: Declarations using var Pin
PIEBALDconsult16-Sep-15 11:52
mvePIEBALDconsult16-Sep-15 11:52 
GeneralRe: Declarations using var Pin
Rob Philpott16-Sep-15 23:05
Rob Philpott16-Sep-15 23:05 
GeneralRe: Declarations using var Pin
Richard Deeming17-Sep-15 1:53
mveRichard Deeming17-Sep-15 1:53 
AnswerRe: Declarations using var Pin
JBHowl17-Sep-15 2:15
JBHowl17-Sep-15 2:15 
GeneralRe: Declarations using var Pin
Pete O'Hanlon17-Sep-15 2:17
mvePete O'Hanlon17-Sep-15 2:17 
QuestionHow to declare and define a method returns a list? Pin
Member 1198763615-Sep-15 21:36
Member 1198763615-Sep-15 21:36 
AnswerRe: How to declare and define a method returns a list? Pin
OriginalGriff15-Sep-15 22:15
mveOriginalGriff15-Sep-15 22:15 
AnswerRe: How to declare and define a method returns a list? Pin
BillWoodruff16-Sep-15 8:01
professionalBillWoodruff16-Sep-15 8:01 
QuestionUSB port Pin
Member 1198669115-Sep-15 20:38
Member 1198669115-Sep-15 20:38 
AnswerRe: USB port Pin
Pete O'Hanlon15-Sep-15 21:29
mvePete O'Hanlon15-Sep-15 21:29 
GeneralRe: USB port Pin
Member 1198669116-Sep-15 15:51
Member 1198669116-Sep-15 15:51 
GeneralRe: USB port Pin
Pete O'Hanlon16-Sep-15 21:09
mvePete O'Hanlon16-Sep-15 21:09 
AnswerRe: USB port Pin
OriginalGriff15-Sep-15 21:39
mveOriginalGriff15-Sep-15 21:39 
GeneralRe: USB port Pin
Member 1198669116-Sep-15 15:54
Member 1198669116-Sep-15 15:54 
AnswerRe: USB port Pin
Eddy Vluggen17-Sep-15 22:54
professionalEddy Vluggen17-Sep-15 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.