为什么这个c#构造函数没有按预期工作?

时间:2013-10-03 15:05:08

标签: c# constructor

如果使用符合特定条件的参数调用构造函数,我会尝试以不同方式初始化构造函数。我有以下代码。

class MessageEventArgs : EventArgs
{
    private int _pId;
    private string _message;
    private string _channelPath;

    public MessageEventArgs(string message)
    {
        _pId = Process.GetCurrentProcess().Id;
        _message = message;
        _channelPath = null;
    }

    public MessageEventArgs(string[] details)
    {
        if (details.Length == 1)
        {
            new MessageEventArgs(details[0]);
            return;
        }
        _pId = int.Parse(details[0]);
        _message = details[1];
        _channelPath = details[2];
    }
}

令人惊讶的是,在调试和逐行调试时,我看到所有调用都正常,但在实例化之后,pId_message有默认值,即0和null

5 个答案:

答案 0 :(得分:9)

您没有调用其他构造函数,而是创建了使用默认值初始化的第二个实例。

因此,第一个实例仍未初始化。

你需要这样做:

class MessageEventArgs : EventArgs
{
    private int _pId;
    private string _message;
    private string _channelPath;

    public MessageEventArgs(string message)
    {
        _pId = Process.GetCurrentProcess().Id;
        _message = message;
        _channelPath = null;
    }

    public MessageEventArgs(string[] details)
    {
        if (details.Length == 1)
        {
            _pId = Process.GetCurrentProcess().Id;
            _message = details[0];
            _channelPath = null;
            return;
        }
        _pId = int.Parse(details[0]);
        _message = details[1];
        _channelPath = details[2];
    }
}

我可能会检查细节是否也是null ..

答案 1 :(得分:8)

根据规范,在C#中,你不能从类中调用另一个构造函数。所以这不是return语句,但是调用其他构造函数是不正确的尝试。你必须制作一个Initialize方法,体现你正在尝试做的事情并从两种方法中调用它。

class MessageEventArgs : EventArgs
{
    private int _pId;
    private string _message;
    private string _channelPath;

    public MessageEventArgs(string message)
    {
        Initialize( message );
    }

    public MessageEventArgs(string[] details)
    {
        if (details.Length == 1)
        {
            Initialize( details[ 0 ] );
            return;
        }

        _pId = int.Parse(details[0]);
        _message = details[1];
        _channelPath = details[2];
    }

    private void Initialize(string message)
    {
        _pId = Process.GetCurrentProcess().Id;
        _message = message;
        _channelPath = null;     
    }
}

答案 2 :(得分:2)

不确定这是否更干净,但您可以尝试以下方法:

    public MessageEventArgs(string message)
        : this(new [] {message})
    {
    }

    public MessageEventArgs(string[] details)
    {
        if (details.Length == 1)
        {
            _pId = Process.GetCurrentProcess().Id;
            _message = details[0];
            _channelPath = null;
        }
        else
        {
            _pId = int.Parse(details[0]);
            _message = details[1];
            _channelPath = details[2];
        }
    }

答案 3 :(得分:1)

您无法从构造函数返回任何内容。尝试这样的事情。

    if (details.Length == 1)
    {
        _pId = Process.GetCurrentProcess().Id;
         _message = message;
        _channelPath = null;
    }
    else 
    {
        _pId = int.Parse(details[0]);
        _message = details[1];
        _channelPath = details[2];
    }

答案 4 :(得分:1)

您无法从构造函数中调用同一个类的构造函数。您的代码创建了MessageEventArgs的本地实例,并且它超出了范围,这就是您没有看到预期行为的原因。

构造函数用于根据传递的参数创建对象的实例。重载意味着允许传递不同的参数并导致对象的正确实例化。构造函数是原子的,因此如果删除一个构造函数,它就不会影响其他构造函数。

您的代码应如下所示:

class MessageEventArgs : EventArgs
{
    private int _pId;
    private string _message;
    private string _channelPath;

    public MessageEventArgs(string message)
    {
        _pId = Process.GetCurrentProcess().Id;
        _message = message;
        _channelPath = null;
    }

    public MessageEventArgs(string[] details)
    {
        if (details.Length == 1)
        {
            _pId = Process.GetCurrentProcess().Id;
            _message = details[0];
            _channelPath = null;
        }
        else 
        {
            _pId = int.Parse(details[0]);
            _message = details[1];
            _channelPath = details[2];
        }
    }
}
相关问题