Hello everyone, there is such an example:

class Program { static void Main() { // Анонимные типы в анонимных типах. var instance = new { Name = "Alex", Age = 27, Id = new { Number = 123 } }; Console.WriteLine("Name = {0}, Age = {1}, Id = {2}", instance.Name, instance.Age, instance.Id.Number); // Delay. Console.ReadKey(); } } 

As you can see, in this example - one anonymous type is embedded in another. I realized that this can be depicted something like this: enter image description here

Also, as I understand it, when I create an anonymous type, Visual Studio creates a new class and a new class is created when the compiler encounters a new anonymous type. Novelty is determined by comparing property names, their order and types. And it turns out that when I create a new anonymous type, Visual Studio generates (approximately) the following class:

 [DebuggerDisplay("{ x = {x}, y = {y} }", Type = "<Anonymous Type>")] public sealed class Anonymous<TX, TY> { private readonly TX field_x; private readonly TY field_y; public TX x { get { return field_x; } } public TY y { get { return field_y; } } [DebuggerHidden] public Anonymous(PX x, PY y) { field_x = x; field_y = y; } public override bool Equals(object value) { /* тут имплементация */ } public override int GetHashCode() { /* тут имплементация */ } public override string ToString() { /* тут имплементация */ } } 

But if I have one anonymous type nested in another, then these generated classes will be Nested classes or not?

  • 6
    Where did you see that one anonymous type is embedded in another? - Vlad from Moscow
  • why should they be nested? - Grundy
  • @VladfromMoscow, judging by the picture, the original code was: var instance = new { Name = "Alex", Age = 27, Id = new { Number=123 } }; - Grundy
  • I apologize for the mistake - I copied the wrong code sample, I DIDN'T EVEN LOOKED ... my joint. made an edit. - BadCatss
  • @BadCats you can always take any decompiler (or at least ildasm that comes with the studio) and see. But I put on "no" - PashaPash

1 answer 1

Practice is the criterion of truth! Let's try and see what happened.

 class Program { static void Main(string[] args) { var instance = new { Name = "Alex", Age = 27, Id = new { Number = 123 } }; var allTypes = Assembly.GetExecutingAssembly().GetTypes(); foreach (var t in allTypes) { var nestedTypes = t.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic); Console.WriteLine($"Name: {t.FullName}, nested types: {nestedTypes.Length}"); } } } 

Result:

 Name: <>f__AnonymousType0`3, nested types: 0 Name: <>f__AnonymousType1`1, nested types: 0 Name: NestedAnonymous.Program, nested types: 0 

Thus, for the compiler, the second anonymous type is no different from the first, there are no nested types in the program. This was to be expected: when we write a var instance = new { List = new List<int>() }; , we do not expect List<int> be a nested type of an anonymous type?


For comparison, put another nested type:

 class Program { class Nested { } static void Main(string[] args) { var instance = new { Name = "Alex", Age = 27, Id = new { Number = 123 } }; var allTypes = Assembly.GetExecutingAssembly().GetTypes(); foreach (var t in allTypes) { var nestedTypes = t.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic); Console.WriteLine($"Name: {t.FullName}, nested types: {nestedTypes.Length}"); } } } 

We get this conclusion:

 Name: <>f__AnonymousType0`3, nested types: 0 Name: <>f__AnonymousType1`1, nested types: 0 Name: NestedAnonymous.Program, nested types: 1 Name: NestedAnonymous.Program+Nested, nested types: 0 

We see that Program has a nested type, and that the nested type name contains an enclosing type name, separated by a plus sign. (This is, of course, the internal details of the implementation of anonymous types by the compiler in the MSVC 2015 package; other compilers may do this in a different way.)