How to use anonymous types in C#
By FoxLearn 1/6/2025 4:00:19 AM 60
Introduction to Anonymous Types in C#
An anonymous type in C# is a reference type defined using the var
keyword, with one or more read-only properties but no fields or methods. It can only be used within the scope where it is defined, and its properties are accessible only in that scope.
Use an anonymous type in C#
Let’s say you want to create an anonymous type for an employee’s information:
var employee = new { EmployeeId = 123, FirstName = "Alice", LastName = "Johnson", Department = "Engineering" };
In this example, employee
is an instance of an anonymous type containing four properties: EmployeeId
, FirstName
, LastName
, and Department
. All of them are read-only properties.
Console.WriteLine("Employee ID: {0}", employee.EmployeeId); Console.WriteLine("Name: {0} {1}", employee.FirstName, employee.LastName); Console.WriteLine("Department: {0}", employee.Department);
Nested Anonymous Type
Anonymous types can also be nested, meaning you can include an anonymous type as a property within another anonymous type.
Now, let’s nest an anonymous type for an employee’s address inside the main employee type:
var employee = new { EmployeeId = 123, FirstName = "Alice", LastName = "Johnson", Address = new { Street = "123 Elm St.", City = "Seattle", ZipCode = "98101" } };
To access the properties of the nested anonymous type:
Console.WriteLine("Employee ID: {0}", employee.EmployeeId); Console.WriteLine("Name: {0} {1}", employee.FirstName, employee.LastName); Console.WriteLine("Address: {0}, {1} {2}", employee.Address.Street, employee.Address.City, employee.Address.ZipCode);
Using Anonymous Types with LINQ
The Select
clause in LINQ generates and returns an anonymous type as the result.
Now let’s assume you have a list of employees and you want to use LINQ to select certain properties in an anonymous type.
First, create an Employee
class and populate a list of employees:
public class Employee { public int EmployeeId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Department { get; set; } } IList<Employee> employees = new List<Employee>() { new Employee() { EmployeeId = 1, FirstName = "John", LastName = "Doe", Department = "Sales" }, new Employee() { EmployeeId = 2, FirstName = "Jane", LastName = "Smith", Department = "HR" }, new Employee() { EmployeeId = 3, FirstName = "Mike", LastName = "Brown", Department = "IT" } };
Using LINQ, you can create an anonymous type with EmployeeId
and the full name:
var result = from e in employees select new { e.EmployeeId, FullName = e.FirstName + " " + e.LastName };
To display the results:
foreach (var data in result) { Console.WriteLine("Employee ID: {0}, Name: {1}", data.EmployeeId, data.FullName); }
Anonymous types enable you to quickly create and instantiate a type without needing to declare it in advance. From the CLR’s perspective, an anonymous type is simply a reference type, with the compiler assigning it a name behind the scenes.
Since anonymous types derive from the Object
class, they can only be cast to an instance of Object
. Additionally, anonymous types cannot be used as the return type of methods, properties, events, delegates, or other similar constructs.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#