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

  • SEARCH
  • Home
  • 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 7999795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:36:18+00:00 2026-06-04T15:36:18+00:00

What is the limitation of public class A { public string Stuff {get;set;} }

  • 0

What is the limitation of

public class A
{
     public string Stuff {get;set;}
}

...

repository.GetAll().Select(x=> new A { x.Stuff });

That doesn’t work. You have to add

{ Stuff = x.Stuff }

repository.GetAll().Select(x=> new { x.Stuff });

But this works. It creates an anon class with a very similar definition to class A.

Conceptually I don’t see a big difference in what is going on here. Anyone shed some light?

  • 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-04T15:36:19+00:00Added an answer on June 4, 2026 at 3:36 pm

    The short answer – the C# compiler and language team didn’t implement it this way – they either didn’t think of it (unlikely) or decided that it was not a good idea….

    repository.GetAll().Select(x=> new A { x.Stuff });
    

    That doesn’t work. You have to add

    This is an object initializer. This works by calling the default constructor on an object, and then matching property names to a value, ie: Stuff = x.Foo, and is really just a shortcut for matching properties, so the syntax is really just “short hand” for:

    A tmp = new A();
    tmp.Stuff = x.Stuff;
    

    Now, I suppose the compiler team could have assumed that an initialization statement with no left hand side on the equation should search for a matching property where the name matched and the type was implicitly convertible, but I suspect this would fall into the realm of “flirting with the bad idea list” if or when it would’ve been discussed by the language team. In general, C# is fairly explicit in its syntax, and this would be loosening that up a bit in a way that requires two separate matches (name + type) and would be non-obvious in many scenarios. Since you’re working with a public API here (A), it’d also be very easy for a refactoring in either side (A or whatever type “x” is defined as being) to break this completely.

    Finally, this also isn’t really necessary – if you want an instance of A to be constructed this way, just add a constructor with an overload (which is safer in many ways in any case), and use:

    repository.GetAll().Select(x=> new A(x.Stuff));
    

    This makes the intention and meaning very explicit, and takes out the brittle maintainability.

    repository.GetAll().Select(x=> new { x.Stuff });
    

    This is doing something completely different – here, you’re initializing an anonymous type, and letting the compiler completely determine the type names and types for you. I suspect this was determined to be “safe” since you’re never really working with a public API – anonymous types aren’t really supposed to “leak” out of the method where it’s defined. The risk of having a refactoring change a property name and effectively change values, etc, gets dramatically reduced and isolated to a single method in this case, which in turn keeps this “automatic” naming feature lower risk overall. Also, there isn’t an easy alternative here, as you can’t define constructors on anonymous types, so there wouldn’t be a simple way to have a concise syntax in this case. This adds benefit without introducing a lot of risk.

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

Sidebar

Related Questions

This is my Model: public class MyModel{ public string Name { get; set; }
I have the next code: class Foo { public Foo (String param) { ...
I have the following classes class A{ private String name; private int value; public
What is the real reason for that limitation? Is it just work that had
I would like to have the form object like this: public class FormData {
I have a dictionary like the follow: public Dictionary<int, SpawnList> spawnEntities = new Dictionary<int,
Say I have a class like this: public class FauxIdentityForm { public Guid FormID
Suppose we have a nested generic class: public class A<T> { public class B<U>
I have a C# annotation which is : [AttributeUsage(AttributeTargets.Method)] public class OperationInfo : System.Attribute
I have the following example classes in Java: public class A { } public

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.