基本构造在C#中 - 其中被调用第一? [英] Base constructor in C# - Which gets called first?

查看:112
本文介绍了基本构造在C#中 - 其中被调用第一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这被称为第一 - 基构造或这里其他的东西

 公共类MyExceptionClass:异常
{
    公共MyExceptionClass(字符串消息,串extrainfo):基地(消息)
    {
        这里//其他的东西
    }
}
 

解决方案

该基地的构造函数将首先被调用。

试试吧:

 公共类MyBase
{
  公共MyBase()
  {
    Console.WriteLine(MyBase);
  }
}

公共类MyDerived:MyBase
{
  公共MyDerived():基地()
  {
    Console.WriteLine(MyDerived);
  }
}
 

Which gets called first - the base constructor or "other stuff here"?

public class MyExceptionClass : Exception
{
    public MyExceptionClass(string message, string extrainfo) : base(message)
    {
        //other stuff here
    }
}

解决方案

The base constructor will be called first.

try it:

public class MyBase
{
  public MyBase()
  {
    Console.WriteLine("MyBase");
  }
}

public class MyDerived : MyBase
{
  public MyDerived():base()
  {
    Console.WriteLine("MyDerived");
  }
}

这篇关于基本构造在C#中 - 其中被调用第一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆