Open In App

What is Anonymous Types in C#?

Anonymous types in C# are the types which do not have a name or you can say the creation of new types without defining them. It is introduced in C# 3.0. It is a temporary data type which is inferred based on the data that you insert in an object initializer. Or in other words, an anonymous type provides an efficient way to combine a set of read-only into a single object without any explicit type. The type of the anonymous type is automatically generated by the compiler according to the value assigned to its properties. These are best for the “use and throw” types.

In the above image, we are creating anonymous types by using “new” keyword with the object initializer



Important Points:

In C#, you are allowed to create an anonymous type object with a new keyword without its class definition and var is used to hold the reference of the anonymous types. As shown in the below example, anony_object is an anonymous type object which contains three properties that are s_id, s_name, language.

Example:




// C# program to illustrate the
// concept of anonymous types
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing anonymous object
        var anony_object = new {s_id = 109,
                                s_name = "Sohan"
                               language = "C#" };
  
        // Accessing the object properties
        Console.WriteLine("Student id: " + anony_object.s_id);
        Console.WriteLine("Student name: " + anony_object.s_name);
        Console.WriteLine("Language: " + anony_object.language);
    }
}

Output:
Student id: 109
Student name: Sohan
Language: C#

Nested Anonymous Type

In C#, an anonymous type can have another anonymous type as a property. The nested anonymous type has IntelliSense support in Visual Studio. As shown in the below example, anony_object is an anonymous type object which contains another anonymous type object that is anony_ob:

Example:




// C# program to illustrate the concept
// of nested anonymous types
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing nested anonymous object
        var anony_object = new {s_id = 149, s_name = "Soniya", language = "C#",
                                anony_ob = new { email = "soniya45@gmail.com"}};
  
        // Accessing the object properties
        Console.WriteLine("Student id: " + anony_object.s_id);
        Console.WriteLine("Student name: " + anony_object.s_name);
        Console.WriteLine("Language: " + anony_object.language);
        Console.WriteLine("Email: " + anony_object.anony_ob.email);
    }
}

Output:
Student id: 149
Student name: Soniya
Language: C#
Email: soniya45@gmail.com

Anonymous type in LINQ

You are allowed to use an anonymous type in LINQ. In LINQ, select clause generates anonymous type so that in a query you can include properties that are not defined in the class. As shown in the below example, the Geeks class contains four properties that are A_no, Aname, language, and age. But in result we only need A_no, Aname, and language, so we use a select query which creates a result of an anonymous type which only contains A_no, Aname, and language.

Example:




// C# program to illustrate the 
// concept of anonymous type in LINQ
using System;
using System.Collections.Generic;
using System.Linq;
  
class Geeks {
  
    public int A_no;
    public string Aname;
    public string language;
    public int age;
}
  
class GFG {
  
    // Main method
    static void Main()
    {
        List<Geeks> g = new List<Geeks> 
        {
  
            new Geeks{ A_no = 123, Aname = "Shilpa",
                        language = "C#", age = 23 },
            new Geeks{ A_no = 124, Aname = "Shilpi",
                        language = "C#", age = 20 },
            new Geeks{ A_no = 125, Aname = "Soniya"
                        language = "C#", age = 22 },
            new Geeks{ A_no = 126, Aname = "Sonaly"
                        language = "C#", age = 25 },
  
        };
  
        // select query showing result
        // using anonymous type
        var anony_ob = from geek in g select new {geek.A_no, geek.Aname, geek.language};
        foreach(var i in anony_ob)
        {
            Console.WriteLine("Author id = " + i.A_no + "\nAuthor name = " 
                                 + i.Aname + "\nLanguage = " + i.language);
            Console.WriteLine();
        }
    }
}

Output:
Author id = 123
Author name = Shilpa
Language = C#

Author id = 124
Author name = Shilpi
Language = C#

Author id = 125
Author name = Soniya
Language = C#

Author id = 126
Author name = Sonaly
Language = C#

Article Tags :
C#