Featured post
visual studio 2010 - Compile error using C# automatically implemented properties -
a c# noob, trying use sharpdevelop utility convert vb.net application c#.
i noticing automatically implemented properties generating lot of errors. example, take following property:
public sqldatetime dateofbirth { get; set; }
whenever try access implied underlying module level variable, _dateofbirth, error.
error 699 name '_dateofbirth' not exist in current context d:\users\chad\desktop\besi csharp\besi\besi.businessobjects.convertedtoc#\chinavisa.cs 240 13 besi.businessobjects.converted
i expand properties declarations out full-fledged properties, should n't necessary , i'd understand why getting error.
you cannot access compiler-created backing variable - must use property. compiler-generated backing field named in such way prevent accessing (it not named _dateofbirth
, named <dateofbirth>k__backingfield
).
access property directly - if need manipulate backing field directly don't use automatically implemented property.
just side note - doesn't matter property name (it implementation detail , change on different versions of compiler or different compiler implementations altogether). field given identifier designed meet naming restrictions of clr fail naming restrictions of c# making impossible ever write straight c# code accesses variable directly.
also remember automatically implemented properties not public fields. shorthand compiler expands (sort of macro).
so class:
class bar { public object foo { get; set; } }
gets expanded this:
class bar { [compilergenerated] private object <foo>k__backingfield; public object foo { [compilergenerated] { return this.<foo>k__backingfield; } [compilergenerated] set { this.<foo>k__backingfield = value; } } }
it still full property - allowing compiler write getters , setters you.
- Get link
- X
- Other Apps
Comments
Post a Comment