Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#构造函数头中使用冒号_C#_Constructor - Fatal编程技术网

在C#构造函数头中使用冒号

在C#构造函数头中使用冒号,c#,constructor,C#,Constructor,下面是一个名为Complex的结构的构造函数,它有两个成员变量,实变量和虚变量: public Complex(double real, double imaginary) : this() { Real = real; Imaginary = imaginary; } 函数头中冒号后面的部分有什么用途 它被称为构造函数链接-实际上它在调用另一个构造函数(在本例中不接受参数),然后返回并在此构造函数中执行任何附加工作(在本例中设置实和虚的值).这是一个调用构造函数体前面的另一个实例构造函数

下面是一个名为Complex的
结构的构造函数,它有两个成员变量,实变量和虚变量:

public Complex(double real, double imaginary) : this()
{
 Real = real;
 Imaginary = imaginary;
}

函数头中冒号后面的部分有什么用途

它被称为构造函数链接-实际上它在调用另一个构造函数(在本例中不接受参数),然后返回并在此构造函数中执行任何附加工作(在本例中设置
的值).

这是一个调用构造函数体前面的另一个实例构造函数的实例。构造函数初始值设定项有两种形式:
this
base

base
构造函数初始值设定项导致调用直接基类中的实例构造函数

构造函数初始值设定项导致调用类本身的实例构造函数。当构造函数初始值设定项没有参数时,将调用无参数构造函数

class Complex
{
   public Complex() // this constructor will be invoked
   {    
   }

   public Complex(double real, double imaginary) : this()
   {
      Real = real;
      Imaginary = imaginary;
   }
}
顺便说一句,通常构造函数链接是从参数计数较少的构造函数到参数较多的构造函数(通过提供默认值):


:this()
指示编译器在执行该构造函数中找到的代码之前调用该类的默认构造函数。如果默认构造函数为空,则它没有实际效果。

您始终可以从一个构造函数中调用另一个构造函数。比如说:

public class mySampleClass
{
    public mySampleClass(): this(10)
    {
        // This is the no parameter constructor method.
        // First Constructor
    }

    public mySampleClass(int Age) 
    {
        // This is the constructor with one parameter.
        // Second Constructor
    }
}
public struct Foo
{
    public int X{get;set;}

    public Foo(int x)
    {
        X = x;
    }
}
public class MyException : Exception
{
    public MyException(string message) : base(message){}
}
this
指的是同一个类,所以当我们说
this(10)
时,实际上是指执行
public-mySampleClass(int-Age)
方法。上述调用方法的方法称为initializer。我们在方法中最多可以有一个这样的初始值设定项


在您的情况下,它将在没有任何参数的情况下调用默认构造函数

它的构造函数链调用同一类的默认或无参数构造函数。

在定义使用C#简写符号表示属性的结构时,调用默认构造函数是有用的(并且是必需的)。例如:

public class mySampleClass
{
    public mySampleClass(): this(10)
    {
        // This is the no parameter constructor method.
        // First Constructor
    }

    public mySampleClass(int Age) 
    {
        // This is the constructor with one parameter.
        // Second Constructor
    }
}
public struct Foo
{
    public int X{get;set;}

    public Foo(int x)
    {
        X = x;
    }
}
public class MyException : Exception
{
    public MyException(string message) : base(message){}
}
编译器将在此处引发错误,因为在分配所有结构字段之前,构造函数中无法使用“this”对象来分配X。在幕后,会自动创建一个字段作为属性X的备份存储,并调用结构的默认构造函数来初始化该字段(在本例中为默认值0)


有关更多详细信息,请参见此问题:

它称为构造函数链接。在构造函数中使用它来调用另一个构造函数。 您也可以为类继承执行此操作,例如:

public class mySampleClass
{
    public mySampleClass(): this(10)
    {
        // This is the no parameter constructor method.
        // First Constructor
    }

    public mySampleClass(int Age) 
    {
        // This is the constructor with one parameter.
        // Second Constructor
    }
}
public struct Foo
{
    public int X{get;set;}

    public Foo(int x)
    {
        X = x;
    }
}
public class MyException : Exception
{
    public MyException(string message) : base(message){}
}

如果不发布public no arg
Complex()
constructor,就很难知道它的用途是什么!请参阅:[[1]:可能重复:您也可以使用:base(…)对基类构造函数进行显式调用。在设置值之前,可能会重复对默认构造函数的调用。请确保在提供新答案之前查看现有答案。在这种情况下,有几个答案提到了构造函数链接,而接受的答案(八年前)没有提到它的名字,它清楚地展示了这个概念。如果你仔细阅读,你会看到我的例子为超类构造链接提供了不同的方法。谢谢你的评论。