c# generic self-referencing declarations

后端 未结 4 1096
面向向阳花
面向向阳花 2020-12-08 07:53

I\'ve been reading Albaharis\' \"C# 5.0 in A Nutshell\" and I\'ve encountered this in Generics section and it is said to be legal:

class Bar where T         


        
4条回答
  •  春和景丽
    2020-12-08 08:27

    I might be a little late to the party but I want to share an unreal-world application scenario for fun :)

    // self referencing list in c#
    // we cant use generic type syntax: new List();
    list.Add(list); // the "FBI! open up" part
    Console.WriteLine(list[0][0][0][0][0][0][0].Count); // 1
    

提交回复
热议问题