C# 从同一类中的其他构造函数调用构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/829870/
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-05 03:20:58  来源:igfitidea点击:

Calling constructor from other constructor in same class

c#constructor

提问by Robbert Dam

I have a class with 2 constructors:

我有一个带有 2 个构造函数的类:

public class Lens
{
    public Lens(string parameter1)
    {
        //blabla
    }

    public Lens(string parameter1, string parameter2)
    {
       // want to call constructor with 1 param here..
    }
}

I want to call the first constructor from the 2nd one. Is this possible in C#?

我想从第二个构造函数中调用第一个构造函数。这在 C# 中可能吗?

采纳答案by Gishu

Append :this(required params)at the end of the constructor to do 'constructor chaining'

附加:this(required params)在构造函数的末尾以进行“构造函数链接”

public Test( bool a, int b, string c )
    : this( a, b )
{
    this.m_C = c;
}
public Test( bool a, int b, float d )
    : this( a, b )
{
    this.m_D = d;
}
private Test( bool a, int b )
{
    this.m_A = a;
    this.m_B = b;
}

Source Courtesy of csharp411.com

来源由csharp411.com 提供

回答by Matthew Dresser

Yes, you'd use the following

是的,你会使用以下

public class Lens
{
    public Lens(string parameter1)
    {
       //blabla
    }

    public Lens(string parameter1, string parameter2) : this(parameter1)
    {

    }
}

回答by Thomas

The order of constructor evaluation must also be taken into consideration when chaining constructors:

链接构造函数时,还必须考虑构造函数求值的顺序:

To borrow from Gishu's answer, a bit (to keep code somewhat similar):

借用 Gishu 的回答,有点(保持代码有点相似):

public Test(bool a, int b, string c)
    : this(a, b)
{
    this.C = c;
}

private Test(bool a, int b)
{
    this.A = a;
    this.B = b;
}

If we change the evalution performed in the privateconstructor, slightly, we will see why constructor ordering is important:

如果我们private稍微改变在构造函数中执行的评估,我们就会明白为什么构造函数的顺序很重要:

private Test(bool a, int b)
{
    // ... remember that this is called by the public constructor
    // with `this(...`

    if (hasValue(this.C)) 
    {  
         // ...
    }

    this.A = a;
    this.B = b;
}

Above, I have added a bogus function call that determines whether property Chas a value. At first glance, it might seem that Cwould have a value -- it is set in the calling constructor; however, it is important to remember that constructors are functions.

上面,我添加了一个伪造的函数调用来确定属性是否C具有值。乍一看,它似乎C有一个值——它是在调用构造函数中设置的;然而,重要的是要记住构造函数是函数。

this(a, b)is called - and must "return" - before the publicconstructor's body is performed. Stated differently, the last constructor called is the first constructor evaluated. In this case, privateis evaluated before public(just to use the visibility as the identifier).

this(a, b)被调用 - 并且必须“返回” - 在public构造函数的主体执行之前。换句话说,最后调用的构造函数是评估的第一个构造函数。在这种情况下,private是在之前评估的public(只是使用可见性作为标识符)。