Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the automatic variable name of an auto-implemented properties

Tags:

c#

I'm trying to do this:

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

do I have to do this?

string _LangofUser
public string LangofUser 
     { 
       get 
       { 
         return string.IsNullOrEmpty(_LangofUser) ? "English" : _LangofUser; 
       } 
       set { _LangofUser = value};
     }
like image 373
Fredou Avatar asked Dec 09 '22 05:12

Fredou


2 Answers

This mixing of auto-implement and not-auto-implemented properties in C# is not possible. A property must be fully auto-implemented or a normal property.

Note: Even with a fully auto-implemented property there is no way to reference the backing field from C# source in a strongly typed manner. It is possible via reflection but that's depending on implementation details of the compiler.

like image 195
JaredPar Avatar answered May 03 '23 23:05

JaredPar


As others have said, don't try to mix automatic and regular properties. Just write a regular property.

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

Where to learn about VS debugger 'magic names'

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

like image 45
Eric Lippert Avatar answered May 03 '23 23:05

Eric Lippert