Do properties always have a value when unset?

后端 未结 4 2083
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 03:04

I have a property like this:

public Tuple[] Breadcrumbs { get; set; }

and I have a test in one of my methods like thi

4条回答
  •  庸人自扰
    2021-01-18 03:30

    An automatically-implemented property which hasn't been explicitly set by any code will always have the default value for the property type - which is null for reference types. (For int it would be 0, for char it would be '\0' etc).

    An automatically implemented property like this is just equivalent to:

    private PropertyType property;
    public PropertyType Property
    {
        get { return property; }
        set { property = value; }
    }
    

    ... except that the backing variable has an unspeakable name (you can't refer to it in code) so it will always start off with the default value for the type.

提交回复
热议问题