
- •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:
Exercise 4:
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" }
};
}
}
}
Exercise 5:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewLanguageFeatures
{
public static class Extensions
{
public static List<T> Append<T>(this List<T> a, List<T> b)
{
var newList = new List<T>(a);
newList.AddRange(b);
return newList;
}
public static bool Compare(this Customer customer1, Customer customer2)
{
if (customer1.CustomerID == customer2.CustomerID &&
customer1.Name == customer2.Name &&
customer1.City == customer2.City)
{
return true;
}
return false;
}
}
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)
{
var customers = CreateCustomers();
var addedCustomers = new List<Customer>
{
new Customer(9) { Name = "Paolo Accorti", City = "Torino" },
new Customer(10) { Name = "Diego Roel", City = "Madrid" }
};
var updatedCustomers = customers.Append(addedCustomers);
var newCustomer = new Customer(10)
{
Name = "Diego Roel",
City = "Madrid"
};
foreach (var c in updatedCustomers)
{
if (newCustomer.Compare(c))
{
Console.WriteLine("The new customer was already in the list");
return;
}
}
Console.WriteLine("The new customer was not in the list");
}
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" }
};
}
}
}