C#3.0 & Vb 9.0 Language Enhancments


Published on

Published in: Technology
1 Comment
  • For brief detail of nullable type in c# please refer
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C#3.0 & Vb 9.0 Language Enhancments

  1. 1. C# 3.0 and VB 9.0 Language Enhancements of VS2008
  2. 2. C# and VB Feature List C# 3.0 VB 9.0 Type Inference Implicitly Typed Arrays Automatic Properties Object Initialisers Collection Initialisers Anonymous Types Partial Methods Type Inference Object Initialisers Anonymous Types Partial Methods Nullable Types If Ternary operator Friend Assemblies Relaxed Delegates
  3. 3. Type Inference
  4. 4. Implicitly Typed Local variables in VB <ul><li>Type of local variable is inferred from the expression used to initialize the variable </li></ul><ul><li>Early Bound </li></ul>
  5. 5. Option Infer <ul><li>VB has “Option Infer” to enable or disable the type inference mechanism. </li></ul>
  6. 6. Type Inference in C# <ul><li>Declared with “var” keyword </li></ul><ul><li>Type of the variable based on the value assigned to it. </li></ul><ul><li>Following code gives error because compiler can’t figure out what type the variable is </li></ul>
  7. 7. Insight into var <ul><li>Var is not var as in Javascript </li></ul><ul><li>Once compiler identifies the type of variable, from then on, it is used as if it is of that type </li></ul><ul><li>Assigning value of different type will throw compile time error </li></ul>
  8. 8. Pros of Type inference <ul><li>Make Code Simpler in case of Generic Types </li></ul>
  9. 9. <ul><li>Generics </li></ul>C# 2.0 VB 8.0
  10. 10. Generics <ul><li>To tailor a method, class, structure or interface to the precise data type that it acts upon. </li></ul><ul><li>Classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use. </li></ul>C# VB
  11. 11. Generics in C# 2.0 A Type Conversion Error Generics for Type Safety
  12. 12. Pros of Type inference <ul><li>Improves Productivity </li></ul><ul><li>Make Code Simpler </li></ul><ul><li>(More in Future slides) </li></ul>
  13. 13. Cons of Type Inference <ul><li>Don’t use for assigning constants to variables of built in types </li></ul><ul><li>Don’t use when the assigning expression does not have a clear type </li></ul><ul><li>Don’t use in case of abstractions </li></ul><ul><li>(More in future slides) </li></ul>
  14. 14. Implicitly Typed Arrays
  15. 15. Implicitly typed arrays in C# <ul><li>Form of array creation and initialization that infers the element type of the array from an array initializer. </li></ul>
  16. 16. Cons of Implicitly typed arrays <ul><li>One type per array </li></ul><ul><li>Cannot use null along with value types </li></ul>
  17. 17. Automatic Properties
  18. 18. Properties in C# <ul><li>Conventional way of declaring Properties </li></ul>
  19. 19. Automatic Properties in C# <ul><li>No need to manually declare a private field and write the get/set logic. </li></ul><ul><li>Compiler can automate creating the private field and the default get/set operations for you </li></ul>
  20. 20. Pros and Cons of Automatic Properties <ul><li>Pros </li></ul><ul><ul><li>Make code simpler </li></ul></ul><ul><li>Cons </li></ul><ul><ul><li>Cannot have Readonly or Writeonly properties </li></ul></ul>
  21. 21. Object Initialisers
  22. 22. Object Instantiation
  23. 23. Object Initializers <ul><li>Allow us to initialize an object by invoking its constructor and setting its properties </li></ul>C# VB
  24. 24. Pros of Object Initialisers <ul><li>Concise object instantiation </li></ul><ul><li>Can be nested and reduce unwanted variables </li></ul>
  25. 25. Collection Initialisers
  26. 26. Collection Initialisers <ul><li>Convenient to initialize the array </li></ul><ul><li>Improves Productivity </li></ul>
  27. 27. Pros - Collection Initializers When used along with Object Initialisers Conventional Way
  28. 28. Anonymous Types
  29. 29. <ul><li> Anonymous Methods </li></ul>C# 2.0 VB 8.0
  30. 30. Anonymous Methods in C# 2.0 <ul><li>Allow Code blocks to be written “in-line” where delegate values are expected </li></ul>
  31. 31. Anonymous Types <ul><li>Allow the creation of inline types without explicitly defining class declaration for the type </li></ul><ul><li>Compiler creates a new Anonymous type at compile time. </li></ul>VB C#
  32. 32. Anonymous Types <ul><li>Anonymous types can also infer the field name </li></ul><ul><li>In this case, a property called Now will be created </li></ul>
  33. 33. Pros of Type inference <ul><li>Make Code Simpler </li></ul><ul><li>Used for creating Anonymous types </li></ul><ul><li>(More in future slides) </li></ul>
  34. 34. Cons of Type Inference <ul><li>Don’t use for assigning constants to variables of built in types </li></ul><ul><li>Don’t use when the assigning expression does not have a clear type </li></ul><ul><li>Don’t use in case of abstractions </li></ul><ul><li>Don’t use for smart type inference in loops unless an anonymous type is iterated </li></ul>
  35. 35. Partial Methods
  36. 36. <ul><li>Partial Types </li></ul>C# 2.0 VB 8.0
  37. 37. Partial Types in C# 2.0
  38. 38. Partial Types in VB 8.0
  39. 39. Partial Methods <ul><li>Help in lightweight event handling </li></ul><ul><li>When a partial method has no body, then it is removed at compile time and is not emitted to metadata. </li></ul><ul><li>Declared within Partial classes and are indicated by a partial modifier. </li></ul><ul><li>They must return void and are not marked with Access Modifiers like Private, public, internal </li></ul>
  40. 40. Partial Methods in C#
  41. 41. Partial Methods in VB
  42. 42. Pros and Cons of Partial Methods <ul><li>Pros </li></ul><ul><ul><li>Method can be split across multiple class files </li></ul></ul><ul><li>Cons </li></ul><ul><ul><li>Can have ref parameters but not out parameters </li></ul></ul><ul><ul><li>Implicitly marked private. Hence cannot be called from outside Partial class </li></ul></ul><ul><ul><li>Cannot be declared virtual </li></ul></ul><ul><ul><li>Cannot be marked extern </li></ul></ul>
  43. 43. Nullable Types
  44. 44. Nullable Types in C# 2.0 Value Types that can be assigned null value
  45. 45. Nullable Types in VB 9.0 <ul><li>CLR has added run-time support for nullability using the generic type Nullable(Of T As Struct). </li></ul><ul><li>Using this type we can declare nullable versions of value types such as Integer, Boolean, Date, etc. </li></ul>
  46. 46. Nullable Types in VB 9.0 <ul><li>VB 9.0 will support three-valued logic and null propagation arithmetic on nullable values. </li></ul><ul><li>If one of the operands of an arithmetic, comparison, logical or bitwise, shift, string, or type operation is Nothing, the result will be Nothing </li></ul><ul><li>If both operands are proper values, the operation is performed on the underlying values of the operands and the result is converted to nullable </li></ul>
  47. 47. If Ternary operator
  48. 48. Ternary Operator <ul><li>Similar to Null Coalesce operator in C# 2.0. </li></ul><ul><li>If the variable evaluates to Null, then a special value can be assigned using ternaryoperator. </li></ul>However the following generates an error. The alternate value given has to match the data type of variable.
  49. 49. Friend Assemblies
  50. 50. Friend Assemblies <ul><li>Visible inside the assembly in which they are defined. </li></ul><ul><li>Exposes Friend types to those assemblies which are considered Friend. </li></ul><ul><li>Declared in the attribute “InternalsVisibleTo” in System.Runtime.CompilerServices. </li></ul>
  51. 51. Friend Assemblies Now the greeting class can be instantiated in VBWinApp assembly This feature was supported in C# 2.0.
  52. 52. Relaxed Delegates
  53. 53. <ul><li>Consider the following Traditional way of Event handling. </li></ul>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.
  54. 54. Relaxed Delegates in VB <ul><li>In VB 9, Delegates can be relaxed to take no arguments, if the arguments are not needed in the methods invoked. </li></ul><ul><li>By Relaxed Delegates, the Event Handler no longer needs to be have same signature as Delegate. </li></ul>
  55. 55. <ul><li> Iterators </li></ul>C# 2.0 VB 8.0
  56. 56. Iterators in C# 2.0 <ul><li>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. </li></ul><ul><li>Simply traverses the data structures in your class </li></ul><ul><li>When the compiler detects your iterator, it will automatically generate the Current, MoveNext and Dispose methods of the IEnumerable interface </li></ul>
  57. 57. Iterators <ul><li>The iterator code uses the yield return statement to return each element in turn. yield break ends the iteration </li></ul>
  58. 58. C# and VB New Features C# 3.0 VB 9.0 Extension Methods Lambda Expressions LINQ Expression Trees Extension Methods Lambda Expressions LINQ Expression Trees Embedded XML