Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 150425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:17:19+00:00 2026-05-11T09:17:19+00:00

Consider this: var me = new { FirstName = John, LastName = Smith };

  • 0

Consider this:

var me = new { FirstName = 'John', LastName = 'Smith' }; 

This is fine as we can then do this:

Console.WriteLine('{0} {1}', me.FirstName, me.LastName); 

However we can’t do this:

public T GetMe() {     return new { FirstName = 'John', LastName = 'Smith' }; } 

because we don’t know the type of T.

We could do this:

public object GetMe() {     return new { FirstName = 'John', LastName = 'Smith' }; } 

but then we’d have to inspect the properties of the object using reflection in order to access them:

var p = new Prog(); object o = p.GetMe(); Type t = o.GetType(); foreach (var prop in t.GetProperties()) {     Console.WriteLine(prop.Name + ': ' + prop.GetValue(o, null)); } 

However what about if we could name an anonymous type as we define it? Of course it would no longer be anonymous, however it would be more succinct and maintainable than a normal class definition.

Consider this:

public Person GetMe() {     return new public class Person { FirstName = 'John', LastName = 'Smith' }; } 

The benefit being it would then be possible to return the result of a complicated Linq query from a method without having to define the class explicitly.

Consider this relatively complex Linq query:

List<int> list = new List<int>(); var query = from number in list             select                 new                     {                         Number = number,                         Square = number*number,                         Absolute = Math.Abs(number),                         Range = Enumerable.Range(0, number)                     }; 

Instead of defining a class like so:

public class MyNumbers {     public int Number { get; set; }     public int Square { get; set; }     public int Absolute { get; set; }     public IEnumerable<int> Range { get; set; } } 

in order to return the query variable from a method we could instead just do this:

List<int> list = new List<int>(); return from number in list             select new public class MyNumbers                     {                         Number = number,                         Square = number*number,                         Absolute = Math.Abs(number),                         Range = Enumerable.Range(0, number)                     }; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:17:19+00:00Added an answer on May 11, 2026 at 9:17 am

    Actually, there’s a ‘hack’ that you can do to get an anonymous type back from a method. Consider this:

    public object MyMethod()     {         var myNewObject = new         {             stringProperty = 'Hello, World!',             intProperty = 1337,             boolProperty = false         };          return myNewObject;     }      public T Cast<T>(object obj, T type)     {         return (T)obj;     } 

    You can now do this:

    var obj = MyMethod(); var myNewObj = Cast(obj, new { stringProperty = '', intProperty = 0, boolProperty = false }); 

    The myNewObj will now be an object of the same Type as the anonymous type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Personally, I would opt for the second method you mention.… May 11, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer I like JExcelApi very much, it is simple and powerful May 11, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer Thread t = new Thread(new YourClassThatImplementsRunnable()); t.start(); // JDK 8… May 11, 2026 at 5:38 pm

Related Questions

Consider the following code: string propertyName; var dateList = new List<DateTime>() { DateTime.Now };
I am implementing a stack in JavaScript. Consider: Stack{0,1,2,3} Top:0 Now the user pops
Is there a best-practice or common way in JavaScript to have class members as
EDIT: Oops - as rightly pointed out, there'd be no way to know whether

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.