我可以从 C# 中同一个类的另一个构造函数调用重载的构造函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/985280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:55:27  来源:igfitidea点击:

Can I call an overloaded constructor from another constructor of the same class in C#?

c#constructor

提问by

Can I call an overloaded constructor from another constructor of the same class in C#?

我可以从 C# 中同一个类的另一个构造函数调用重载的构造函数吗?

回答by Ari Roth

EDIT: According to the comments on the original post this is a C# question.

编辑:根据对原始帖子的评论,这是一个 C# 问题。

Short answer: yes, using the thiskeyword.

简短回答:是的,使用this关键字。

Long answer: yes, using the thiskeyword, and here's an example.

长答案:是的,使用this关键字,这是一个例子。

class MyClass
{
   private object someData;

   public MyClass(object data)
   {
      this.someData = data;
   }

   public MyClass() : this(new object())
   {
      // Calls the previous constructor with a new object, 
      // setting someData to that object
   }
}

回答by mfawzymkh

No, You can't do that, the only place you can call the constructor from another constructor in C# is immediately after ":" after the constructor. for example

不,你不能那样做,你可以从 C# 中的另一个构造函数调用构造函数的唯一地方是紧跟在构造函数之后的“:”之后。例如

class foo
{
    public foo(){}
    public foo(string s ) { }
    public foo (string s1, string s2) : this(s1) {....}

}

回答by JaapM

In C# it is not possible to call another constructor from inside the method body. You can call a base constructor this way: foo(args):base() as pointed out yourself. You can also call another constructor in the same class: foo(args):this().

在 C# 中,无法从方法主体内部调用另一个构造函数。您可以通过这种方式调用基础构造函数: foo(args):base() 正如您自己指出的那样。你也可以在同一个类中调用另一个构造函数:foo(args):this()。

When you want to do something before calling a base constructor, it seems the construction of the base is class is dependant of some external things. If so, you should through arguments of the base constructor, not by setting properties of the base class or something like that

当您想在调用基构造函数之前做某事时,基类的构造似乎依赖于某些外部事物。如果是这样,您应该通过基构造函数的参数,而不是通过设置基类的属性或类似的东西

回答by Gishu

If you mean if you can do ctor chaining in C#, the answer is yes. The questionhas already been asked.

如果您的意思是可以在 C# 中进行 ctor 链接,那么答案是肯定的。这个问题已经问过了。

However it seems from the comments, it seems what you really intend to ask is 'Can I call an overloaded constructor from withinanother constructor with pre/post processing?'
Although C# doesn't have the syntax to do this, you could do this with a common initialization function (like you would do in C++ which doesn't support ctor chaining)

然而,从评论看来,似乎你真的打算要问的是“我可以从调用一个重载的构造函数与前/后处理的另一个构造函数?
尽管 C# 没有执行此操作的语法,但您可以使用通用初始化函数执行此操作(就像您在不支持 ctor 链接的 C++ 中所做的那样)

class A
{
  //ctor chaining
  public A() : this(0)
  {  
      Console.WriteLine("default ctor"); 
  }

  public A(int i)
  {  
      Init(i); 
  }

  // what you want
  public A(string s)
  {  
      Console.WriteLine("string ctor overload" );
      Console.WriteLine("pre-processing" );
      Init(Int32.Parse(s));
      Console.WriteLine("post-processing" );
  }

   private void Init(int i)
   {
      Console.WriteLine("int ctor {0}", i);
   }
}