Quick facts about Anonymous types
- Anonymous types are reference types derived form system.objects.
- Properties of the Anonymous type is read only.
- If two Anonymous types have the same properties and the same order then the compiler treats its as the same type. But if both are in one assembly.
- Anonymous types have method scope. If you want to return an Anonymous type from a method you must convert it to an object type. But that is not a good practice.
You can read more on the following blog about this: Anonymous types.
As in the preceding facts you cannot return an anonymous type from a method, if you do want to return one then you need to cast it to an object.
Now in the following article I will do the same, return an anonymous type as an object and show three ways to handle it.
- Solution 1: Handle using Dynamic type.
- Solution 2: Handle by creating the same anonymous type.
- Solution 3: Handle using Reflection.
To understand each solution I created the following method that returns an anonymous type:
- object AnonymousReturn()
- {
- return new { Name = "Pranay", EmailID = "[email protected]" };
- }
- dynamic newtype= AnonymousReturn();
- Console.WriteLine(newtype.Name + " " + newtype.EmailID);
Note: There is no intellisense support since we are using a dynamic type. And we need to remember the property name and type also.
Solution 2: Handle by creating the same anonymous type
- object o = AnonymousReturn();
- var obj = Cast(o, new { Name = "", EmailID = "" });
- Console.WriteLine(obj.Name + " " + obj.EmailID);
- T Cast<T>(object obj, T type) { return (T)obj; }
Solution 3: Handle using Reflection
- object refobj = AnonymousReturn();
- Type type = refobj.GetType();
- PropertyInfo[] fields = type.GetProperties();
- foreach (var field in fields)
- {
- string name = field.Name;
- var temp = field.GetValue(obj, null);
- Console.WriteLine(name + " " + temp);
- }
Check out the full source code to test all techniques we discussed:
- using System;p
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- namespace Test
- {
- class Program
- {
- static void Main(string[] args)
- {
- Program p = new Program();
- dynamic newtype= p.AnonymousReturn();
- Console.WriteLine("With Dynamic Type");
- Console.WriteLine(newtype.Name + " " + newtype.EmailID);
- Console.WriteLine();
- Console.WriteLine("With Creation of same anonymous type");
- object o = p.AnonymousReturn();
- var obj = p.Cast(o, new { Name = "", EmailID = "" });
- Console.WriteLine(obj.Name + " " + obj.EmailID);
- Console.WriteLine();
- Console.WriteLine("With Reflection");
- object refobj = p.AnonymousReturn();
- Type type = refobj.GetType();
- PropertyInfo[] fields = type.GetProperties();
- foreach (var field in fields)
- {
- string name = field.Name;
- var temp = field.GetValue(obj, null);
- Console.WriteLine(name + " " + temp);
- }
- Console.ReadLine();
- }
- object AnonymousReturn()
- {
- return new { Name = "Pranay", EmailID = "[email protected]" };
- }
- T Cast<T>(object obj, T type) { return (T)obj; }
- public static void Write()
- {
- Program p = new Program();
- object obj = p.AnonymousReturn();
- }
- }
- }

Conclusion
So now using the preceding techniques it is possible to return anonymous types from a method.
I hope you guys like it. Please rate it and leave your comments.

Ken LangPosted Feb 2, 2019, 6:08 PM
Great article! Great help in know what ways arrays of anonymous types can be worked with and the limitations of using an array instead of a list (or other type that has an add method).
ANSAR TPosted Mar 7, 2018, 11:08 PM
Thanks alot mahn... You saved my day....
Harpreet SinghPosted Feb 7, 2015, 4:59 AM
nice article
VulpesPosted Feb 6, 2015, 6:11 PM
A useful little article! I knew about the first and third solutions but wouldn't have thought of the second. The use of the Cast<T> method to circumvent the problem of not being able to express an anonymous type in code is a neat solution :)
Sam HobbsPosted Feb 6, 2015, 5:05 PM
What is the advantage of anonymous types instead of defining a structure? Defining a structure is a bit more work but it seems to me that defining a structure can be less work elsewhere. I think a structure would make the program easier to understand and save time determining what the data is (the members are).