14.5.5 Invocation expressions

An invocation-expression is used to invoke a method.
invocation-expression:
primary-expression ( argument-listopt )
C# LANGUAGE SPECIFICATION
142
The primary-expression of an invocation-expression must be a method group or
a value of a delegate-type.
If the primary-expression is a method group, the invocation-expression is a
method invocation (?4.5.5.1). If
the primary-expression is a value of a delegate-type, the
invocation-expression is a delegate invocation
(?4.5.5.2). If the primary-expression is neither a method group nor a
value of a delegate-type, a compiletime
error occurs.
The optional argument-list (?4.4.1) provides values or variable references
for the parameters of the method.
The result of evaluating an invocation-expression is classified as follows:
?If the invocation-expression invokes a method or delegate that returns
void, the result is nothing. An
expression that is classified as nothing cannot be an operand of any
operator, and is permitted only in the
context of a statement-expression (?5.6).
?Otherwise, the result is a value of the type returned by the method or
delegate.
14.5.5.1 Method invocations
For a method invocation, the primary-expression of the
invocation-expression must be a method group. The
method group identifies the one method to invoke or the set of overloaded
methods from which to choose a
specific method to invoke. In the latter case, determination of the
specific method to invoke is based on the
context provided by the types of the arguments in the argument-list.
The compile-time processing of a method invocation of the form M(A), where
M is a method group and A is
an optional argument-list, consists of the following steps:
?The set of candidate methods for the method invocation is constructed.
Starting with the set of methods
associated with M, which were found by a previous member lookup (?4.3),
the set is reduced to those
methods that are applicable with respect to the argument list A. The set
reduction consists of applying
the following rules to each method T.N in the set, where T is the type in
which the method N is declared:
If N is not applicable with respect to A (?4.4.2.1), then N is removed
from the set.
If N is applicable with respect to A (?4.4.2.1), then all methods declared
in a base type of T are removed
from the set.
?If the resulting set of candidate methods is empty, then no applicable
methods exist, and a compile-time
error occurs. If the candidate methods are not all declared in the same
type, the method invocation is
ambiguous, and a compile-time error occurs (this latter situation can only
occur for an invocation of a
method in an interface that has multiple direct base interfaces, as
described in ?0.2.5).
?The best method of the set of candidate methods is identified using the
overload resolution rules of
?4.4.2. If a single best method cannot be identified, the method
invocation is ambiguous, and a
compile-time error occurs.
?Given a best method, the invocation of the method is validated in the
context of the method group: If the
best method is a static method, the method group must have resulted from a
simple-name or a memberaccess
through a type. If the best method is an instance method, the method group
must have resulted
from a simple-name, a member-access through a variable or value, or a
base-access. If neither of these
requirements are true, a compile-time error occurs.
Once a method has been selected and validated at compile-time by the above
steps, the actual run-time
invocation is processed according to the rules of function member
invocation described in ?4.4.3.
[Note: The intuitive effect of the resolution rules described above is as
follows: To locate the particular
method invoked by a method invocation, start with the type indicated by the
method invocation and proceed
up the inheritance chain until at least one applicable, accessible,
non-override method declaration is found.
Then perform overload resolution on the set of applicable, accessible,
non-override methods declared in that
type and invoke the method thus selected. end note]
Chapter 14 Expressions
143
14.5.5.2 Delegate invocations
For a delegate invocation, the primary-expression of the
invocation-expression must be a value of a
delegate-type. Furthermore, considering the delegate-type to be a function
member with the same parameter
list as the delegate-type, the delegate-type must be applicable (?4.4.2.1)
with respect to the argument-list of
the invocation-expression.
The run-time processing of a delegate invocation of the form D(A), where D
is a primary-expression of a
delegate-type and A is an optional argument-list, consists of the following
steps:
?D is evaluated. If this evaluation causes an exception, no further steps
are executed.
?The value of D is checked to be valid. If the value of D is null, a
System.NullReferenceException is thrown and no further steps are executed.
?Otherwise, D is a reference to a delegate instance. A function member
invocation (?4.4.3) is performed
on the method referenced by the delegate. If the method is an instance
method, the instance of the
invocation becomes the instance referenced by the delegate.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值