C#3.0 & Vb 9.0 New Features

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    C#3.0 & Vb 9.0 New Features - Presentation Transcript

    1. C# 3.0 and VB 9.0 New Features of VS2008
    2. C# and VB Feature List C# 3.0 VB 9.0 Extension Methods Lambda Expressions LINQ Expression Trees Extension Methods Lambda Expressions LINQ and Embedded XML Expression Trees
    3. Extension Methods
    4. Extension Methods in C#
      • Allows to extend existing types and constructed types with additional methods, without having to sub-class it or recompile the original type
      • Static methods that can be invoked using instance method syntax
    5. Extension Methods in C#
      • Applied not just to individual types, but also to parent base class or interface within DOTNET framework
      • If “this” keyword precedes an “object”, then it indicates that the extension method should be applied to all types that derive from the base System.Object base type. The method can be applied on every object in .NET
      • Powerful way to inject our own custom functionality into existing types
      • Functionally same as wrapper classes; only difference is Design time syntax
      • Extension method with same name as Class/Interface method will never be called.
      • Have lower priority than instance methods defined in type
    6. Extension Methods in C#
      • To add a new method to existing type, Create a static method in a static class and import the namespace in the class in which it is needed
    7. Extension Methods in VB
      • Decorated with “Extension” attribute (in System.Runtime.CompilerServices)
      • Preferred over Shared utility methods
      • Should be in a Module
    8. Pros of Extension Methods
      • Can be called on Null objects
      • Composability – Chaining of operations
      • LINQ
      • Add functionality without altering existing code
      • .NET types, third party types, older COM/Active X types can be extended
      • Provide concrete implementation to interfaces
    9. Cons of Extension Methods
      • Not a replacement to Inheritance
      • Code Hijack / Security
      • Adding a Real method to an object with same name as extension method gives precedence for Real method
      • Properties cannot be extended
      • Similar to Wrapper classes, only access public methods and cannot read/write protected members
      • Counter-Intuitive – Specified in the order opposite to the order of execution
    10. Guidelines for Extension Method
      • Can be used to extend a class/interface but not to override them
      • Use sparingly only when you have to
    11. Useful Extension Methods
      • http://www.extensionmethod.net/
    12. Lambda Expressions
    13. Lambda Expressions in C# 3.0
      • Compact way of writing functions in middle of expressions and obtaining a delegate to function we write.
    14. Lambda Expressions A lambda expression is written as a parameter list followed by => token and then followed by an expression or statement block to be executed when statement is invoked. Params=> expression
    15. Lambda Expression
      • Add (a, b) => (a + b)
      • AddChecked (a, b) => (a + b)
      • And (a, b) => (a And b)
      • AndAlso (a, b) => (a && b)
      • ArrayIndex (a, i) => a[i]
      • ArrayLength a => ArrayLength(a)
      • Call a => Abs(a)
      • Coalesce s => (s ?? "(null)")
      • Conditional (b, t, f) => IIF(b, t, f)
      • Constant () => 123
      • Convert a => Convert(a)
      • ConvertChecked a => ConvertChecked(a)
      • Divide (a, b) => (a / b)
      • Equal (a, b) => (a = b)
      • ExclusiveOr (a, b) => (a ^ b)
      • GreaterThan (a, b) => (a > b)
    16. Lambda Expression
      • GreaterThanOrEqual (a, b) => (a >= b)
      • Invoke a => Invoke(a)
      • Lambda a => a
      • LeftShift (a, s) => (a << s)
      • LessThan (a, b) => (a < b)
      • LessThanOrEqual (a, b) => (a <= b)
      • ListInit () => new List`1() {Void Add(Int32)(1)}
      • MemberAccess m => m.i
      • MemberInit a => new MemberAccess() {i = a}
      • Modulo (a, b) => (a % b)
      • Multiply (a, b) => (a * b)
      • MultiplyChecked (a, b) => (a * b)
      • Negate a => -a
      • NegateChecked a => -a
      • New n => new String(a, n)
    17. Lambda Expression
      • NewArrayBounds n => new System.Int32[*](n)
      • NewArrayInit () => new [] {}
      • Not b => Not(b)
      • NotEqual (a, b) => (a != b)
      • Or (a, b) => (a Or b)
      • OrElse (a, b) => (a || b)
      • Parameter a => a
      • Power (a, e) => (a ^ e)
      • Quote () => 1
      • RightShift (a, s) => (a >> s)
      • Subtract (a, b) => (a - b)
      • SubtractChecked (a, b) => (a - b)
      • TypeAs o => (o As String)
      • TypeIs o => (o Is String)
      • UnaryPlus a => +a
    18. Lambda Expression - Features
      • First class citizens
      • Can be returned from a function
      • Can be passed to a function
      • Can be used as Callback delegates
      • Return statement in lambda expression does not return an enclosing method
      • Cannot contain goto statement/ break statement/ continue statement whose target is outside the body or in the body of contained anonymous function
    19. Lambda Expressions - Uses
      • Used In LINQ Query expressions
      • Used in Construction of Expression trees
      • Used in place of Delegates
    20. Lambda Expressions - Cons
      • Can include only expressions and not statements
      • Single Expressions in VB 9.0
      • Variable captured will not be garbage collected unless the delegate that references it goes out of scope
      • Variables inside lambda expression are not visible outside the method
      • Cannot capture ref or out parameter from an enclosing method
      • Recursive Lambda expressions cannot be written
    21. LINQ
    22. Language Integrated Query (LINQ)
      • Express efficient query behavior in programming language, transforms the query results into any format, and easily manipulate the results.
      • Full type safety and compile time checking of query expressions
      • LINQ query returns objects of type IEnumerable
    23. A LINQ Query C# VB
    24. LINQ Operators
      • Restriction - Where
      • Projection - Select, SelectMany
      • Partitioning - Take, Skip, TakeWhile, SkipWhile
      • Ordering - OrderBy, OrderByDescending, ThenBy, ThenByDecending, Reverse
      • Grouping - GroupBy
      • Set - Distinct, Union, Intersect, Except
      • Conversion – ToArray, ToDictionary, ToList, OfType
    25. LINQ Operators
      • Element – First, FirstOrDefault, ElementAt
      • Generation – Range,Repeat
      • Quantifiers – Any, All
      • Aggregate - Count, Sum, Min, Max, Average, Fold
      • Miscellaneous - Concat, EqualAll
      • Custom Sequence - Combine
      • Query Execution - Deferred, Immediate, Query Reuse
    26. Typed Results using LINQ
    27. LINQ and Anonymous Types
    28. Pros of Type inference
      • Make Code Simpler
      • Used for creating Anonymous types
      • Iterate over IEnumerable<T> results where T is a projected anonymous type
      • Used in LINQ expressions
    29. LINQ with Lambda Expressions
      • Anonymous methods required parameter type to be explicitly stated.
      • Lambda expressions permit parameter types to be omitted and allow them to be inferred based on usage.
      • Explicit type declaration is also allowed
      Anonymous Methods
    30. Lambda Expressions in VB
      • Lambda expression is an Inline function that you can pass to something that takes the delegate
      • Func type – Allows 4 arguments as leading generic parameters and 1 last generic parameter as return type
    31. Evolution of LINQ
    32. Comparison of LINQ and SQL Query
    33. Projection
    34. Filtering
    35. Filtering
    36. Sub Query
    37. Joins Cross Join Inner Join
    38. Joins Outer Join
    39. Set Operators
    40. Set Operators
    41. Aggregate Operations
    42. LINQ Query Samples
      • 101 LINQ C# Samples
      • http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
      • 101 LINQ VB Samples
      • http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx
    43. DLINQ
      • Query expressions against Relational data
      • Data Context is the conduit to retrieve data from database and submit changes back
      • LINQ to SQL ORM
    44. Deferred Execution Immediate Execution DLINQ
    45. XLINQ
      • Query expressions against XML
    46. XLINQ API
    47. Pros of LINQ
      • Avoids dynamic creation of queries hardcoded as strings inside Code
      • Full type safety
      • Gives more power to manipulate data. Big advantage over datasets.
      • Extensibility and expression trees allow mostly consistent querying of multiple sources
      • Common syntax for querying any data source - Relational, XML or In memory datasets or .NET objects
      • Can be used against .NET 1.0 or .NET 2.0 objects
      • Able to Extend LINQ to support new classes and technologies
    48. Pros – LINQ (Continued)
      • Ability to access Stored procedures and user defined functions in Database
      • Integrated to .NET framework and Accessible from any .NET compliant language
      • Data manipulation logic/code is written in the application
      • Declarative approach makes queries easier to understand and more compact
      • Even in-process queries can be implemented in ways other than LINQ to Objects - e.g. Parallel LINQ and my own Push LINQ framework. Very flexible.
    49. Pros – LINQ (Continued)
      • Fabulously useful for in-process queries, where it's easiest to understand
      • Wide range of operators provided by default, and others can easily be added for LINQ to Objects
      • Language features introduced primarily for LINQ are widely applicable elsewhere
      • Consistent Domain Modeling
      • Hiding the mundane code
      • Short learning curve
      • Intelli-sense and Designer support in Visual Studio
    50. Cons of LINQ
      • Query expressions aren't understood well enough, and are overused. Often simple method invocation is shorter and simpler.
      • Inevitable inconsistencies between provider - impedance mismatch is still present, which is reasonable but needs to be understood
      • There will always be some things you can do in SQL but not in LINQ
      • Without understanding what's going on, it's easy to write very inefficient code
      • It's hard to write a LINQ provider.
      • It's a new way of thinking about data access for most developers, and will need time for understanding to percolate
    51. Cons of LINQ (Continued)
      • Some operators are &quot;missing&quot;, particularly the equivalents of OrderBy for things other than ordering - e.g. finding the item with the maximum value of a property
      • Deferred execution and streaming are poorly understood (but improving)
      • Debugging can be very tricky due to deferred execution and streaming
      • Data manipulation logic/code is written in the application. Cannot change logic without recompiling (Unlike Stored procedures)
      • Problem of viewing Execution plan and optimizing query
    52. Embedded XML
    53. Embedded XML in VB
      • VB 9.0 provides deep support for XLINQ through XML literals and late binding over XML.
      • XML Literals allow straight XML to be embedded into the source code. XML has become the first class citizen in Vb.NET.
    54. Embedded XML in VB
      • The XML API helps us constructing the XML as follows
      The compiler knows to use late binding over XML when the target expression is of type, or collection of, XElement, XDocument, or XAttribute.
    55. Embedded XML in VB
      • The XML Support together with LINQ makes it possible to dynamically query and generate an XML.
    56. Expression Trees
    57. What are Expression Trees
      • A form of Data structure
      • Represent Language level code in form of data
      • Data is stored in Tree shaped structure
      • Exposes parts of an expression by delineating code into data
      • Each node in tree is an expression
    58. Expression Tree Graph
    59. Expression Trees
      • Higher level representation of query expressions which permit lambda expressions to be represented as data (expression trees) instead of as code (delegates).
      C# VB
    60. Building Expression trees
      • Creating an expression
      Initialize the Members
    61. Building Expression trees
      • Form an expression
      Execute an expression
    62. Expression Tree Visualizer
    63. Uses of Expression Trees
      • Play main role in LINQ to SQL
      • LINQ to SQL queries returns objects of type IQueryable<T>
      • LINQ to SQL queries are not executed in C# program; Translated to SQL, sent across wire and executed in Database server
      • Code found in a query expression has to be translated into a SQL query
      • Easier to translate a data structure into SQL than executable code
      • Queries that return IEnumerable<T> do not use expression trees (LINQ to Objects)
    64. C# 4.0 and VB 10.0 (VS2010) C# 4.0 VB 10.0 Dynamic lookup Optional and Named Parameters Generic Co variance and Contra variance Improved COM interoperability No PIA Parallel Programming, PLINQ Dynamic Type Generic Co variance and Contra variance Improved COM interoperability No PIA Parallel Programming, PLINQ Anonymous Delegates Implicit Line continuation Auto Implemented Properties Collection Initialisers Array literals Nullable Optional Parameters
    SlideShare Zeitgeist 2009

    + techfreaktechfreak Nominate

    custom

    444 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 444
      • 444 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 8
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags