Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2_language_enhancements_hands_on_lab.doc
Скачиваний:
1
Добавлен:
01.05.2025
Размер:
411 Кб
Скачать
☆

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:

  • Use of Automatically Implemented Properties

  • Easy Initialization with Object and Collection Initializers

  • Implicitly Typed Local Variables and Implicitly Typed Arrays

  • Extending Types with Extension Methods

  • Working with Lambda Expressions

  • Using Lambda Expressions to Create Expression Trees

  • Understanding Queries and Query Expressions

  • Anonymous Types and Advanced Query Creation

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.