- •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:
Lab 1: c# 3.0 Language Enhancements
The C# 3.0 language enhancements build on C# 2.0 to increase developer productivity: they make written code more concise and make working with data as easy as working with objects. These features provide the foundation for the LINQ project, a general purpose declarative query facility that can be applied to in-memory collections and data stored in external sources such as XML files and relational databases.
The C# 3.0 language enhancements consist of:
Auto-implemented properties, which automate the process of creating properties with trivial implementations.
Implicitly typed local variables, which permit the type of local variables to be inferred from the expressions used to initialize them.
Implicitly typed arrays, a form of array creation and initialization that infers the element type of the array from an array initializer.
Extension methods, which make it possible to extend existing types and constructed types with additional methods.
Lambda expressions, an evolution of anonymous methods that concisely improves type inference and conversions to both delegate types and expression trees.
Expression trees, which permit lambda expressions to be represented as data (expression trees) instead of as code (delegates).
Object and collection initializers, which you can use to conveniently specify values for one or more fields or properties for a newly created object, combining creation and initialization into a single step.
Query expressions, which provide a language-integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery.
Anonymous types, which are tuple types automatically inferred and created from object initializers.
Lab Objective
Estimated time to complete this lab: 60 minutes
The objective of this lab is to provide a clear understanding of the new language features in C# 3.0 and how the features work together to increase productivity and provide a rich, expressive way to work with data.
The lab is structured into eight exercises, each containing a varying number of tasks. Each exercise presents a new feature that utilizes features presented in preceding exercises. To start an exercise without completing the prior exercises, see the corresponding code sample in the appendix, which provides the prerequisites.
This lab consists of the following exercises:
|
Exercise I – Use of Automatically Implemented Properties
Oftentimes property accessors (get and set) have trivial implementations and follow the pattern that simply get (return) a private field and set the private field to the value passed. Automatically Implemented Properties provide a more concise syntax for implementing this pattern, where the C# compiler automatically generates the backing fields.
This exercise will show you how to create properties without creating additional unused fields.
