C#
  Home arrow C# arrow Page 3 - C# 3.0 Extension Methods
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# 3.0 Extension Methods
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-11-06

    Table of Contents:
  • C# 3.0 Extension Methods
  • Ambiguity and Resolution
  • Anonymous Types (C# 3.0)
  • Attributes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C# 3.0 Extension Methods - Anonymous Types (C# 3.0)


    (Page 3 of 4 )

    An anonymous type is a simple class created on the fly to store a set of values. To create an anonymous type, use the new keyword followed by an object initializer, specifying the properties and values the type will contain. For example:

      var dude = new { Name = "Bob", Age = 1 };

    The compiler translates this to the following:

      internal class AnonymousGeneratedTypeName
      {
        private string name;   // actual field name is irrelevant
        private int    age;    // actual field name is irrelevant

        public string  Name get {return name;} {set {name = value;}}
       
    public int     Age get  {return age; } {set {age = value; }}
      }
      ...

      AnonymousGeneratedTypeName dude = new AnonymousGeneratedTypeName ();
      dude.Name = "Bob";
      dude.Age = 1;

    You must use thevar keyword to reference an anonymous type, because the name of the type is anonymous.

    The property name of an anonymous type can be inferred from an expression that is itself an identifier. For example:

      int Age = 1;
      var dude = new { Name = "Bob", Age };

    is equivalent to:

      var dude = new { Name = "Bob", Age = Age };

    Anonymous types are used primarily when writing LINQ queries (see Chapter 8).

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 in a Nutshell, Third Edition, A...
     

    Buy this book now. This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - C# Meets Design Patterns
    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek