c# - 链接基本构造函数时,如何在重载中重用初始化代码

标签 c# .net

请参阅下面评论中嵌入的问题。

public class CustomException : Exception
{
    private readonly string _customField;

    public CustomException(string customField, string message)
        : base(message)
    {
        // What's the best way to reuse the customField initialization code in the
        // overloaded constructor? Something like this(customField)
    }

    public CustomException(string customField)
    {
        _customField = customField;
    }
}

我愿意考虑重用基本构造函数并最小化初始化代码的替代实现。我想将 _customField 保留为只读,如果我提取单独的初始化方法,这是不可能的。

最佳答案

public CustomException(string customField) : this(customField,"")
{
}

关于c# - 链接基本构造函数时,如何在重载中重用初始化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12072241/

相关文章:

c# - 如何更改 DataTable 或 GridView 中日期时间的日期文化?

javascript - ASP.NET 如何使用 ajax 将文本从文本框推送到另一个页面

C# Linq 查询查找无效条目

c# - 连接不同的编程语言

c# - 我要添加什么程序集引用才能使用 System.Win32?

c# - 如何在 JSON 中指定子类类型(在 REST Web 服务中)

.net - NHibernate 控制台

c# - 从 List<string[]> 到 String[,]

c# - GridView 对行进行分组并更改颜色

.net - 与DataGridColumn.Visibility匹配的F#模式