Click here to Skip to main content
15,891,788 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: C#'s sneaky typedef Pin
Simon O'Riordan from UK30-May-13 22:32
Simon O'Riordan from UK30-May-13 22:32 
GeneralRe: C#'s sneaky typedef Pin
KP Lee29-May-13 17:11
KP Lee29-May-13 17:11 
GeneralRe: C#'s sneaky typedef Pin
Simon O'Riordan from UK29-May-13 20:55
Simon O'Riordan from UK29-May-13 20:55 
GeneralRe: C#'s sneaky typedef Pin
Rob Grainger31-May-13 3:08
Rob Grainger31-May-13 3:08 
GeneralRe: C#'s sneaky typedef Pin
Simon O'Riordan from UK2-Jun-13 20:54
Simon O'Riordan from UK2-Jun-13 20:54 
GeneralRe: C#'s sneaky typedef Pin
johannesnestler6-Jun-13 4:17
johannesnestler6-Jun-13 4:17 
GeneralRe: C#'s sneaky typedef Pin
KP Lee6-Jun-13 20:48
KP Lee6-Jun-13 20:48 
GeneralRe: C#'s sneaky typedef Pin
johannesnestler7-Jun-13 1:52
johannesnestler7-Jun-13 1:52 
The "var" thing in C# is often miss-interpreted. Because everyone thinks of runtime type identification (VB...). But in C# it's not - it's only syntactic sugar if you don't want to retype complicated Type-names like: Dictionary<mykeyclass<int>, List<list<double>>>, but the compiler always knows what type it is - it's normal strong compile-time type checking. Even the IntelliSense knows the type.
But there are situations it is neccessary: you - as the programmer - sometimes do not know which type an expression will give back (anonymouse types), but the compiler will create a class for the anonymouse type during compilation and can fill in the missing type instead of the var keywords - but again: AT COMPILE TIME.

So in your example: object field = new SomeClass(); var x = field; will result in x interpreted as object not SomeClass. Don't mix that up with GetType() or typeof operator in c# - you can always ask an object for it's type - AT RUNTIME.
This is an better example: object field1 = new SomeClass(); var field2 = new SomeClass(); Where for field1 you are holding a SomeClass-instance as a base class reference (everything derives from object) where field2 is totally equivalent to: SomeClass field2 = new SomeClass();

If you want my personal opinion: I just use it where needed (anonymouse types) or for LINQ (there you often have those ugly long typenames). For me it had another nice side-effect I didn't think of in the first place: If you have specific algorithms or snippets and used the var keyword instead of the real type, you get code that is great for copy-pasting arround. This sometimes helps to focus on the underlaying constructs - and it helped me "to see" how repetitive code can be unified.

var regards = from reg in AllRegards
where reg.Quality = "best"
select new { Regards=reg, Quality=reg.Quality };

(this would be valid C# (LINQ) giving back an anonymouse type - you can not know it's Name, so use of var keyword is mandatory...)
GeneralRe: C#'s sneaky typedef Pin
Chad3F29-May-13 12:49
Chad3F29-May-13 12:49 
GeneralRe: C#'s sneaky typedef Pin
Gary Wheeler4-Jun-13 0:51
Gary Wheeler4-Jun-13 0:51 
RantOh no, not again.... Pin
Lutosław25-May-13 3:23
Lutosław25-May-13 3:23 
GeneralRe: Oh no, not again.... Pin
KP Lee28-May-13 18:11
KP Lee28-May-13 18:11 
GeneralRe: Oh no, not again.... Pin
Lutosław29-May-13 1:12
Lutosław29-May-13 1:12 
GeneralRe: Oh no, not again.... Pin
johannesnestler6-Jun-13 4:32
johannesnestler6-Jun-13 4:32 
GeneralRe: Oh no, not again.... Pin
KP Lee6-Jun-13 20:13
KP Lee6-Jun-13 20:13 
GeneralRe: Oh no, not again.... Pin
johannesnestler7-Jun-13 2:14
johannesnestler7-Jun-13 2:14 
GeneralRe: Oh no, not again.... Pin
KP Lee7-Jun-13 22:13
KP Lee7-Jun-13 22:13 
GeneralFound something interesting PinPopular
BotCar23-May-13 22:08
BotCar23-May-13 22:08 
GeneralWhy not: // just shoot me! Pin
esaulsberry20-May-13 8:48
esaulsberry20-May-13 8:48 
GeneralRe: Why not: // just shoot me! PinPopular
Pualee20-May-13 10:14
Pualee20-May-13 10:14 
GeneralRe: Why not: // just shoot me! Pin
OriginalGriff20-May-13 11:02
mveOriginalGriff20-May-13 11:02 
GeneralRe: Why not: // just shoot me! Pin
Pankaj Nikam20-May-13 19:26
professionalPankaj Nikam20-May-13 19:26 
GeneralRe: Why not: // just shoot me! Pin
Oshtri Deka20-May-13 22:15
professionalOshtri Deka20-May-13 22:15 
GeneralRe: Why not: // just shoot me! Pin
Gary Wheeler21-May-13 1:17
Gary Wheeler21-May-13 1:17 
GeneralRe: Why not: // just shoot me! Pin
englebart21-May-13 2:39
professionalenglebart21-May-13 2:39 

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.