VCS2010_09_02 – Local Type Inference (var Keyword)
This video continues to explain the building blocks of LINQ by introducing the var keyword, also known as Local Type Inference (or rather, implicitly typed local variables). Local Type Inference is an easy way to declare a variable without having to know the type upfront. When we work with LINQ, many times it may not be very clear what a given LINQ query is returning, and in some cases when you start projecting anonymous types there IS no pre-existing type to use! (I’ll explain what an anonymous type is later in Day 9).
And so the var keyword allows us to say “we don’t really care what it is, let C# figure it out.” Now, Microsoft suggests that you use the var keyword to make development easier. Some authors I’ve read insist that you should use it as a temporary measure until you can figure out the exact return type … one technique would be to set a break point and use the debugging tools to figure out what the actual type is. I think they say this because they, like me, know that ambiguity leads to laziness. While I’m against laziness, I do like convenience, so I’m going to side with Microsoft on this one. But whichever you decide is up to you. Keep in mind is that the var keyword only works for the local scope. In other words, you can’t return or reference a variable created with var outside of the scope of a local method. This video will demonstrate these ideas and show it used in a LINQ example with thorough explanations.
This is a short preview that demonstrates the quality of the screencasts we offer at LearnVisualStudio.NET.
Scroll down and click the play button in the lower-left hand corner of the video player to start watching.
The full length video is 13 Minutes, 11 Seconds and is available to LearnVisualStudio.NET members. Join today!
Bob, we love the videos – good presentation & pace. They’re a great way to help beginners get up to speed quickly.Mark Jewett , Senior Manager
SQL Server, Microsoft Corporation
1 Year Membership
$75per year
(it’s about $6 a month)
- Watch hundreds of screencasts
- New training each week
- Stream instantly via Vimeo … even works on iOS (and *some* Android devices)!
- Download source code
Lifetime Membership
$200pay just once
keep learning new .NET stuff
- 1 Year benefits, plus …
- Download video files to keep. Powered by Amazon S3 (blazing fast).
- Q & A Get answers to your questions about the videos
