- •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
- •Task 2 – Using Collection Initializers
- •Exercise 2 – Easy Initialization with Object and Collection Initializers
- •Task 1 – Using Object 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 2 – Easy Initialization with Object and Collection Initializers
In C# 3.0, when declaring an object or collection, you may include an initializer that specifies the initial values of the members of the newly created object or collection. This new syntax combines object creation and initialization into a single step.
Task 1 – Using Object Initializers
An object initializer consists of a sequence of member initializers, contained in { } braces and separated by commas. Each member initializer assigns a value to a field or property of the object. For example, recall the Point class from Exercise 1:
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
An object of type Point can be initialized using an object initializer like this:
Point p = new Point { X = 3, Y = 99 };
This concise initialization syntax is semantically equivalent to invoking the instance constructor and then performing assignments for each of the properties.
Replace the code in Main with the following:
static void Main(string[] args)
{
Customer c = new Customer(1) { Name = "Maria Anders", City = "Berlin" };
Console.WriteLine(c);
}
Press Ctrl+F5 to build and run the application. The customer is printed to the console window. Now press any key to terminate the application.
For this object, the object initializer invokes the object’s constructor, and then initializes the named fields to the specified values. Not all fields of the object need to be specified in the list. If not specified, the fields will have their default value. Also note if invoking the object’s parameterless constructor, the parentheses are optional. For example you could have written Customer c = new Customer() {…} as Customer c = new Customer {…}.
Task 2 – Using Collection Initializers
With C# 3.0, any object that implements System.Collections.Generic.IEnumerable<T> and has a public Add method can have its values initialized with a collection initializer.
Using the Point class, let’s create a shape that is made up of a collection of points.
List<Point> Square = new List<Point>
{
new Point { X=0, Y=5 },
new Point { X=5, Y=5 },
new Point { X=5, Y=0 },
new Point { X=0, Y=0 }
};
For the remainder of the lab a collection of Customers will be needed. Create a list of Customers in a new method CreateCustomers.
static void Main(string[] args)
{
Customer c = new Customer(1) { Name = "Maria Anders", City = "Berlin" };
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" }
};
}
In Main, replace the code with the following:
static void Main(string[] args)
{
List<Customer> customers = CreateCustomers();
Console.WriteLine("Customers:\n");
foreach (Customer c in customers)
Console.WriteLine(c);
}
Press Ctrl+F5 to build and run the application and print the list of customers. Press any key to terminate the application.
When calling CreateCustomers, the list is initialized empty and each object is created separately. Each object is then added to the list using the Add method.
