什么是类中构造函数c#的Order Precedence? [英] what is Order Precedence of constructors c# in classes?

查看:95
本文介绍了什么是类中构造函数c#的Order Precedence?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类中构造函数c#的顺序优先级是什么?

what is Order Precedence of constructors c# in classes?

推荐答案

构造函数中没有顺序优先级的真实概念。被调用的构造函数是与调用代码的签名匹配的构造函数 - 现在,您可以获得构造函数将责任委托给另一个构造函数的点(通过调用 base(...)这个(....),但被调用的那个总是第一个被击中的 - 即使它可能不是代码首先执行。说,当你这样做时实际发生的事情很有趣。考虑这个类结构:
There is no real concept of order precedence in constructors. The constructor that gets called is the one that matches the signature of the calling code - now, you may get a point where a constructor delegates responsibility to another constructor (either through calling base(...) or this(....), but the one that is called is always the first that is hit - even though it might not be the code that first execute. Saying that, what actually happens when you do this is interesting. Consider this class structure:
public class Program
{
    static void Main(string[] args)
    {
        Bar bar = new Bar();
        Bar bar2 = new Bar("FooBar");

        Console.ReadKey();
    }
}

public class Foo
{
    public Foo()
    {
        Console.WriteLine("Foo");
    }
}

public class Bar : Foo
{
    public Bar()
        : base()
    {
        Console.WriteLine("Bar");
    }

    public Bar(string text)
        : this()
    {
        Console.WriteLine(text);
    }
}

栏栏=新栏(); 可能会让你认为应用程序会写出来 Bar 然后 Foo 因为 Bar 中的构造函数被调用首先,但这不是实际发生的事情。即使 Bar 构造函数被实例化,它首先要做的是通过 base()操作。因此, Foo 中的构造函数在此情况下在 Bar 中的构造函数之前执行。在 Bar bar2 = new Bar(FooBar); 我们看到类似的问题,因为 Bar的构造函数(字符串文本)使用 this()将控制传递给 Bar(),然后才能继续处理(在本例中为构造函数) for Bar在执行之前将控制权传递给 Foo 。因此对于第一个构造函数,输出如下:

The line Bar bar = new Bar(); might make you think that the application will write out Bar and then Foo because the constructor in Bar is called first, but that's not what actually happens. Even though the Bar constructor is instantiated, the first thing it does is pass control to the base constructor via the base() operation. So, the constructor in Foo executes before the constructor in Bar does in this case. In Bar bar2 = new Bar("FooBar"); we see a similar issue because the constructor for Bar(string text) passes control to Bar() using this() before it can continue processing (in this case, the constructor for Bar passes control down to Foo before it executes. So for the first constructor, the following is output:

Foo
Bar

第二个构造函数调用生成:

The second constructor call generates this:

Foo
Bar
FooBar

我希望这会有所帮助。


The order is:

Member variables are initialized to default values for all classes in the hierarchy
Then starting with the most derived class:

Variable initializers are executed for the most-derived type
Constructor chaining works out which base class constructor is going to be called
The base class is initialized 
The constructor bodies in the chain class are executed (note that there can be more than one if they're chained 





问候,

Praveen Nelge



Regards,
Praveen Nelge


这篇关于什么是类中构造函数c#的Order Precedence?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆