Is the backing field of a compiler generated event always guaranteed to use the same name as the event?

后端 未结 3 1912
盖世英雄少女心
盖世英雄少女心 2021-01-04 22:19

C# allows us to create custom event accessors.

Action _custom;
public event Action Custom
{
    add { _custom = (Action)Delegate.Combine( _custom, value ); }         


        
3条回答
  •  青春惊慌失措
    2021-01-04 23:07

    No - from the C# 4 spec (section 10.8.1):

    Within the class X, references to Ev are compiled to reference the hidden field _Ev instead. The name “_Ev” is arbitrary; the hidden field could have any name or no name at all.

    So while source-code compatibility is guaranteed, there's no guarantee about the name of the generated field. (In practice, I wouldn't expect this to change any time soon in the MS compiler - but it's not guaranteed, so you shouldn't make assumptions.)

提交回复
热议问题