C# inheritance - same identifier on either side of ':'

邮差的信 提交于 2019-12-26 03:54:40

问题


I know : indicates inheritance in C#;

but what does it mean when the same identifier is on either side?

Eg

class GameObject : GameObject {

回答1:


It does not work, because Visual Studio will throw a 'cyclic dependency' error, because, when you think about it, GameObject inherits from GameObject which inherits from GameObject which inherits from... In other words, this is impossible, and so means nothing (except, like Neil mentioned in the comments, a compiler error).




回答2:


Or this is Unicode

class GameObject
{

}
class GameObjеct : GameObject 
{

}



回答3:


The syntax you provided is legal if base class is declared in a different namespace You need, however, use namespaces to resolve naming conflicts

Example

   namespace A
   {
       class GameObject
       {
       }
   }

   namespace B
   {

      class GameObject: A.GameObject
      {
      }
   }

Another possible scenario involves generics, which allow class name overload. The following will also compile:

class GameObject
{
}

class GameObject<T> : GameObject
{
}

class GameObject<T, T> : GameObject<T>
{
}


来源:https://stackoverflow.com/questions/7116614/c-sharp-inheritance-same-identifier-on-either-side-of

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!