c#3.0 & vb 9.0 language enhancments

58
C# 3.0 and VB 9.0 Language Enhancements of VS2008

Upload: techfreak

Post on 10-Sep-2014

1.634 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C#3.0 & Vb 9.0 Language Enhancments

C# 3.0 and VB 9.0 Language Enhancements of VS2008

Page 2: C#3.0 & Vb 9.0 Language Enhancments

C# and VB Feature List

C# 3.0 VB 9.0

Type InferenceImplicitly Typed ArraysAutomatic PropertiesObject InitialisersCollection InitialisersAnonymous TypesPartial Methods

Type InferenceObject InitialisersAnonymous TypesPartial MethodsNullable TypesIf Ternary operatorFriend AssembliesRelaxed Delegates

Page 3: C#3.0 & Vb 9.0 Language Enhancments

Type Inference

Page 4: C#3.0 & Vb 9.0 Language Enhancments

Implicitly Typed Local variables in VB

• Type of local variable is inferred from the expression used to initialize the variable

• Early Bound

Page 5: C#3.0 & Vb 9.0 Language Enhancments

Option Infer

• VB has “Option Infer” to enable or disable the type inference mechanism.

Page 6: C#3.0 & Vb 9.0 Language Enhancments

Type Inference in C#• Declared with “var” keyword

• Type of the variable based on the value assigned to it.• Following code gives error because compiler can’t figure out

what type the variable is

Page 7: C#3.0 & Vb 9.0 Language Enhancments

Insight into var

• Var is not var as in Javascript• Once compiler identifies the type of variable, from

then on, it is used as if it is of that type• Assigning value of different type will throw compile

time error

Page 8: C#3.0 & Vb 9.0 Language Enhancments

Pros of Type inference

• Make Code Simpler in case of Generic Types

Page 9: C#3.0 & Vb 9.0 Language Enhancments

Generics

C# 2.0 VB 8.0VB 8.0

Page 10: C#3.0 & Vb 9.0 Language Enhancments

Generics

• To tailor a method, class, structure or interface to the precise data type that it acts upon.

• Classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use.

C#

VB

Page 11: C#3.0 & Vb 9.0 Language Enhancments

Generics in C# 2.0A Type Conversion Error

Generics for Type Safety

Page 12: C#3.0 & Vb 9.0 Language Enhancments

Pros of Type inference• Improves Productivity• Make Code Simpler

• (More in Future slides)

Page 13: C#3.0 & Vb 9.0 Language Enhancments

Cons of Type Inference• Don’t use for assigning constants to variables of built in types• Don’t use when the assigning expression does not have a clear type• Don’t use in case of abstractions

(More in future slides)

Page 14: C#3.0 & Vb 9.0 Language Enhancments

Implicitly Typed Arrays

Page 15: C#3.0 & Vb 9.0 Language Enhancments

Implicitly typed arrays in C#

• Form of array creation and initialization that infers the element type of the array from an array initializer.

Page 16: C#3.0 & Vb 9.0 Language Enhancments

Cons of Implicitly typed arrays

• One type per array

• Cannot use null along with value types

Page 17: C#3.0 & Vb 9.0 Language Enhancments

Automatic Properties

Page 18: C#3.0 & Vb 9.0 Language Enhancments

Properties in C#

• Conventional way of declaring Properties

Page 19: C#3.0 & Vb 9.0 Language Enhancments

Automatic Properties in C#

• No need to manually declare a private field and write the get/set logic.

• Compiler can automate creating the private field and the default get/set operations for you

Page 20: C#3.0 & Vb 9.0 Language Enhancments

Pros and Cons of Automatic Properties

• Pros – Make code simpler

• Cons – Cannot have Readonly or Writeonly properties

Page 21: C#3.0 & Vb 9.0 Language Enhancments

Object Initialisers

Page 22: C#3.0 & Vb 9.0 Language Enhancments

Object Instantiation

Page 23: C#3.0 & Vb 9.0 Language Enhancments

Object Initializers

C#

VB

• Allow us to initialize an object by invoking its constructor and setting its properties

Page 24: C#3.0 & Vb 9.0 Language Enhancments

Pros of Object Initialisers• Concise object instantiation• Can be nested and reduce unwanted variables

Page 25: C#3.0 & Vb 9.0 Language Enhancments

Collection Initialisers

Page 26: C#3.0 & Vb 9.0 Language Enhancments

Collection Initialisers

• Convenient to initialize the array

• Improves Productivity

Page 27: C#3.0 & Vb 9.0 Language Enhancments

Pros - Collection Initializers

When used along with Object Initialisers

Conventional Way

Page 28: C#3.0 & Vb 9.0 Language Enhancments

Anonymous Types

Page 29: C#3.0 & Vb 9.0 Language Enhancments

Anonymous Methods

C# 2.0 VB 8.0VB 8.0

Page 30: C#3.0 & Vb 9.0 Language Enhancments

Anonymous Methods in C# 2.0

• Allow Code blocks to be written “in-line” where delegate values are expected

Page 31: C#3.0 & Vb 9.0 Language Enhancments

Anonymous Types• Allow the creation of inline types without explicitly defining class declaration for the type• Compiler creates a new Anonymous type at compile time.

VB

C#

Page 32: C#3.0 & Vb 9.0 Language Enhancments

Anonymous Types

• Anonymous types can also infer the field name

In this case, a property called Now will be created

Page 33: C#3.0 & Vb 9.0 Language Enhancments

Pros of Type inference• Make Code Simpler

• Used for creating Anonymous types • (More in future slides)

Page 34: C#3.0 & Vb 9.0 Language Enhancments

Cons of Type Inference• Don’t use for assigning constants to variables of built in types• Don’t use when the assigning expression does not have a clear type• Don’t use in case of abstractions

• Don’t use for smart type inference in loops unless an anonymous type is iterated

Page 35: C#3.0 & Vb 9.0 Language Enhancments

Partial Methods

Page 36: C#3.0 & Vb 9.0 Language Enhancments

Partial Types

C# 2.0 VB 8.0 VB 8.0

Page 37: C#3.0 & Vb 9.0 Language Enhancments

Partial Types in C# 2.0

Page 38: C#3.0 & Vb 9.0 Language Enhancments

Partial Types in VB 8.0

Page 39: C#3.0 & Vb 9.0 Language Enhancments

Partial Methods

• Help in lightweight event handling• When a partial method has no body, then it is removed at

compile time and is not emitted to metadata.• Declared within Partial classes and are indicated by a partial

modifier. • They must return void and are not marked with Access

Modifiers like Private, public, internal

Page 40: C#3.0 & Vb 9.0 Language Enhancments

Partial Methods in C#

Page 41: C#3.0 & Vb 9.0 Language Enhancments

Partial Methods in VB

Page 42: C#3.0 & Vb 9.0 Language Enhancments

Pros and Cons of Partial Methods

• Pros– Method can be split across multiple class files

• Cons – Can have ref parameters but not out parameters– Implicitly marked private. Hence cannot be called from

outside Partial class– Cannot be declared virtual– Cannot be marked extern

Page 43: C#3.0 & Vb 9.0 Language Enhancments

Nullable Types

Page 44: C#3.0 & Vb 9.0 Language Enhancments

Nullable Types in C# 2.0

Value Types that can be assigned null value

Page 45: C#3.0 & Vb 9.0 Language Enhancments

Nullable Types in VB 9.0

• CLR has added run-time support for nullability using the generic type Nullable(Of T As Struct).

• Using this type we can declare nullable versions of value types such as Integer, Boolean, Date, etc.

Page 46: C#3.0 & Vb 9.0 Language Enhancments

Nullable Types in VB 9.0

• VB 9.0 will support three-valued logic and null propagation arithmetic on nullable values.

• If one of the operands of an arithmetic, comparison, logical or bitwise, shift, string, or type operation is Nothing, the result will be Nothing

• If both operands are proper values, the operation is performed on the underlying values of the operands and the result is converted to nullable

Page 47: C#3.0 & Vb 9.0 Language Enhancments

If Ternary operator

Page 48: C#3.0 & Vb 9.0 Language Enhancments

Ternary Operator

• Similar to Null Coalesce operator in C# 2.0. • If the variable evaluates to Null, then a special value can be

assigned using ternaryoperator.

However the following generates an error. The alternate value given has to match the data type of variable.

Page 49: C#3.0 & Vb 9.0 Language Enhancments

Friend Assemblies

Page 50: C#3.0 & Vb 9.0 Language Enhancments

Friend Assemblies

• Visible inside the assembly in which they are defined. • Exposes Friend types to those assemblies which are

considered Friend. • Declared in the attribute “InternalsVisibleTo” in

System.Runtime.CompilerServices.

Page 51: C#3.0 & Vb 9.0 Language Enhancments

Friend Assemblies

Now the greeting class can be instantiated in VBWinApp assembly

This feature was supported in C# 2.0.

Page 52: C#3.0 & Vb 9.0 Language Enhancments

Relaxed Delegates

Page 53: C#3.0 & Vb 9.0 Language Enhancments

Consider the following Traditional way of Event handling.

Delegates in VB

When creating a delegate in VB 8, one of the methods targeted for binding to the delegate identifier must exactly match the signature of the delegate’s type.

Page 54: C#3.0 & Vb 9.0 Language Enhancments

Relaxed Delegates in VB• In VB 9, Delegates can be relaxed to take no arguments, if the

arguments are not needed in the methods invoked.• By Relaxed Delegates, the Event Handler no longer needs to be

have same signature as Delegate.

Page 55: C#3.0 & Vb 9.0 Language Enhancments

Iterators

C# 2.0 VB 8.0 VB 8.0

Page 56: C#3.0 & Vb 9.0 Language Enhancments

Iterators in C# 2.0• Method, get accessor or operator that enables you to support foreach iteration in

a class or struct without having to implement the entire IEnumerable interface. • Simply traverses the data structures in your class• When the compiler detects your iterator, it will automatically generate the

Current, MoveNext and Dispose methods of the IEnumerable interface

Page 57: C#3.0 & Vb 9.0 Language Enhancments

Iterators

• The iterator code uses the yield return statement to return each element in turn. yield break ends the iteration

Page 58: C#3.0 & Vb 9.0 Language Enhancments

C# and VB New Features

C# 3.0 VB 9.0

Extension MethodsLambda ExpressionsLINQExpression Trees

Extension MethodsLambda ExpressionsLINQExpression TreesEmbedded XML