Knowing how to manipulate complex datatypes such as Lists is crucial when storing and manipulating data in C#. Sometimes, we may want to incorporate versatility by not having to define explicit classes and create generic lists of anonymous types in C#. In this article, we are going to learn some of the different ways we can leverage to create a generic list of anonymous types in C#.
Without further delay, let’s start!
Anonymous Types Implementation in C#
We know that anonymous types are class-level reference types that do not have a name. These types come in handy when we want to instantiate objects without defining their types in C#. These types consist of read-only properties, and the compiler determines their types based on the values we assign them.
So, to start, let’s define a method that returns an array of anonymous types, which we are going to use for our examples:
public static object[] GenerateRandomAnonymousObject() { var anonymousObj = new[] { new { Name = "Patrick", Age = 32 }, new { Name = "Mary", Age = 25 }, new { Name = "Marley", Age = 40 } }; return anonymousObj; }
Our example returns an object[]
array as we derive anonymous types from the object
class. Our anonymous object contains an array of people’s names and ages, and we will create lists from this object.
Create a Generic List of Anonymous Types using the ToList() Method
First, we can use the ToList()
method to create a generic list of anonymous types in C#:
public static List<object> GeneretaListOfAnonymousTypesUsingToList(object[] listItems) { var listOfAnonymous = listItems.ToList(); return listOfAnonymous; }
The example makes use of the ToList()
method to return a list of our array of anonymous types.
Also, you can see we accept the object[] listItems
as a parameter, which we populate with the GenerateRandomAnonymousObject
method – the one we created in the previous section. In the rest of the examples, we will just show the code inside the method using that listItems
parameter.
Dynamic Type to Create a Generic List of Anonymous Types
Besides using the ToLIst()
method, we can use the dynamic
type to achieve our goal. The dynamic type avoids compile-time type checking and resolves the types on runtime, which can work quite well with anonymous types.
Let’s implement an example to understand how it works:
var anonymousObj = new List<dynamic>(listItems); return anonymousObj;
Our implementation copies the elements in the listItems
object into a new List<dynamic>
instance.
Create a Generic List of Anonymous Types using Object
We can use the object
type to create a generic list of anonymous types in C#. This built-in reference type allows us to assign values of any type to an object
type. Therefore, we can take advantage of this feature and use it to accomplish our needs:
var anonymousObj = new List<object>(listItems); return anonymousObj;
Just like in our previous example, we copy the elements in the listItems
object into a new List<object>
instance.
Other Ways to Create Lists of Anonymous Types
Besides the previous techniques, there are some other that we can talk about in this article, like the use of LINQ:
public static List<object> GeneretaListOfAnonymousTypesUsingLINQ() { var anonymousObj = new[] { new { Name = "Patrick", Age = 32 }, new { Name = "Mary", Age = 25 }, new { Name = "Marley", Age = 40 } }; var result = anonymousObj.Select(x => new { x.Name, x.Age }); return new List<object>(result); }
Alternatively, we can use generic methods to implement a method that accomplishes our needs:
public static List<T> CreateGenericList<T>(T[] listItems) { return new List<T>(listItems); }
The method can accept an array of generic types and return it as a list.
Conclusion
In this article, we learned different ways to create a generic list of anonymous types in C#. Mastering these techniques can help us manipulate complex data types and maximize code reusability. Which technique have you used before? Let us know in the comments section below.