Do properties always have a value if it is not set?

I have a property like this:

public Tuple<String, String>[] Breadcrumbs { get; set; } 

and I have a test in one of my methods:

 if (Breadcrumbs != null && Breadcrumbs.Length > 0) { } 

Depending on when this method is called, Breadcrumbs may not be installed. In one test, Breadcrumbs == null evaulate to true.

Do undefined properties always have a value? (Will it always be null ?)

+7
source share
4 answers

An automatically implemented property that was not explicitly set by any code will always have a default value for the property type - which is null for reference types. (For int it will be 0, for char it will be "\ 0", etc.).

An automatically implemented property like this is simply equivalent:

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

... except that the support variable has an unspeakable name (you cannot reference it in code), so it will always start with the default value for the type.

+18
source

Auto properties use support fields and compiled for regular properties.

If the property type is a reference type, the value will be null if the value is not the default value.

+3
source

Class member variables (called fields) and thus supporting property variables are always initialized by default unless explicitly initialized, which is null for reference types. The default value for all types is a value whose binary representation consists of all bits set to 0.

C #, on the other hand, requires you to explicitly initialize local variables. These are: variables declared in methods, constructors and accessory properties and parameters of the out method; that is, they are treated as undefined until you assign them a value.

+2
source

It is logically impossible for him to have no meaning. It will have to return something, several bundles of 1 and 0, which are at least considered a reference to Tuple<String, String>[] , so it matters to such an extent.

This is also the case when all fields in classes are set by default ( default(T) for any type T , which are null for all types of links). Otherwise, one could have an object that was in a state that not only made no sense in terms of its action, but also had no meaning in the rules of what .NET expects from objects. This includes hidden fields behind automatic properties.

Now in some languages ​​we can do the equivalent of this:

 public Tuple<String, String>[] Breadcrumbs { get { Tuple<String, String>[] whatIWillSend; return whatIWillSend; } } 

If it was allowed, whatIWillSend will have a value determined not by any convincing decision on your part, but by what was in memory at that time. It can be null, it can be a valid Tuple<String, String>[] just by coincidence (but not the one you wanted to use!), It could be Dictionary<int, List<string>> , which now will be think runtime Tuple<String, String>[] (there goes security like the whole system), it can be a quarter of the decimal structure. (In languages ​​that allow such things, it may also be well known that debuggers for such languages ​​are installed in these cases just to help find errors caused by them).

This is the closest we can get a property that doesn't matter. Note that:

  • It will still matter, not a meaningful one.
  • We are not allowed to do this in C # anyway.
0
source

All Articles