KyloRen KyloRen - 12 days ago 6
C# Question

How long does an auto property last?

I could not find any information on how long an auto property will last in an application?

What I mean by that is do they keep their default value for the entire time the application is running?
ie, if I initialize an auto property at startup like in the example code below,

namespace MyApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
example = "SomeString";
InitializeComponent();

}

.....////other stuff

public static string example { get; set; }
}
}


Will it keep that value for the entirety of the application running? Apart from the fact that the value can be changed through re-assigning or through
INotifyPropertyChanged
are the some instances where the value will be lost and need to be re-assigned?

If the value can lost, what are the causes?

Note: I went through all the tags on
automatic-properties
so if this is a duplicate please let me know as I could not find anything in my searches on this.

Answer

The lifetime of a static automatic property is completely unrelated to it being an automatic property, and instead is determined by the static qualifier.

As stated by the documentation:

A static variable comes into existence before execution of the static constructor for its containing type, and ceases to exist when the associated application domain ceases to exist.

The value of the static automatic property is managed in the same way that with any other variable.