<p>I've seen lots of descriptions how anonymous types work, but I'm not sure how they're really useful.  What are some scenarios that anonymous types can be used to address in a well-designed program?</p>

[{'answer_id': 48672, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 2, 'selected': False, 'text': "<p>When you create types for 'Use and throw' purposes. \nThis seems to have come due to LINQ. Seems to be a way to create structures with fields on the fly for a LINQ query. Returning a struct/type with specified fields only. If not for this, you'd have to declare a .Net type for each unique combination of fields you wish to retrieve.</p>\n"}, {'answer_id': 48677, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 3, 'selected': False, 'text': "<p>From LINQ in action (page 76 section 2.6.3): </p>\n\n<blockquote>\n  <p>... anonymous types [are] a great tool for quick and simple temporary results.  We don't need to declare classes to hold temporary results thanks to temporary types.</p>\n</blockquote>\n\n<p>basically they're useful to hold information in the local scope temporarily.  Anything more requires the use of reflection and can become quite a problem.  The example they give in the above-quoted book is in writing to console the id, name, and amount of memory taken up by each running process.  They create an anonymous type, add it to a list (all one statement) and then use ObjectDumper to output it.  Therefore the code no longer needs a separately declared class to hold the id, name and memory used but its all declared implicitly bringing the line count down to 4:</p>\n\n<pre><code>var pl = new List&lt;Object&gt;();\nforeach(var p in Process.GetProcesses())\n  pl.Add(new {p.Id, p.ProcessName, Memory=p.WorkingSet64});\nObjectDumper.Write(pl);\n</code></pre>\n"}, {'answer_id': 48705, 'author': 'Omer van Kloeten', 'author_id': 4979, 'author_profile': 'https://Stackoverflow.com/users/4979', 'pm_score': 5, 'selected': True, 'text': "<p>Anonymous types have nothing to do with the design of systems or even at the class level. They're a tool for developers to use when coding.</p>\n\n<p>I don't even treat anonymous types as types per-se. I use them mainly as method-level anonymous tuples. If I query the database and then manipulate the results, I would rather create an anonymous type and use that rather than declare a whole new type that will never be used or known outside of the scope of my method.</p>\n\n<p>For instance:</p>\n\n<pre><code>var query = from item in database.Items\n            // ...\n            select new { Id = item.Id, Name = item.Name };\n\nreturn query.ToDictionary(item =&gt; item.Id, item =&gt; item.Name);\n</code></pre>\n\n<p>Nobody cares about `a, the anonymous type. It's there so you don't have to declare another class.</p>\n"}, {'answer_id': 48706, 'author': 'Registered User', 'author_id': 5087, 'author_profile': 'https://Stackoverflow.com/users/5087', 'pm_score': 1, 'selected': False, 'text': '<p>Use them with Linq.</p>\n'}, {'answer_id': 168109, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': '<p>The most popular use of anonymous types are for specifying projections in a LINQ to SQL query. </p>\n\n<p>This query</p>\n\n<pre><code>from x in db.Table1 select new {x.Column1, Alias2=x.Column2}\n</code></pre>\n\n<p>will be converted to this SQL:</p>\n\n<pre><code>SELECT Column1, Column2 AS Alias2 FROM Table1\n</code></pre>\n\n<p>With anonymous types, you can create ad hoc projections without defining the type for it beforehand. The compiler will define the type for you.</p>\n'}, {'answer_id': 168135, 'author': 'ChaosSpeeder', 'author_id': 205962, 'author_profile': 'https://Stackoverflow.com/users/205962', 'pm_score': 0, 'selected': False, 'text': "<p>It is important to know, that LINQ doesn't force you, to use anonymous types. You can also write normal object constructions after select.</p>\n\n<pre><code>var query = from item in database.Items\n// ...\nselect new Person(item.id, item.Name)\n</code></pre>\n\n<p>This prevents you from ugly reflection programming.</p>\n"}, {'answer_id': 887762, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>@Wouter :</p>\n\n<pre><code>var query = from item in database.Items\nselect new Person\n{\nID =item.id,\nNAME= item.Name\n};\n</code></pre>\n\n<p>where ID and NAME are real property of your Person class.</p>\n'}]
['https://Stackoverflow.com/questions/48668', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']