Skip to content

The first case for name inference in Projection initializers is wrong #49117

@Xulei-NL

Description

@Xulei-NL

Type of issue

Outdated article

Description

Hi there!

I was reading Anonymous Types. I tried to grasp three cases in the section Project initializers where the dotnet compiler would not be able to infer member names. The cases are as follows:

The member name isn't inferred in the following cases:

    The candidate name is a member name of an anonymous type, such as ToString or GetHashCode.
    The candidate name is a duplicate of another property member in the same anonymous type, either explicit or implicit.
    The candidate name isn't a valid identifier (for example, it contains spaces or special characters).

I doubt the first case The candidate name is a member name of an anonymous type, such as ToString or GetHashCode. I tried it in dotnetfiddle. However the code was compilable and runnable. Therefore I create this issue to see whether it is possible to delete the first case if I am right.

using System;
					
public class Program
{
	public static void Main()
	{
		var ToString = "hello world";
		var anAnonymous = new { ToString };
		Console.WriteLine(anAnonymous.ToString);
	}
}

What do you think? I'm looking forward to your input.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/anonymous-types

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/fundamentals/types/anonymous-types.md

Document Version Independent Id

dd937177-98f0-a797-d49b-29332aae0614

Platform Id

ecfed45c-63aa-17f6-2cfc-88b1f8bc7adb

Article author

@BillWagner

Metadata

  • ID: d1b30ebe-b640-449b-3602-9891a55794db
  • PlatformId: ecfed45c-63aa-17f6-2cfc-88b1f8bc7adb
  • Service: dotnet-csharp
  • Sub-service: fundamentals

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⌚ Not TriagedNot triageddotnet-csharp/svcfundamentals/subsvchelp wantedGood for community contributors to help [up-for-grabs]okr-qualityContent-quality KR: Concerns article defects (bugs), freshness, or build warnings.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions