C# Anonymous Types

In C#, anonymous types are a way to define a type without explicitly declaring a class or structure. They allow you to create objects with a set of properties on the fly, typically used for temporary or small data structures. Anonymous types are defined using the new keyword and an object initializer syntax.

Here’s an example of creating an anonymous type in C#:

var person = new { Name = "John", Age = 30, Occupation = "Developer" };

In this example, we create an anonymous type with three properties: Name, Age, and Occupation. The type of person is inferred by the compiler. Note that we use the var keyword to let the compiler determine the type of the anonymous object.

You can access the properties of an anonymous type using dot notation:

Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, Occupation: {person.Occupation}");

Anonymous types are read-only, meaning you cannot modify their properties after creation. They are mainly useful for scenarios where you want to encapsulate a small set of related data temporarily without explicitly defining a separate class or structure.

Anonymous types are often used in LINQ queries to create projections or select specific properties from a data source. For example:

var employees = GetEmployees();
var employeeNames = employees.Select(e => new { e.FirstName, e.LastName });

In this example, we use an anonymous type to select only the FirstName and LastName properties from a collection of Employee objects.

Keep in mind that anonymous types have limitations. They don’t support inheritance, explicit interfaces, or defining methods. They are primarily used for simple data representation and not intended for complex scenarios or long-term data structures.

C# Anonymous Types Example:

Certainly! Here’s an example that demonstrates the usage of anonymous types in C#:

var product = new { Name = "iPhone", Price = 999.99, Category = "Electronics" };

Console.WriteLine($"Product: {product.Name}, Price: {product.Price}, Category: {product.Category}");

In this example, we create an anonymous type with three properties: Name, Price, and Category. The values assigned to these properties are “iPhone”, 999.99, and “Electronics” respectively. The var keyword is used to let the compiler infer the type of the anonymous object.

We then use Console.WriteLine to print the values of the properties to the console. The output will be:

Product: iPhone, Price: 999.99, Category: Electronics

Here’s another example that demonstrates the usage of anonymous types with LINQ:

var books = new[]
    new { Title = "The Great Gatsby", Author = "F. Scott Fitzgerald" },
    new { Title = "To Kill a Mockingbird", Author = "Harper Lee" },
    new { Title = "1984", Author = "George Orwell" }

var selectedBooks = books.Where(b => b.Author.Contains("Fitzgerald"));

foreach (var book in selectedBooks)
    Console.WriteLine($"Title: {book.Title}, Author: {book.Author}");

In this example, we create an array of anonymous types representing books. Each book has two properties: Title and Author. We then use LINQ’s Where method to filter the books based on a condition (the author’s name contains “Fitzgerald” in this case).

Finally, we iterate over the filtered books and print their titles and authors to the console. The output will be:

Title: The Great Gatsby, Author: F. Scott Fitzgerald

These examples illustrate how anonymous types can be used to create objects with properties on the fly and work with them in various scenarios.