What is an automatic variable name for automatically implemented properties

I am trying to do this:

public string LangofUser { get { return string.IsNullOrEmpty("how to get value?") ? "English" : "how to get value?"; } set; } 

Should I do this?

 string _LangofUser public string LangofUser { get { return string.IsNullOrEmpty(_LangofUser) ? "English" : _LangofUser; } set { _LangofUser = value}; } 
+4
source share
5 answers

This mix of automatic and non-automated properties in C # is not possible. The property must be fully automatic or normal.

Note. Even with a fully automatic property, there is no way to refer to the support field from a C # source in a strongly typed way. This is possible through reflection, but it depends on the details of the compiler implementation.

+11
source

As others have said, do not try to mix automatic and regular properties. Just write a regular property.

If you want to know what secret names we create behind the scenes for the hidden magic of the compiler, see

Where to find out about the "magic names" of the VS debugging device

but do not rely on it; it can change at any time at our whim.

+3
source

If you provide your own property implementation, it is not automatic. So yes, you need to create an instance.

0
source

If you want to keep the auto property and still have a default value, why don't you initialize it in your constructor?

 public class MyClass { public MyClass() { LangOfUser = "English"; } public string LangOfUser { get; set; } } 
0
source

All Articles