Method Signature in C#

前端 未结 7 812
误落风尘
误落风尘 2020-11-27 13:36

What is the Method Signature in the following

int DoSomething(int a, int b);

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

7条回答
  •  旧时难觅i
    2020-11-27 14:09

    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:

    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 out or ref).

    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).

提交回复
热议问题