7.5.4 Member access
A member-access consists of a primary-expression or a predefined-type, followed by a "." token, followed by an identifier.
- member-access:
- primary-expression . identifier
predefined-type . identifier - predefined-type: one of
- bool byte char decimal double float int long
object sbyte short string uint ulong ushort
A member-access of the form E.I, where E is a primary-expression or a predefined-type and I is an identifier, is evaluated and classified as follows:
- If
Eis a namespace andIis the name of an accessible member of that namespace, then the result is that member and, depending on the member, is classified as a namespace or a type. - If
Eis a predefined-type or a primary-expression classified as a type, and a member lookup (Section 7.3) ofIinEproduces a match, thenE.Iis evaluated and classified as follows:- If
Iidentifies a type, then the result is that type. - If
Iidentifies one or more methods, then the result is a method group with no associated instance expression. - If
Iidentifies astaticproperty, then the result is a property access with no associated instance expression. - If
Iidentifies astaticfield:If the field is
readonlyand the reference occurs outside the static constructor of the class or struct in which the field is declared, then the result is a value, namely the value of the static fieldIinE.Otherwise, the result is a variable, namely the static field
IinE. - If
Iidentifies astaticevent:If the reference occurs within the class or struct in which the event is declared, and the event was declared without event-accessor-declarations (Section 10.7), then
E.Iis processed exactly as ifIwas a static field.Otherwise, the result is an event access with no associated instance expression.
- If
Iidentifies a constant, then the result is a value, namely the value of that constant. - If
Iidentifies an enumeration member, then the result is a value, namely the value of that enumeration member. - Otherwise,
E.Iis an invalid member reference, and a compile-time error occurs.
- If
- If
Eis a property access, indexer access, variable, or value, the type of which isT, and a member lookup (Section 7.3) ofIinTproduces a match, thenE.Iis evaluated and classified as follows:- First, if
Eis a property or indexer access, then the value of the property or indexer access is obtained (Section 7.1.1) andEis reclassified as a value. - If
Iidentifies one or more methods, then the result is a method group with an associated instance expression ofE. - If
Iidentifies an instance property, then the result is a property access with an associated instance expression ofE. - If
Tis a class-type andIidentifies an instance field of that class-type:If the value of
Eisnull, then aSystem.NullReferenceExceptionis thrown.Otherwise, if the field is
readonlyand the reference occurs outside an instance constructor of the class in which the field is declared, then the result is a value, namely the value of the fieldIin the object referenced byE.Otherwise, the result is a variable, namely the field
Iin the object referenced byE. - If
Tis a struct-type andIidentifies an instance field of that struct-type:If
Eis a value, or if the field isreadonlyand the reference occurs outside an instance constructor of the struct in which the field is declared, then the result is a value, namely the value of the fieldIin the struct instance given byE.Otherwise, the result is a variable, namely the field
Iin the struct instance given byE. - If
Iidentifies an instance event:If the reference occurs within the class or struct in which the event is declared, and the event was declared without event-accessor-declarations (Section 10.7), then
E.Iis processed exactly as ifIwas an instance field.Otherwise, the result is an event access with an associated instance expression of
E.
- First, if
- Otherwise,
E.Iis an invalid member reference, and a compile-time error occurs.