Is there a way to return Anonymous Type from method?
Solution 1:
Returning it as a System.Object
is the only way to return an anonymous type from a method. Unfortunately there is no other way to do this since anonymous types were designed specifically to prevent their use in this way.
There are some tricks that you can do to in conjunction with returning an Object
that allow you to get close. If you are interested in this workaround please read Can't return anonymous type from method? Really?.
Disclaimer: Even though the article I linked does show a workaround that doesn't mean it is a good idea to do it. I would strongly discourage you using this approach when creating a regular type would be safer and easier to understand.
Solution 2:
Alternatively, you can use the Tuple class in .NET 4.0 and higher:
http://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx
Tuple<string, string> Create()
{
return Tuple.Create("test1", "test2");
}
then you can access the properties like this:
var result = Create();
result.Item1;
result.Item2;