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 7973495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:07:17+00:00 2026-06-04T08:07:17+00:00

A junior co-worker of mine managed to write very scary, scary code. printJob.Type =

  • 0

A junior co-worker of mine managed to write very scary, scary code.

printJob.Type = item[LocalFunctions.GetName(new { printJob.Type })].ToString();

 public static string GetName<T>(T item) where T : class
    {
        try
        {
            return typeof(T).GetProperties()[0].Name;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

What is you gues what will GetName will output? It will output “Type”!
I just don’t get how this is possible. My first thought is that MS will create an anonymous type with property that has the same name as the property from which the value came from (compiler magic?). As this cannot possibly be a supported feature, I advised my junior co-worker to not use things he cannot understand.

But that leaves the question open: How is this possible?

  • 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. Editorial Team
    Editorial Team
    2026-06-04T08:07:19+00:00Added an answer on June 4, 2026 at 8:07 am

    Anonymous types infer property names unless they are specified:

    If you do not specify member names in the anonymous type, the compiler
    gives the anonymous type members the same name as the property being
    used to initialize them.

    http://msdn.microsoft.com/en-us/library/bb397696.aspx

    The compiler then infers the type for the generic at compile time – so typeof(T) works. It is fully supported, even if the code is fragile. What happens when someone refactors the name of the property?

    I’d also say it’s inadvisable to advise people on topics you don’t have an answer to yourself – this is the source of many a http://www.thedailywtf.com article 😉

    Personally I’d still remove this in favour of more robust code, instead of assuming the property name is always going to be the same.

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

Sidebar

Related Questions

This program below moves the last (junior) and the penultimate bytes variable i type
My apologies once again for asking another very junior question. For one reason or
I'm a junior developer and recently started working for a very small office where
I am a junior at university and quite new to the .NET framework. Currently
There is my code <input type=radio name=category id=elementary value=elementary /> 1 <input type=radio name=category
This is very junior, but how does one express a but as a conditional
I am a junior developer, and trying to write an application to consume SOAP
We have a junior programmer that simply doesn't write enough tests. I have to
I am a junior web designer who has fair amount of knowledge in photoshop,
I have pretty good skills in PHP , Mysql and Javascript for a junior

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.