0

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.

KyloRen
  • 2,691
  • 5
  • 29
  • 59
  • 1
    Why do you think it would change by itself? If the property is a member of that object, then it will be there for the lifetime of the object, no? – OldProgrammer Nov 24 '16 at 22:44
  • No, properties do not "lose" their values over time (unless you specifically program that behaviour). – Blorgbeard Nov 24 '16 at 22:44
  • 1
    @OldProgrammer, I don't think I suggested it would change by itself, I believe I asked if it could drop its value. ie, in VBA global variables can lose their value in certain circumstances, but they are considered to be able to hold their value for the entirety of the life of the application. – KyloRen Nov 24 '16 at 22:47
  • No, it is there forever. C# <> VBA :) – OldProgrammer Nov 24 '16 at 22:51
  • @OldProgrammer, thanks. – KyloRen Nov 24 '16 at 22:58
  • 1
    Note that `public MainWindow() { example = "SomeString"; InitializeComponent(); }` is a ___non-static___ constructor while `public static string example { get; set; }` is a __`static`__ property. So the property will not be initialized (meaning it has definite value `null`) unless some instance has been created, and it will be set _again_ each time a new instance is created. Maybe you wanted a `statc` constructor, or just an initializer on the auto-property (new in C# 6.0 (2015))? – Jeppe Stig Nielsen Nov 24 '16 at 23:26
  • @JeppeStigNielsen, where should the property be to assign a value? Something like this? `public static void Method() { example = "SomeString"; }` – KyloRen Nov 24 '16 at 23:46
  • __If__ you do not want a `static` property, you can just remove the word `static` from your code. __If__ you actually want a static property, you can initialize it in a static constructor. It looks like this: `static MainWindow() { example = "SomeString"; }` Note that a constructor has the same name as the class/struct it is a member of and has no return type (do not write `void`). With C# 6.0, you do not need any constructor; you can simply write: `public static string example { get; set; } = "SomeString";` – Jeppe Stig Nielsen Nov 25 '16 at 09:04

1 Answers1

1

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.

rucamzu
  • 926
  • 1
  • 9
  • 16