views:

127

answers:

4

i've been programming in C#/asp.net for some years now (mostly basic programs and websites),and lately i started reading about .net features and programming techniques like Linq,lambda expressions,Generics and such..

Features List:

  • Object Initializers
  • Extension Methods
  • Anonymous Types
  • Generics
  • Lambda Expressions
  • LINQ

  • What i am trying to find is:

    -When is using Such features more appropriate than the traditional/alternative ways?

    -Why would i prefer using them ?

    -How can i force/integrate those skills i am learning into my current programming style?

    i know the first two questions are broad ones, so providing links to articles,blogs that discuss the usage of these features or even questions on SO that cover some of the features would be much appreciated.

    looking for suggestions -short or illustrated- for the 3rd one :).

    A: 

    The answer to all three questions is relatively simple - it allows you to write code faster/more efficiently.

    You should be learning and using these as it will improve your ability to produce working, maintainable applications in a timely fashion.

    Paddy
    +3  A: 

    Use LINQ to avoid following..

    • manipulative iterative code
    • improved readability over iterative code
    • leverage your sql query skills as first-class .Net language feature

    Advantages of using LINQ [as found on MSDN]

    • Familiar syntax for writing queries.
    • Compile-time checking for syntax errors and type safety.
    • Improved debugger support.
    • IntelliSense support.
    • Ability to work directly with XML elements instead of creating a container XML document, as required with W3C DOM.
    • In-memory XML document modification that is powerful, yet simpler to use than XPath or XQuery.
    • Powerful filtering, ordering, and grouping capabilities.
    • Consistent model for working with data across various kinds of data sources and formats.

    Extension Methods..

    this. __curious_geek
    +1 dividing the answer ,putting into points and continuously updating :)
    Madi D.
    +2  A: 

    The features you have listed can be split into a couple of categories, firstly there are the features that are purely syntactical sugar, they don't allow you to do anything new, they just help you write cleaner code that is easier to read. The features fit in this category are:

    • Object Initializers (avoid overloaded constructors, or many lines of initialization code)
    • Extension Methods (neater way to access static methods in helper classes)
    • Lambda Expressions (when used as a replacement for delegates)
    • Anonymous Types (replace small, throwaway classes in certain circumstances - mainly used to support LINQ and not often used in your own code)

    Two of the other features actually provide you a programming benefit which may improve the performance of your application

    • Generics (helps avoid boxing on value types)
    • LINQ (when used to defer intensive queries until a more suitable time (IQueryable))

    But the main power of LINQ is as an integrated query language, allowing simple, type safe methods for querying datasets (a database, XML, objects etc) in a more OO less SQL way.

    Therefore there is no great advantage in forcing yourself to use the first three. Personally I think they are all good things to get into the habit of using, but you won't see any major benefit from them if you don't like the syntax or find them unnatural to use. If you do want to use them then code style plugins such as Resharper or CodeRush will have features you can turn on to warn you when you could use the new style instead of your longhand code.

    You can start using generics by relying on the supplied generic collections rather than the old .NET 1 lists and arrays. There are cases where you will want to write your own generic classes, but you should have a good handle on them by the time that happens if you start leveraging the supplied ones now.

    LINQ will either be fantastically useful to you or completely pointless, depending on how much data you access, if you think you'll need it I'd suggest looking at LINQPad - it gives you a very nice sandbox to play in where you can get a feel for the way the language expresses concepts. You'll either love it (most do) or see no reason for it in your code.

    Martin Harris
    +1 for the really useful answer and the nice approach of divide and answer.. :)
    Madi D.
    I disagree with the contention that object initializers are mere sugar. Object and collection initializers change the context of a complex initialization from a *statement* context to an *expression* context and thereby enable manipulation of such constructs via expression trees. That's an important new feature.
    Eric Lippert
    +2  A: 

    This is a huge and vague question, so you'll get a vague answer. The point of those features is:

    • to increase the representational power of the language; that is, to enable you to communicate more complex information in fewer pages of code
    • to increase the richness of the type system; that is, to enable you to use the type system to reason about the flow of data through the program
    • to raise the level of coding above the imperative mechanism level and into the declarative semantics level. That is, rather than saying "compiler, tell the program to open the fridge, get the bread, put a slice in the toaster..." just say "I require breakfast, compiler, go do what you need to do".
    Eric Lippert
    +1 for the simple,yet very rich answer.. and the example :D
    Madi D.