Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 推荐具有属性的最佳构造函数?_C#_Constructor - Fatal编程技术网

C# 推荐具有属性的最佳构造函数?

C# 推荐具有属性的最佳构造函数?,c#,constructor,C#,Constructor,有没有一种方法来表示在C#中使用的最佳构造函数,以便开发人员知道默认情况下选择哪个构造函数 假设你有一门课: public class MyClass { public MyClass(int requestReferenceId, int controlNumber1, string companyNumber, int recordId, DateTime mySpecialDate, DateTime myOtherDate, bool isProduction)

有没有一种方法来表示在C#中使用的最佳构造函数,以便开发人员知道默认情况下选择哪个构造函数

假设你有一门课:

public class MyClass
{
    public MyClass(int requestReferenceId, int controlNumber1, string companyNumber, int recordId, DateTime mySpecialDate, DateTime myOtherDate, bool isProduction)
        : this(controlNumber1, controlNumber1, companyNumber, recordId, mySpecialDate, myOtherDate)
    {
        IsProduction = isProduction;
    }

    public MyClass(int requestReferenceId, int controlNumber1, string companyNumber, int recordId, DateTime mySpecialDate, DateTime myOtherDate)
        : this(requestReferenceId, controlNumber1, companyNumber, recordId)
    {
        MySpecialDate = mySpecialDate;
        MyOtherDate = myOtherDate;
    }

    public MyClass(int requestReferenceId, int controlNumber1, string companyNumber, int recordId, DateTime mySpecialDate)
        : this(requestReferenceId, controlNumber1, companyNumber, recordId)
    {
        MySpecialDate = mySpecialDate;
        MyOtherDate = mySpecialDate;
    }

    public MyClass(int requestReferenceId, int controlNumber1, string companyNumber, int recordId)
        : this()
    {
        if (controlNumber1 == 0)
            throw new ArgumentException("Invalid Parameter: cannot be 0", "controlNumber1");
        if (recordId == 0)
            throw new ArgumentException("Invalid Parameter: cannot be 0", "recordId");
        if (string.IsNullOrWhiteSpace(companyNumber))
            throw new ArgumentException("Invalid Parameter: cannot be empty", "companyNumber");

        RequestReferenceId = requestReferenceId == 0 ? 1 : requestReferenceId;
        ControlNumber1 = controlNumber1;
        ControlNumber2 = controlNumber1;
        RecordId = recordId;
        CompanyNumber = companyNumber;

    }
    public MyClass()
    {
        MyStringProperty = string.Empty;
        MySpecialDate = DateTime.Now;
        MyOtherDate = MySpecialDate;
        RequestReferenceId = 1;
        ControlNumber1 = 1;
        ControlNumber2 = 1;
        TransactionNumber = 1;
        ReferenceNumber = 1;
        IsProduction = true;
    }

    public string MyStringProperty { get; set; }
    public DateTime MySpecialDate { get; set; }
    public DateTime MyOtherDate { get; set; }
    public bool IsProduction { get; set; }
    public int RequestReferenceId { get; set; }
    public int ControlNumber1 { get; set; }
    public int ControlNumber2 { get; set; }
    public int TransactionNumber { get; set; }
    public int ReferenceNumber { get; set; }
    public string CompanyNumber { get; set; }
    public int RecordId { get; set; }

    public string TraceNumber
    {
        get {  return string.Format("{0}-{1}", ControlNumber2, RecordId)}
    }

}
并希望通知开发人员以下版本是推荐使用的版本:

public MyClass(int requestReferenceId, int controlNumber1, string companyNumber, int recordId, DateTime mySpecialDate)
在C#中是否有类似下面这样的东西可以做到这一点?我知道您可以在内部构建属性,但是否有C#特性可以实现这一点?我是不是错过了一些基本的东西..哎呀。。。C#


没有
[推荐]
属性,因为

  • 您不知道开发人员的用例,因此无法判断哪一个是最合适的
  • 我希望您不要在代码中故意留下糟糕的实现,这样就不会出现这种情况
  • 根据具体情况,您有几个选择:

    XML注释 通过使用XML注释,Intellisense将获取它,并在使用您的构造时向开发人员显示。Visual Studio为此提供了一个快捷方式,键入
    //
    ,将其扩展为

    /// <summary>
    /// 
    /// </summary>
    
    //
    /// 
    /// 
    
    [过时] 您可以将其他重载标记为较低优先级,而不是将一个重载标记为较高优先级。这是通过使用
    [过时]
    属性来完成的,这表明强烈不鼓励使用该方法


    我会问自己为什么会出现这种情况。为什么您的重载不是等价的?似乎有一个核心问题需要解决


    考虑使用可选参数来减少当前代码中的重载混乱

    首先,我建议您以另一种方式链接您的构造函数-将参数较少的构造函数链接(可能是间接链接)到一个包含所有参数的构造函数,并使其成为唯一设置任何内容的构造函数。这样你的作业就不会像现在一样分散在各处了

    第二,我会考虑使用可选的参数而不是所有的重载:

    public MyClass(
        int requestReferenceId,
        int controlNumber1,
        string companyNumber,
        int recordId,
        DateTime mySpecialDate, = default(DateTime)
        DateTime myOtherDate = default(DateTime),
        bool isProduction = false)
    {
        ...
    }
    

    那么你只有一个构造函数,所以你不必“推荐”任何东西。开发人员必须为所有必需的参数指定参数,但可以选择为哪些可选参数指定参数。不要忘记,不同的开发人员可能处于不同的环境中-如果您建议重载时不使用
    isProduction
    参数,但他们希望指定
    isProduction
    参数,这会导致您的建议与他们需要实现的目标之间发生冲突。

    您可以使用///注释,以便他们可以在IntelliSense中看到您的注释。使用XML文档中的注释高亮显示它,例如,
    ///因此…勇敢一点…添加答案!是什么让一个构造函数比另一个“更好”——我认为XML文档能够解释何时应该使用每个构造函数会很有帮助。如果一个构造函数比其他构造函数好,那么为什么首先要预见其他构造函数呢?在一个好的设计中,每个构造器都有自己的位置。这是真正的用户的阶级,看看哪一个最适合他的需要。注意,这是要考虑的东西,如果你正在编写一个图书馆。
    
    public MyClass(
        int requestReferenceId,
        int controlNumber1,
        string companyNumber,
        int recordId,
        DateTime mySpecialDate, = default(DateTime)
        DateTime myOtherDate = default(DateTime),
        bool isProduction = false)
    {
        ...
    }