What is the design decision to lean towards not returning an anonymous types from a method?
What is the design decision to lean towards not returning an anonymous types from
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can return an instance of an anonymous type from a method – but because you can’t name it, you can’t declare exactly what the method will return, so you’d have to declare that it returns just
object. That means the caller won’t have statically typed access to the properties etc – although they could still pass the instance around, access it via reflection (or dynamic typing in C# 4).Personally I would quite like a future version of C# to allow you to write a very brief class declaration which generates the same code (immutable properties, constructor, Equals/GetHashcode/ToString) with a name…
There is one grotty hack to go round it, called casting by example. I wouldn’t recommend it though.