From the course: .NET Essentials: Working with LINQ

Implicit variable typing

- This is the chapter where we look at some of the features that were added to .net and C sharp to bring functional programming concepts into the framework. These were released at the same time as link, and many of these features I'm looking at in this chapter, are enablers for link. They make it easier for us to write the link code. To find, the examples here in chapter three and key features, I'll be talking about implicitly type variables, anonymous types, object, and collection initializers, Lambda expressions, extension method, and Generics. All these concepts have been around for a long time. So you might already be familiar with them. If that's the case, you can skip ahead to the next chapter. Otherwise stay with me and I'll do a review of these concepts and then later in the course, we'll see how they help enable working with link. The first concept we're going to look at is implicitly typed variables. These are available in C sharp and visual basic. And the idea is the compiler looks at your variable. You're declaring the variable with the var keyword, and you're not specifying a type. So the compiler is going to look at this expression, and see that the type of this string literal, is a string, so it's going to infer the type and make this variable string. For line number nine, it's going to infer that this is a char. The main reason this is available, it was added to C sharp and visual basic, is it's really important for the next feature we'll look at in the next video, which is anonymous types. So what we're doing here is I've got these two variables. You'll notice that you're not saving a lot of space. It does get more simplified, when you have something like this, where, if I'm declaring this guids, a SortedDictionary of string as the key and a guid as the value, I have to repeat that at the beginning of the line, for the type, whereas I've used var it's less code. And I like this. I think it cleans up your code nicely. Before I run the demo, Let's talk about one more section of code. I wrote a method down here called ShowType, that takes an object, and it's going to figure out what the type of that object is, and then it's going to also take the name of the parameter that I'm passing in. And you see I'm calling that here, ShowType, variables greeting, and then I'm using the nameof, and is taking greeting and that will give me the string representation of this greeting name. And I'm using that to build this string here. Put the variable name, and the implied type. So I should get one for creating one for thechar, and one for this dictionary, this SortedDictionary. And there they are. Inferred or implied type is system.string applied type is system.char, and then imply type for this one is here. System.collections.Generic.SortedDictionary And, this is the eridy. You see this when you're working with Generics, that means it has two parameters, two Generic parameters, and the first one is a string, and the second one is a guid.

Contents