c#3.0 & vb 9.0 language enhancments

Post on 10-Sep-2014

1.634 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

C# 3.0 and VB 9.0 Language Enhancements of VS2008

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

Type Inference

Implicitly Typed Local variables in VB

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

• Early Bound

Option Infer

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

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

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

Pros of Type inference

• Make Code Simpler in case of Generic Types

Generics

C# 2.0 VB 8.0VB 8.0

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

Generics in C# 2.0A Type Conversion Error

Generics for Type Safety

Pros of Type inference• Improves Productivity• Make Code Simpler

• (More in Future slides)

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)

Implicitly Typed Arrays

Implicitly typed arrays in C#

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

Cons of Implicitly typed arrays

• One type per array

• Cannot use null along with value types

Automatic Properties

Properties in C#

• Conventional way of declaring Properties

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

Pros and Cons of Automatic Properties

• Pros – Make code simpler

• Cons – Cannot have Readonly or Writeonly properties

Object Initialisers

Object Instantiation

Object Initializers

C#

VB

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

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

Collection Initialisers

Collection Initialisers

• Convenient to initialize the array

• Improves Productivity

Pros - Collection Initializers

When used along with Object Initialisers

Conventional Way

Anonymous Types

Anonymous Methods

C# 2.0 VB 8.0VB 8.0

Anonymous Methods in C# 2.0

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

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#

Anonymous Types

• Anonymous types can also infer the field name

In this case, a property called Now will be created

Pros of Type inference• Make Code Simpler

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

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

Partial Methods

Partial Types

C# 2.0 VB 8.0 VB 8.0

Partial Types in C# 2.0

Partial Types in VB 8.0

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

Partial Methods in C#

Partial Methods in VB

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

Nullable Types

Nullable Types in C# 2.0

Value Types that can be assigned null value

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.

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

If Ternary operator

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.

Friend Assemblies

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.

Friend Assemblies

Now the greeting class can be instantiated in VBWinApp assembly

This feature was supported in C# 2.0.

Relaxed Delegates

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.

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.

Iterators

C# 2.0 VB 8.0 VB 8.0

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

Iterators

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

C# and VB New Features

C# 3.0 VB 9.0

Extension MethodsLambda ExpressionsLINQExpression Trees

Extension MethodsLambda ExpressionsLINQExpression TreesEmbedded XML

top related