: C# 构造函数中的 this(foo) 语法?

发布于 2024-07-10 06:27:37 字数 227 浏览 5 评论 0原文

我时不时地会遇到以前见过但从未使用过的语法。 这是其中之一。

有人可以解释 C# 构造函数方法后“:this”或“:base”的用途吗?

例如:

public MyClass(SomeArg arg) : this(new SomethingElse(), arg)
{
}

我的直觉是它用于将默认参数映射到另一个构造函数方法。

Every now and then, I bump into syntax that I've seen before, but never used. This is one of those times.

Can someone explain the purpose of ":this" or ":base" following a C# constructor method?

For example:

public MyClass(SomeArg arg) : this(new SomethingElse(), arg)
{
}

My gut feeling is that it is used to map a default argument onto another constructor method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

瘫痪情歌 2024-07-17 06:27:37

你基本上是对的。 this() 在当前实例上调用构造函数,base() 在当前实例上调用超类型的构造函数。 它们通常用于处理构造函数重载,因此您可以添加其他选项,而无需将其分解为单独的方法。

You're basically right. this() calls a constructor on the current instance, base() calls the supertype's constructor on current instance. They're generally used to handle constructor overloads so you can add additional options without breaking things out into a separate method.

心如荒岛 2024-07-17 06:27:37

你的直觉是对的。 该语法用于调用同一类中的重载构造函数:

public class Test
{
    public Test() : this("Called from default constructor") { }
    public Test(String msg)
    {
        Console.WriteLine(msg);
    }
}

以下代码:

public static void Main(String[] args)
{
    Test t1 = new Test();
    Test t2 = new Test("Called from Main function");
}

输出以下内容

Called from default constructor
Called from main function

类似地, : base(someParams) 用于调用基构造函数。

Your gut feeling is right. The syntax is used to call overloaded constructors in the same class:

public class Test
{
    public Test() : this("Called from default constructor") { }
    public Test(String msg)
    {
        Console.WriteLine(msg);
    }
}

The following code:

public static void Main(String[] args)
{
    Test t1 = new Test();
    Test t2 = new Test("Called from Main function");
}

Outputs the following

Called from default constructor
Called from main function

Similarly, : base(someParams) is used to call base constructors.

起风了 2024-07-17 06:27:37

你说得对。

:base(...) 调用基类的构造函数。

:this(...) 调用定义类的另一个构造函数。 大多数时候它只是作为一个门面。

You're right.

: base(...) calls a constructor of the base class.

: this(...) calls another constructor of the defining class. Most of the time it works merely as a facade.

柠北森屋 2024-07-17 06:27:37

.NET 类中的每个构造函数都确保它所继承的类中的构造函数也被调用。

因此,如果您有以下类:

public class Base { }
public class Something : Base { }
public class Else : Something { }

那么 Else 中的构造函数将调用 Something 中的构造函数,Something 还将调用 Base 中的构造函数。

在基类(即您的后代)中调用的构造函数始终是无参数构造函数。

如果您没有,或者想要覆盖它,您可以通过指定基础(此处的一些参数)来覆盖它。 这将在基类中选择正确的构造函数。

您还可以要求构造函数首先调用同一类中同一级别的另一个构造函数。 这可用于避免在多个构造函数中重复构造函数代码。 但最终,被调用的构造函数将调用基类中的构造函数。

我希望这是可以理解的。

Every constructor in a .NET class ensures that a constructor in the class it inherits from is also called.

So if you have the following classes:

public class Base { }
public class Something : Base { }
public class Else : Something { }

then a constructor in Else, will call a constructor in Something, which will also call a constructor in Base.

The constructor called in a base class (ie. the one you're descending from) is always the parameterless constructor.

If you don't have one, or want to override that, you can override it, by specifying base(some parameters here). This will pick the right constructor in the base class.

You can also ask a constructor to first call another constructor in the same class, at the same level. This can be used to avoid duplicating constructor code in multiple constructors. Ultimately though, the constructors being called will call a constructor in the base class.

I hope this was understandable.

骑趴 2024-07-17 06:27:37

确切地。 称之为“构造函数链接”,它用于解决 C# 无法执行默认参数的事实。

这在 IoC 中被大量使用。

Exactly. The call it Constructor Chaining and it's used to get around the fact that C# doesn't have the ability to do default arguments.

This is used a lot in IoC.

凉宸 2024-07-17 06:27:37

如果我没记错的话,情况是这样的:

public MyClass(SomeArg arg) : this(new SomethingElse(), arg)

会打电话

public MyClass(SomethingElse arg, SomeArg arg1) : base or this or nothing

,一直持续到你有基地或什么也没有。

如果你有base(....),那么该构造函数将使用给定的参数(如果有)调用基本构造函数,而该参数又可以委托给它自己的构造函数(同一个游戏)。

如果没有任何内容,则会自动调用基类的无参数构造函数。

使用 this(....) 后,将使用与参数匹配的构造函数并执行其主体 - 除了已使用 this(. ...)

It's like this if i'm not mistaken:

public MyClass(SomeArg arg) : this(new SomethingElse(), arg)

will call

public MyClass(SomethingElse arg, SomeArg arg1) : base or this or nothing

and that will go on until you got a base or nothing.

If you have base(....) then that constructor having that will call the base constructor with the parameters (if any) given, which in turn can delegate to its own constructors (same game).

If you have nothing, then the parameter-less constructor of the base-class is called automatically.

After you have used this(....), then the constructor matching the parameters will be used and its body will be executed - additionally to the body of the constructor having used this(....).

幸福丶如此 2024-07-17 06:27:37

是的你是对的。 此语法用于让子类构造函数显式调用适当的自定义基类构造函数,而不是默认的构造函数。 您问题中的 this 关键字已解释:

this 关键字的另一种用途是强制一个构造函数调用另一个构造函数,以避免冗余的成员初始化逻辑。

Pro C# 2005 和 .NET 2.0 平台第三版
作者:安德鲁·特罗尔森

Yes you are right. This syntax is used to have your subclass constructors to explicitly call an appropriate custom base class constructor, rather than the default. The this keyword in your question is explained:

Another use of the this keyword is to force one constructor to call another in order to avoid redundant member initialization logic.

in Pro C# 2005 and the .NET 2.0 Platform, 3rd Edition
by Andrew Troelsen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文