15
Visual Studio 2008 Training 3.5 RTM Mohamed Saleh [email protected] www.jordev.net www.geeksconnected.com/mohamed

Module 2: C# 3.0 Language Enhancements (Slides)

Embed Size (px)

DESCRIPTION

Module 2: C# 3.0 Language Enhancements (PowerPoint Slides)Jordan .NET User Group (Jordev)Community MaterialMohamed Saleh

Citation preview

Page 2: Module 2: C# 3.0 Language Enhancements (Slides)

Module 2: C# 3.0 Language Enhancements

Page 3: Module 2: C# 3.0 Language Enhancements (Slides)

Overview

Automate the process of creating propertiesEnhance the objects and collections using the InitializersCreate implicitly typed local variablesExtend existing types using Extension MethodsWrite the new lambda expressionsUsing the new features in multi-framework versions

Page 4: Module 2: C# 3.0 Language Enhancements (Slides)

Automatically Implemented Properties

Auto-Implemented Properties can handle the trivial implementation of getter and setter.The compiler will generate hidden backing fields with the implementation.This feature is related to the compiler and not the Framework Version or the Intermediate Language.The Auto-Implemented Properties can be implemented using the following syntax:

public string Name { get; set; }

Auto-Implemented Properties Overview

Page 5: Module 2: C# 3.0 Language Enhancements (Slides)

Lab 1: Using Auto-Implemented Properties

Using the Automatic Implemented Properties.Creating read-only and write-only properties.Using Automatic Implemented Properties with multi-framework versions.Examining the effects of the Automatic Implemented Properties on the generated intermediate language.

Page 6: Module 2: C# 3.0 Language Enhancements (Slides)

Object and Collection Initializers

Object Initializers allows the developer to create the object instance and assign the initial values at the same time.The C# 2.0 way of initializing objects:

Customer cst2 = new Customer();cst2.ID = 2;cst2.Name = "Ayman Farouk";cst2.Phone = "0799-987-980-98";

The C# 3.0 way of initializing objects:

Customer cst3 = new Customer() { ID = 3, Name = "Osama Salam", Phone = "074-545-545-67" };

Object Initializer Overview

Page 7: Module 2: C# 3.0 Language Enhancements (Slides)

Object and Collection Initializers

The collection Initializers allows the developer to specify one or more elements Initializers when initializing any type that implements the System.Collections.Generic.IEnumerable<T>.

Initializers Rules:1. Object Initializers cannot include more than one member initializer

for the same field or property.

2. The Object Initializers cannot refer to the newly created object it is initializing.

3. The Collection type must implements System.Collections.Generic.IEnumerable<T> in order to have initializers.

Collection Initializers Overview

Page 8: Module 2: C# 3.0 Language Enhancements (Slides)

Lab 2: Using Initializers

Writing Object Initializer expressions.Writing Collection Initializer expressions.Using the nested object initializer. Using Initializers with multi-framework versions.Examining the generated initialization instructions in the intermediate language.

Page 9: Module 2: C# 3.0 Language Enhancements (Slides)

Implicit Typing

The new keyword var allows the C# compiler to infers the type of variablesthe compiler will determines the appropriate CLR typeVar differ than variant keyword in VB6 and COMImplicit Typing Context:

1. Declaring variable at the method/property scope2. In a for loop statement.

3. In a foreach loop statement.

4. In a using statement.

Implicit Typing Overview

Page 10: Module 2: C# 3.0 Language Enhancements (Slides)

Lab 3: Using Implicit Typing

Using implicit-typed variables.Using implicit typing with foreach context.Using implicit typing with custom classes and lists. Using Implicit Typing with multi-framework versions.Examining the types of the implicit-typed variables.

Page 11: Module 2: C# 3.0 Language Enhancements (Slides)

Extension Methods

Extension Methods allows the developer to inject new methods to the existing compiled types without the need to re-write or override the current implementations.Extension Methods are natively supported in the VS 2008 IDE.Defining Extension Methods:

1. Must be defined in separated static class.2. Must be declared as static methods.3. The first parameter modifier of the extension methods must be this

keyword.

Extension Methods Overview

Page 12: Module 2: C# 3.0 Language Enhancements (Slides)

Lab 4: Using Extension Methods

Extending types with extension methods.Consuming extension methods.Extending different .NET Framework built-in types.

Page 13: Module 2: C# 3.0 Language Enhancements (Slides)

Lambda Expressions

Lambda expressions allows the developer to write functions in expression context instead of writing it the regular method body with a name.it consists of two sides separated by the lambda operator => “goes to” the left side specifies the parameters if any, and the right side holds the statement block or the expression.Lambda Expression Limitation:

1. It can be only used as a part of statement.2. The lambda expression does not have a name.3. Lambda expression cannot contain a goto, break, or continue

statement whose target is outside the body.

Lambda Expressions Overview

Page 14: Module 2: C# 3.0 Language Enhancements (Slides)

Lab 5: Writing Expression Methods

Writing lambda expressionsUsing the Lambda expressions.Understanding the different in writing expressions using delegates, anonymous methods, and lambda expressions.

Page 15: Module 2: C# 3.0 Language Enhancements (Slides)

Review

In this module, you learned to:

Examine the Auto Properties FeatureWork with InitializersUse the Implicit Typing FeatureExtending Existing TypesWriting Lambda Expressions