C#中的方法签名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8808703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:32:09  来源:igfitidea点击:

Method Signature in C#

c#

提问by Usman

What is the Method Signature in the following

下面的方法签名是什么

int DoSomething(int a, int b);

Return type is a part of signature or not???

返回类型是否是签名的一部分???

采纳答案by ean5533

Return type is not part of the method signature in C#. Only the method name and its parameters types(but not the parameter names) are part of the signature. You cannot, for example, have these two methods:

返回类型不是 C# 中方法签名的一部分。只有方法名称及其参数类型(而不是参数名称)是签名的一部分。例如,您不能拥有以下两种方法:

int DoSomething(int a, int b);
string DoSomething(int a, int b);

To be clear: Methods cannot be overloaded based on their return type. They must have a unique name, unique parameter types, or pass their arguments differently (e.g. using outor ref).

需要明确的是:方法不能根据其返回类型进行重载。它们必须具有唯一的名称、唯一的参数类型,或者以不同的方式传递它们的参数(例如,使用outref)。

Edit:To answer your original question, the method signature for your method is:

编辑:要回答您的原始问题,您的方法的方法签名是:

DoSomething(int, int)

Note that this all applies to normal methods. If you're talking about delegates, then you should see keyboardP's answer. (Short version: return type IS part of the signature for a delegate).

请注意,这一切都适用于普通方法。如果您在谈论delegates,那么您应该看到 keyboardP 的回答。(简短版本:返回类型是委托签名的一部分)。

回答by DarthVader

DoSomething(int a, int b);

is the method signature,

是方法签名,

intis the return type.

int是返回类型。

take a look at this :Signatures and overloading

看看这个:签名和重载

回答by Sean U

From MSDN:

来自MSDN:

The signature of a method consists of the name of the method and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. The signature of a method specifically does not include the return type

方法的签名由方法的名称和每个形式参数的类型和种类(值、引用或输出)组成,按从左到右的顺序考虑。方法的签名具体不包括返回类型

Edit:That is from old documentation. It seems the definition of 'signature' has changed since then. Now a method has two different signatures, one for the purpose of overloading and one for the purposes of determining delegate compatibility. See keyboardP's answer below for more details.

编辑:那来自旧文档。从那时起,“签名”的定义似乎发生了变化。现在一个方法有两种不同的签名,一种用于重载,一种用于确定委托兼容性。有关更多详细信息,请参阅下面的键盘 P 的回答。

回答by MatthiasG

The signature does not contain the return type. Neither the parameter names. In your case it would be DoSomething(int, int)

签名不包含返回类型。参数名称都没有。在你的情况下,这将是DoSomething(int, int)

回答by keyboardP

From MSDN:

MSDN

A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.

出于方法重载的目的,方法的返回类型不是方法签名的一部分。但是,在确定委托与其指向的方法之间的兼容性时,它是方法签名的一部分。

To clarify, in your example the return type is not part of the signature. However, when you're matching the signature of a delegate, it is considered part of the signature. From MSDN:

澄清一下,在您的示例中,返回类型不是签名的一部分。但是,当您匹配委托的签名时,它被视为签名的一部分。从MSDN

Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own delegated method.

任何与委托签名匹配的方法(由返回类型和参数组成)都可以分配给委托。这使得以编程方式更改方法调用以及将新代码插入现有类成为可能。只要知道委托的签名,就可以分配自己的委托方法。

So I believe it's based on context. Most of the time, and as shown in your code, the return type is not part of it. However, in the context of delegation, it is considered part of it.

所以我相信它是基于上下文的。大多数情况下,如您的代码所示,返回类型不是其中的一部分。然而,在授权的背景下,它被认为是它的一部分。

回答by Eric Lippert

Is the return type is a part of signature or not?

返回类型是否是签名的一部分?

It depends on why you are asking the question. Why do you care?

这取决于你问这个问题的原因。你为什么在乎?

There are two definitions of method signature. The C# language definition does notinclude the return type, and uses the signature of the method to determine whether two overloads are allowed. Two methods with the same signature are not allowed in a type. Since C# does not consider the return type to be a part of the signature, C# does not allow two methods that differ only in return type to be declared in the same type.

方法签名有两种定义。C#语言的定义并没有包括返回类型,并使用该方法的签名,以确定两个重载是否是允许的。一个类型中不允许有两个具有相同签名的方法。由于 C# 不将返回类型视为签名的一部分,因此 C# 不允许在同一类型中声明两个仅返回类型不同的方法。

The CLR, however, does include the return type in the signature. The CLR allows for two methods to be in the same type that differ only in return type.

但是,CLR 确实在签名中包含了返回类型。CLR 允许两个方法属于同一类型,仅返回类型不同。

To be more specific: in C# the signature consists of the methods:

更具体地说:在 C# 中,签名由以下方法组成:

  • name
  • number of type parameters
  • number of formal parameters
  • type of each formal parameter
  • out/ref/value-ness of each formal parameter
  • 姓名
  • 类型参数的数量
  • 形式参数的数量
  • 每个形参的类型
  • 每个形参的 out/ref/value-ness

with the following additional notes:

附有以下附加说明:

  • generic type parameter constraints are not part of the signature
  • return type is not part of the signature
  • type parameter and formal parameter names are not part of the signature
  • two methods may not differ onlyin out/ref
  • 泛型类型参数约束不是签名的一部分
  • 返回类型不是签名的一部分
  • 类型参数和形式参数名称不是签名的一部分
  • 两种方法可能不会在 out/ref 上有所不同

In the CLR the signature consists of:

在 CLR 中,签名包括:

  • name
  • number of type parameters
  • number of formal parameters
  • type of each formal parameter including modopts and modreqs
  • return type including modopts and modreqs
  • ref/value-ness of each formal parameter
  • 姓名
  • 类型参数的数量
  • 形式参数的数量
  • 每个形参的类型,包括 modopts 和 modreqs
  • 返回类型包括 modopts 和 modreqs
  • 每个形参的引用/值

Note that the CLR does not distinguish between "ref int" and "out int" at all when considering signatures. Note that the CLR doesdistinguish between modopt/modreq types. (The way that the C# compiler deals with modopt/modreq types is too complex to summarize here.)

请注意,在考虑签名时,CLR 根本不区分“ref int”和“out int”。请注意,CLR确实区分了 modopt/modreq 类型。(C# 编译器处理 modopt/modreq 类型的方式过于复杂,无法在此总结。)

回答by Denis535

In the case when method have generic arguments understanding of signature becomes more confusing.

在方法具有通用参数的情况下,对签名的理解变得更加混乱。

Generic types declaring on class level are considered as normal types.

在类级别声明的泛型类型被视为正常类型。

But generic types declaring on method level are considered as index in method's generic arguments.

但是在方法级别声明的泛型类型被视为方法泛型参数中的索引。

For example these all methods have different signatures.

例如,这些所有方法都有不同的签名。

class MyClass<TValue>
{
    public void F(TValue v) { }       // generics: 0, arg: TValue
    public void F<X>(TValue v) { }    // generics: 1, arg: TValue
    public void F<X, Y>(TValue v) { } // generics: 2, arg: TValue

    public void F<X>(X v) { }    // generics: 1, arg: 0
    public void F<X, Y>(X v) { } // generics: 2, arg: 0
    public void F<X, Y>(Y v) { } // generics: 2, arg: 1
}