我有两个构造函数,它们将值提供给只读字段。

public class Sample
{
    public Sample(string theIntAsString)
    {
        int i = int.Parse(theIntAsString);
        _intField = i;
    }

    public Sample(int theInt) => _intField = theInt;
    public int IntProperty    => _intField;

    private readonly int _intField;
}

一个构造函数直接接收值,另一个构造函数进行一些计算并获得值,然后设置字段。

现在问题来了:

我不想复制 设置代码。在这种情况下,只有一个 场地已经设置好了,当然还有可能 要大于1。 要使字段为只读,我需要 从构造函数中设置它们,那么 我不能将共享代码“提取”到 效用函数。 我不知道怎么叫 另一个构造函数。

什么好主意吗?


当前回答

以防你需要在调用另一个构造函数之前而不是之后运行一些东西。

public class Sample
{
    static int preprocess(string theIntAsString)
    {
        return preprocess(int.Parse(theIntAsString));
    }

    static int preprocess(int theIntNeedRounding)
    {
        return theIntNeedRounding/100;
    }

    public Sample(string theIntAsString)
    {
        _intField = preprocess(theIntAsString)
    }

    public Sample(int theIntNeedRounding)
    {
        _intField = preprocess(theIntNeedRounding)
    }

    public int IntProperty  => _intField;

    private readonly int _intField;
}

如果需要设置多个字段,ValueTuple会非常有用。

其他回答

是的,你可以在call base或this之前调用其他方法!

public class MyException : Exception
{
    public MyException(int number) : base(ConvertToString(number)) 
    {
    }

    private static string ConvertToString(int number) 
    { 
      return number.toString()
    }

}

以防你需要在调用另一个构造函数之前而不是之后运行一些东西。

public class Sample
{
    static int preprocess(string theIntAsString)
    {
        return preprocess(int.Parse(theIntAsString));
    }

    static int preprocess(int theIntNeedRounding)
    {
        return theIntNeedRounding/100;
    }

    public Sample(string theIntAsString)
    {
        _intField = preprocess(theIntAsString)
    }

    public Sample(int theIntNeedRounding)
    {
        _intField = preprocess(theIntNeedRounding)
    }

    public int IntProperty  => _intField;

    private readonly int _intField;
}

如果需要设置多个字段,ValueTuple会非常有用。

如果没有初始化方法就不能令人满意地实现你想要的(例如,因为你想在初始化代码之前做太多事情,或者将它包装在try-finally中,或者其他什么),你可以让任何或所有构造函数通过引用初始化例程来传递只读变量,然后该例程将能够随意操作它们。

public class Sample
{
    private readonly int _intField;
    public int IntProperty => _intField; 

    private void setupStuff(ref int intField, int newValue) => intField = newValue;

    public Sample(string theIntAsString)
    {
        int i = int.Parse(theIntAsString);
        setupStuff(ref _intField,i);
    }

    public Sample(int theInt) => setupStuff(ref _intField, theInt);
}

在构造函数体之前,使用以下任意一个:

: base (parameters)

: this (parameters)

例子:

public class People: User
{
   public People (int EmpID) : base (EmpID)
   {
      // Add more statements here.
   }
}

注意:上面的大多数解决方案都不适用于结构体。

不幸的是,在构造函数调用的方法中初始化struct字段不会被编译器识别,并将导致2个错误:

字段xxxx必须被完全赋值… 在方法中,如果你有只读字段:只读字段不能被赋值,除非在构造函数中。

例如,当你只需要做一些简单的检查来决定调用指向哪个构造函数时,这真的很令人沮丧。