Anonymous Types In C#

Introduction

Anonymous types allow us to create new type without defining them. This is way to defining read only properties into a single object without having to define type explicitly. Here Type is generating by the compiler and it is accessible only for the current block of code. The type of properties is also inferred by the compiler.

We can create anonymous types by using “new” keyword together with the object initializer.

Example

  1. var anonymousData = new {  
  2.     ForeName = "Jignesh",  
  3.         SurName = "Trivedi"  
  4. };  
  5. Console.WriteLine("First Name : " + anonymousData.ForeName);  

 

Anonymous Types with LINQ Example

Anonymous types are also used with the "Select" clause of LINQ query expression to return subset of properties.

Example

If Any object collection having properties called FirstName , LastName, DOB etc. and you want only FirstName and LastName after the Querying the data then.

  1. class MyData {  
  2.     public string FirstName {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string LastName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public DateTime DOB {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string MiddleName {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
  19. static void Main(string[] args) {  
  20.     // Create Dummy Data to fill Collection.  
  21.     List < MyData > data = new List < MyData > ();  
  22.     data.Add(new MyData {  
  23.         FirstName = "Jignesh", LastName = "Trivedi", MiddleName = "G", DOB = new DateTime(1990, 12, 30)  
  24.     });  
  25.     data.Add(new MyData {  
  26.         FirstName = "Tejas", LastName = "Trivedi", MiddleName = "G", DOB = new DateTime(1995, 11, 6)  
  27.     });  
  28.     data.Add(new MyData {  
  29.         FirstName = "Rakesh", LastName = "Trivedi", MiddleName = "G", DOB = new DateTime(1993, 10, 8)  
  30.     });  
  31.     data.Add(new MyData {  
  32.         FirstName = "Amit", LastName = "Vyas", MiddleName = "P", DOB = new DateTime(1983, 6, 15)  
  33.     });  
  34.     data.Add(new MyData {  
  35.         FirstName = "Yash", LastName = "Pandiya", MiddleName = "K", DOB = new DateTime(1988, 7, 20)  
  36.     });  
  37. }  
  38. var anonymousData = from pl in data  
  39. select new {  
  40.     pl.FirstName, pl.LastName  
  41. };  
  42. foreach(var m in anonymousData) {  
  43.     Console.WriteLine("Name : " + m.FirstName + " " + m.LastName);  
  44. }  
  45. }  
Anonymous Types

Anonymous type is class type which is directly derived from “System.Object” and it cannot be cast any type other than the object. The Compiler generates a name for each anonymous type. If two or more anonymous type objects are define in the same assembly and the sequence of properties are same in terms of names and types than the compiler treats as same object instances of type. It cannot be declared fields, events, or the return type of a method which having anonymous type. Same as it cannot be declared formal parameter of method, property, constructor or indexer which having anonymous type.

Anonymous type VS Dynamic Type

  • NET framework 4.0 introduced new keyword called "Dynamic". Object of type dynamic bypasses the static type checking at time of compilation whereas for anonymous type, creating static type and checking type at compile time.
  • Anonymous type is a class type that contain one or more read only properties whereas dynamic can be any type it may be any type integer, string, object or class.
  • Anonymous types are assigned type by the compiler.
  • Anonymous type is directly derived from System.Object whereas Dynamic is differ from object type and DLR (dynamic Language Runtime) define and assign type runtime.
  • Anonymous types throw compile time errors, Dynamic types does not throw any compile time error but throw run-time errors.
  • For Anonymous type, Visual studio able to show intellisense because type is known at the time compilation whereas intellisense is not available with dynamic because type is defines at runtime.

Dynamic Type example

  1. dynamic dynamicData = 5;  
  2. Console.WriteLine("Data of dynamic type : " + dynamicData);  
  3. dynamicData = "Jignesh Trivedi";  
  4. Console.WriteLine("Data of dynamic type : " + dynamicData);  

 

Dynamic type

Summary
  • Anonymous types are class type, directly derived from “System.Object” so they are reference type.
  • If two or more Anonymous types have the same properties in same order in a same assembly then compiler treats it as same type.
  • All properties of anonymous type are read only.

Up Next
    Ebook Download
    View all
    Learn
    View all