Module 2: C# 3.0 Language Enhancements (Slides)

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)


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

Transcript of Module 2: C# 3.0 Language Enhancements (Slides)

  • Visual Studio 2008 Training 3.5 RTMMohamed [email protected]/mohamed
  • Module 2:C# 3.0 Language Enhancements
  • Overview Automate the process of creating properties Enhance the objects and collections using the Initializers Create implicitly typed local variables Extend existing types using Extension Methods Write the new lambda expressions Using the new features in multi-framework versions
  • Automatically Implemented Properties Auto-Implemented Properties Overview 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; }
  • 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.
  • Object and Collection Initializers Object Initializer Overview 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 and Collection Initializers Collection Initializers Overview The collection Initializers allows the developer to specify one or more elements Initializers when initializing any type that implements the System.Collections.Generic.IEnumerable. 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 in order to have initializers.
  • 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.
  • Implicit Typing Implicit Typing Overview The new keyword var allows the C# compiler to infers the type of variables the compiler will determines the appropriate CLR type Var differ than variant keyword in VB6 and COM Implicit Typing Context: 1. Declaring variable at the method/property scope 2. In a for loop statement. 3. In a foreach loop statement. 4. In a using statement.
  • 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.
  • Extension Methods Extension Methods Overview 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.
  • Lab 4: Using Extension Methods Extending types with extension methods. Consuming extension methods. Extending different .NET Framework built- in types.
  • Lambda Expressions Lambda Expressions Overview 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.
  • Lab 5: Writing Expression Methods Writing lambda expressions Using the Lambda expressions. Understanding the different in writing expressions using delegates, anonymous methods, and lambda expressions.
  • Review In this module, you learned to: Examine the Auto Properties Feature Work with Initializers Use the Implicit Typing Feature Extending Existing Types Writing Lambda Expressions