0

This answer describes constructing 'an anonymous struct' in C#: Is there an equivalent C# syntax for C's inline anonymous struct definition?

var x = new { SomeField = 1, SomeOtherField = "Two" };

Is it valid to return this from a method to avoid having to define a struct explicitly? A comment on that answer says no, but that was 2011...

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 2
    Please explain why you'd want to return it from a method. You can, using `object` or `dynamic`, but the reason for needing this generally is a design smell. – CodeCaster Mar 16 '16 at 13:52
  • You'll want to create a class to return, or return a `Tuple`, or venture into some not recommend areas to solve a simple problem like this – Jonesopolis Mar 16 '16 at 13:53
  • @CodeCaster purely to return named parameters (like in the linked question) without having to declare a struct/class somewhere. – Mr. Boy Mar 16 '16 at 13:55
  • Not advised, but you could return `IDictionary` in this case – DavWEB Mar 16 '16 at 13:56
  • Technically that is an anonymous object... And yes, you can retrieve it... You can even do bad things to recover it. – xanatos Mar 16 '16 at 13:58
  • The response I gave as a duplicate is "one step further" than what you asked... Because after passing it around you want to access the data inside the object... If you want I can reopen your question. – xanatos Mar 16 '16 at 14:00
  • @xanatos I was just about to say "this isn't an exact duplicate, more of something that would be his next question", but I see why you marked this.. – Yuval Itzchakov Mar 16 '16 at 14:01
  • I'm not sure this should be a duplicate but I am happy with the answer I accepted. – Mr. Boy Mar 16 '16 at 14:04
  • @Mr.Boy Probably the question linked from the one I've connected this to would be better: [Is there a way to return Anonymous Type from method?](http://stackoverflow.com/questions/1329672/is-there-a-way-to-return-anonymous-type-from-method) But the chain of duplicated questions flows more easily in one direction than in the other. – xanatos Mar 16 '16 at 14:07

2 Answers2

6

That's not a struct, it's an Anonymous Type in C# (which is actually implemented as a class by the compiler). An anonymous type is declared in local scope, and you cannot pass it as a strong typed class in C# because it is a compiler generated class. Your options are either to use object or dynamic as return types, but you still lose the underlying type.

To sum up your question, the answer is a simple "not the way you want it to work".

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
4

MSDN says:

Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object. The compiler provides a name for each anonymous type, although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type.

So you can return it as an object but afterwards you cannot do too much with them unless you use reflection or dynamics.

If your real intention is to return multiple values use the Tuple<...> type instead:

return Tuple.Create<int, string>(1, "Two");
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65