C# 这个冒号 (:) 是什么意思?

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

What does this colon (:) mean?

c#constructorconstructor-chaining

提问by

Before the thiskeyword is a colon. Can anyone explain what the colon means in this context? I don't believe this is inhertance.

this关键字之前是冒号。任何人都可以解释冒号在这种情况下的含义吗?我不相信这是继承。

Thanks

谢谢

using System;

namespace LinkedListLibrary
{
    class ListNode
    {
        private object data;
        private ListNode next;

        public ListNode(object dataValue)
            : this(dataValue, null)
        {
        }

        public ListNode(object dataValue, ListNode nextNode)
        {
            data = dataValue;
            next = nextNode;
        }

        public ListNode Next
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
        public object Data
        {
            get
            {
                return data;
            }
        }


    }
}

回答by Quintin Robinson

It is constructor chaining so the constructor with the subsequent : thiscall will chain to the ctor that matches the signature.

它是构造函数链接,因此带有后续: this调用的构造函数将链接到与签名匹配的构造函数。

So in this instance

所以在这个例子中

public ListNode(object dataValue)

is calling

正在打电话

public ListNode(object dataValue, ListNode nextNode)

with null as the second param via : this(dataValue, null)

以 null 作为第二个参数 via : this(dataValue, null)

it's also worth noting that the ctor called via the colon executes before the ctor that was called to initialize the object.

还值得注意的是,通过冒号调用的 ctor 在被调用以初始化对象的 ctor 之前执行。

回答by Jeff Yates

It (along with the thiskeyword) is instructing the constructor to call another constructor within the same type before it, itself executes.

它(连同this关键字)指示构造函数在它自己执行之前调用同一类型内的另一个构造函数。

Therefore:

所以:

public ListNode(object dataValue)
    : this(dataValue, null)
{
}

effectively becomes:

有效地变成:

public ListNode(object dataValue)
{
    data = dataValue;
    next = null;
}

Note that you can use baseinstead of thisto instruct the constructor to call a constructor in the base class.

请注意,您可以使用base代替this来指示构造函数调用基类中的构造函数。

回答by Otávio Décio

It means before running the body, run the constructor with object and ListNode parameters.

这意味着在运行主体之前,使用 object 和 ListNode 参数运行构造函数。

回答by opedog

It calls the other ListNode constructor. You can do a similar thing with the base keyword to call a constructor of a class you're deriving from.

它调用另一个 ListNode 构造函数。你可以用 base 关键字做类似的事情来调用你派生的类的构造函数。

回答by Groo

No, that enables you to execute the existing constructor overload (the one with two parameters), before executing the body of the new constructor.

不,这使您能够在执行新构造函数的主体之前执行现有的构造函数重载(带有两个参数的重载)。

That's the simplest way to reuse the constructor code in multiple constructor overloads.

这是在多个构造函数重载中重用构造函数代码的最简单方法。

回答by Jeff L

The code is telling the other constructor to execute with the supplied arguments before the body of the current constructor is executed.

代码告诉另一个构造函数在执行当前构造函数的主体之前使用提供的参数执行。

回答by justin.m.chase

Constructor chain arguments. There is also ": base()" for chaining a call to a constructor on the base type.

构造函数链参数。还有“:base()”用于将调用链接到基类型上的构造函数。