c# - Set a default value to a property -


Is it possible to set a default value without the property? Favorites with annotations

  [SetTheDefaultValueTo (true)] public bool IsTrue {get; Set; } [Set Default Default To (False)] Public Bull Eiffel {Received; Set; } Public Zero Some () {var isTrue = this.IsTrue; Var isFalse = this.IsFalse; No, there is no ability to set the value of the property there. Metadata. You can use a factory of any kind that will create an example of a square with reflection and then it can set the default value. But in a nutshell, you can use consultants (or field sets ... which have been taken by constructors) need to. To set the default value  

If there are many overloads for your controller, then you see.

BTW ... A highly requested feature for automatic properties will be for this kind of creation ...

  public string {{get; Set; } = "My default";  

... it is not currently present in C #, but it is expected that it will be in some future versions.

Oh, it becomes more fun because people have requested something like this ...

Public String Myself {Private string _myValue; Meet {Return _myValue ?? "My default"; } Set {_myValue = value; }}

... The advantage is that you can control the scope of the area to be available only in the property code so that you do not have to worry about anything else in your class. Please. Playing with the state without using gator / setter.


Comments