Subscribe For Free Updates!

We'll not spam mate! We promise.

Mar 20, 2011

Object and Collection Initializers Local Type Inference



  • In this video we look at a object and collection initializers. They work similarly to an initialized array, where we're able to set the properties of a newly created object or multiple instances of new objects in a new collection in essentially one line of code. We'll begin by demonstrating object initializers and then show how to use collection initializers ... In some ways it's similar to a constructor that populates the properties of a newly instance of an object. However, unlike a constructor, you can pick and choose which properties to populate in any combination. We'll also demonstrate some advanced scenarios like nested initializers and more.

  • 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,

  • In this video we'll look at another language enhancement in C# 3.0 and that is Anonymous Types. Anonymous types are are "on the fly classes" ... un-named classes ... and the key to this is that the compiler "infers" or figures out what the type definition looks like based on the object initializer. You might wonder why you would need such a thing, but they are used extensively when working with LINQ (i.e.,, when projecting results back from a LINQ query), so we want to get our heads around them before we start looking at LINQ examples.

  • In this video we'll talk about extension methods which are simply methods -- usually helper methods that can be bolted on to the side of existing types / classes ... even if that type is sealed, meaning that it cannot be inherited from. Extension Methods also help promote a more fluent style of programming (which we'll demonstrate and explain in the video.) In the past you may have had some helper methods that are intended for operation on certain types ... like a formatting method for example. This is similar in concept, but the implementation is cleaner and more elegant. The reason why Extension Methods are important is because this is how LINQ's methods, like Select, Where, Sum, Count, are bolted on to the side of types that implement the IEnumerable interface, as well as others. So, Extension Methods are what makes LINQ's method syntax work.

  • Typically, we speak of delegates in the context of events. However, since LINQ there's been a need to understand Delegates in a new context. In the first of a three-part mini series about Lambda Expressions, we use Delegates to explain the first chapter of the Lamda Expression story. Several length, well explained examples highlight how delegates work, what is their purpose, and most importantly, will help us to better understand Anonymous Methods and ultimately Lambda Expressions and their use within LINQ.

  • In this second in a three-part mini-series about Lambda Expressions we demonstrate how Anonymous Methods can be used to satisfy a delegate input parameter to a method without having to declare a named method that matches the delegate's method signature ... an Anonymous Method is a block of code without a name and can be declared "in line" and passed into a method as an input parameter. How does this help us? The real payoff for this idea is in the next video when we learn about Lambda Expressions, but Anonymous Methods are another step towards that important language element that is used extensively by the LINQ Method Syntax.

  • This video is the thrilling conclusion to a three part mini-series that culminates in deciphering what a Lambda Expression is, how it is used in LINQ, and also the Func delegate declaration used by Microsoft in LINQ's Method syntax (that you can use, too!)

  • In this lecture, Bob explains the benefits and two major styles of LINQ; the Method Syntax and the Query Syntax. Also discussed is the how LINQ is implemented in C#, and how LINQ is used to describe intent allowing the implementation of the given LINQ Provider to take on the responsibility of determining (and executing) the most efficient means of achieving the developer's intent.

  • This video demonstrates a dozen short examples to showcase both the Method Syntax and the Query Syntax to perform simple operations against a small sequence of data in an array of strings. Through this example we get a taste for both syntax and will use this a basis for further exploration throughout the remainder of Day 9.

  • Projection in LINQ is the act of selecting data items from a sequence and shaping them into a new instance of a named type (a .NET Framework type or custom class) OR into an anonymous type. This video demonstrates many different techniques for using the LINQ C# keyword SELECT as a means of projecting your results into new lists of a given type. Even some more advanced projections are demonstrated such as taking an object graph and projecting it into a new XML document -- in one line of code! We demonstrate both method and query syntax for all examples.

  • In this video we demonstrate the use of LINQ's WHERE clause to filter the data items in a sequence. In addition, we show how to chain WHERE clauses together using the && and || (AND and OR) operators in C#, how to accomplish this using the method syntax and lambda expressions and extension methods, and more.

  • In this video we'll look at the FROM and JOIN statements in LINQ. We'll look at how to retrieve both flattened and hierarchical results, and how to create what we might basically consider both inner and outer joins if this were T-SQL. We'll over the following C# keywords: join - on - equals and into ... and we'll cover the following extension methods: SelectMany(), Join() and GroupJoin().

  • In this video we've included pretty much every other linq extension method ... featuring methods that can help you determine whether there are any items in the sequence at all, or at least, any items that meet a certain criteria, other methods that perform basic operations on the seuqnce ... that is the sum, the min, the max, the average and so on, methods that find the first or last occurance of an item in the sequence that meets a certain criteria and so on. We'll cover: Any(), All(), Contains(), Count(), Sum(), Min(), Max(), Average(), First(), FirstOrDefault(), Last(), LastOrDefault(), Single(), SingleOrDefault() and Aggregate().

  • This video caps off our jaunt into the land of LINQ. Here you are presented with 9 LINQ challenges and are required to complete each one using the Query Syntax and the Method Syntax. Good luck … you will need it! (If you get stuck, there's always the solution videos for each part of this exercise!)

  • This video demonstrates the thought process and the solution for each of the nine LINQ challenges for Day 9 using the Method Syntax.

  • This video demonstrates the thought process and the solution for each of the nine LINQ challenges for Day 9 using the Query Syntax.

  • Plese Feel Free to Socializer This Post
    SHARE IT →


    Post a Comment

    Become a Fan

    visual studio learn