
- •Contents
- •Lab 1: c# 3.0 Language Enhancements
- •Lab Objective
- •Exercise I – Use of Automatically Implemented Properties
- •Task 1 – Creating the "NewLanguageFeatures" Project
- •Task 2 – Creating a new class utilizing Automatically Implemented Properties
- •Task 3 – Use of Accessibility Modifiers with Properties
- •Exercise 2 – Easy Initialization with Object and Collection Initializers
- •Task 1 – Using Object Initializers
- •Task 2 – Using Collection Initializers
- •Exercise 3 – Implicitly Typed Local Variables and Implicitly Typed Arrays
- •Task 1 – Declaring Simple Implicitly Typed Local Variables and Arrays
- •Exercise 4 – Extending Types with Extension Methods
- •Task 1 – Declaring Extension Methods
- •Task 2 – Using Extension Methods with Generic Types
- •Exercise 5 – Working with Lambda Expressions
- •Task 1 – Understanding Lambda Expressions
- •Task 2 – Calling a Complex Extension Method using Lambda Expressions [optional]
- •Exercise 6 – Using Lambda Expressions to Create Expression Trees
- •Task 1 – Creating an Expression Tree using a Lambda Expression
- •Task 2 – Relationship between functions and expressions
- •Task 1 – Creating a new class to Query
- •Task 2 –Querying with in-Memory Collections
- •Task 3 – Additional Query Expressions
- •Exercise 8 – Anonymous Types and Advanced Query Creation
- •Task 1 – Creating Anonymous Types
- •Task 2 – Additional Query Expressions Using Anonymous Types
- •Task 3 – Overview of linq To sql, linq To xml, and linq to DataSet
- •Lab Summary
- •Appendix: Code Samples (used if not running sequentially through the lab)
- •Exercise 1: The first task in this exercise explains how to open Visual Studio and create a new project. Exercise 2:
- •Exercise 3:
- •Exercise 4:
- •Exercise 5:
- •Exercise 6:
- •Exercise 7:
- •Exercise 8:
Task 3 – Overview of linq To sql, linq To xml, and linq to DataSet
As shown here LINQ provides a language integrated query framework for .NET. The features shown in the previous tasks can be used to query against relational databases, datasets, and data stored in XML.
This overview demonstrated LINQ To Objects (In-Memory Collections). For a deeper understanding of using LINQ with databases, datasets, and XML, see the LINQ Project Overview Hands On Lab.
Lab Summary
In this lab you preformed the following exercises:
|
Through the eight exercises you explored the new language features available in C# 3.0. Many of the features, such as implicitly typed local variables, extension methods, lambda expressions, and object initializers, provide a new level of convenience for C# developers. Together, the features create an expressive language for data access, allowing you to apply LINQ technology to your work, increasing your productivity as you are able to work with data as objects.
Appendix: Code Samples (used if not running sequentially through the lab)
This section contains the starting blocks of code required if skipping sections. All code written in this lab is contained in one file: program.csEach exercise in this lab is structured to build upon the code written in the previous exercises. Compiled in this appendix are the complete sets of code that are generated by each exercise (the entire program.cs file). For example: If you wish to start at exercise 5, then you may start with the code provided for you in the exercise 5 below.
Exercise 1: The first task in this exercise explains how to open Visual Studio and create a new project. Exercise 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewLanguageFeatures
{
public class Customer
{
public int CustomerID { get; private set; }
public string Name { get; set; }
public string City { get; set; }
public Customer(int ID)
{
CustomerID = ID;
}
public override string ToString()
{
return Name + "\t" + City + "\t" + CustomerID;
}
}
class Program
{
static void Main(string[] args)
{
Customer c = new Customer(1);
c.Name = "Maria Anders";
c.City = "Berlin";
Console.WriteLine(c);
}
}
}
Exercise 3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewLanguageFeatures
{
public class Customer
{
public int CustomerID { get; private set; }
public string Name { get; set; }
public string City { get; set; }
public Customer(int ID)
{
CustomerID = ID;
}
public override string ToString()
{
return Name + "\t" + City + "\t" + CustomerID;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = CreateCustomers();
Console.WriteLine("Customers:\n");
foreach (Customer c in customers)
Console.WriteLine(c);
}
static List<Customer> CreateCustomers()
{
return new List<Customer>
{
new Customer(1) { Name = "Maria Anders", City = "Berlin" },
new Customer(2) { Name = "Laurence Lebihan", City = "Marseille" },
new Customer(3) { Name = "Elizabeth Brown", City = "London" },
new Customer(4) { Name = "Ann Devon", City = "London" },
new Customer(5) { Name = "Paolo Accorti", City = "Torino" },
new Customer(6) { Name = "Fran Wilson", City = "Portland" },
new Customer(7) { Name = "Simon Crowther", City = "London" },
new Customer(8) { Name = "Liz Nixon", City = "Portland" }
};
}
}
}