C# 对DefaultValueAttribute的误解

    看到这个特性时,以为能少声明内部字段,像以下代码那么写

public class TestValue
{
    [DefaultValue(true)]
    public bool IsSame
    {
        get;
        set;
    }
}

   但其实测试过后,直接获取IsSame属性,并不是true,而且测试其他数据类型,也是一样

   后来网上查了下,才发现之前的理解是错的,一下是MSDN的解释

A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.

DefaultValueAttribute并不能自动初始化一个字段,必须在代码中手动初始化字段。

另外这个特性主要是供可视化设计器使用

原文地址:https://www.cnblogs.com/xyz0835/p/3048950.html