Types and Base Types

Non-interface types that do not specify a base type get System.Object as their base type.  Declare a type as sealed prohibits the use of the type as a base type. Declareing a type as abstract, on the other hand, disallows direct instantiation of the type and makes it useful only as a base type.

The policy for dealing with collisions between reused method names and reused field names might be different. The CLR supports two basic policies to use when the base and derived types have a method of the same name: hide-by-name and hide-by-signature. When one declares a method using hide-by-signature, only the base method with the same name and the same signature will be hidden. In contrast, when one declares a method using hide-by-name, the derived method hides all methods in the base type that have the same name, no matter what their signature may be. For method name, in C#, always use hide-by-signature. Field name always use hide-by-name.

When the CLR allocates a new object, it calls the constructor method from the most-derived type. If a base type constructor causes a virtual method call to be invoked, the most-derived type’s method will be dispatched even though the derived type’s constructor has probably not completed execution. To avoid this problem, you are strongly encouraged to avoid virtual method calls in a constructor of a non-sealed type.

In the face of instance field declarations with initialize expressions, the compiler-generated .ctor will first call all field initializers in order of declaration. Once the derived type’s field initializers have been called, the derived constructor calls the base type constructor, using the programmer-provided parameters if the base constructor was used. Once the base type’s constructor has completed execution, the derived constructor resumes execution at the body of the constructor. This means that when the base type’s constructor executes, the derived type’s constructor body has not even begun to execute.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值