C# Anonymous Types: Creating Objects Without Explicit Class Definitions
Learn how to use anonymous types in C# for creating objects without defining a separate class. This tutorial explains their syntax, demonstrates their use for grouping data, and highlights scenarios where anonymous types offer a concise and efficient alternative to explicitly defining classes.
Understanding and Using Anonymous Types in C#
Introduction
C# anonymous types provide a convenient way to create objects without explicitly defining a class. These types are implicitly defined by the compiler, and their properties are read-only. They're especially useful for temporary objects or when you need a simple way to group data.
Creating Anonymous Types
Anonymous types are created using the new
operator with an object initializer. The compiler automatically generates a unique type name.
Example 1: Basic Anonymous Type
This example shows how to create a simple anonymous type representing a student and access its properties.
Example 1: Creating and Accessing an Anonymous Type
using System;
namespace CSharpFeatures {
class AnonymousTypesExample {
public static void Main() {
// Creating Anonymous Object
var student = new { ID = 101, Name = "Peter", Email = "peter@example.com" };
// Accessing object properties
Console.WriteLine(student.ID);
Console.WriteLine(student.Name);
Console.WriteLine(student.Email);
}
}
}
Output Example 1
101
Peter
peter@example.com
Explanation Example 1
The var
keyword lets the compiler infer the type. The properties ID
, Name
, and Email
are created directly within the object initializer.
Example 2: Anonymous Types in LINQ Queries
Anonymous types are frequently used with LINQ to select specific properties from a data source without creating a new class.
Example 2: Anonymous Types in a LINQ Query
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharpFeatures {
class Student {
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
class AnonymousTypesExample {
public static void Main() {
List<Student> students = new List<Student> {
new Student { ID = 101, Name = "Rahul", Email = "rahul@example.com" },
new Student { ID = 102, Name = "Peter", Email = "peter@abc.com" },
new Student { ID = 103, Name = "Irfan", Email = "irfan@example.com" }
};
var stquery =
from student in students
select new { student.ID, student.Name, student.Email }; // Creating Anonymous Types
foreach (var st in stquery) {
Console.WriteLine("ID = {0}, Name = {1}, Email = {2}", st.ID, st.Name, st.Email);
}
}
}
}
Output Example 2
ID = 101, Name = Rahul, Email = rahul@example.com
ID = 102, Name = Peter, Email = peter@abc.com
ID = 103, Name = Irfan, Email = irfan@example.com
Explanation Example 2
This LINQ query selects specific properties from the Student
objects and creates a new anonymous type for each selected record.
Conclusion
Anonymous types offer a concise way to create simple, temporary objects in C#. Their use in LINQ queries simplifies selecting specific data without needing to define separate classes.