What does "Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one" mean?

时间:2015-11-18 21:02:30

标签: c# partial-methods

While reading about "partial methods" in the C# documentation, I found the following sentence:

Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.

Can someone explains me with an example what this sentence means?

1 个答案:

答案 0 :(得分:5)

That means that the following code, note the parameter names, successfully compiles:

// Definition in file1.cs
partial void Foo(string foo);

// Implementation in file2.cs
partial void Foo(string bar)
{
  // method body
}

Just as it is the case with interfaces:

public interface IFoo
{
    void Bar(string baz);
}

public class Foo : IFoo
{
    public void Bar(string qux)
    {
    }
}

It's parameter order that matters. You can choose whatever valid name you want in the implementation.

For the "type parameter names" part, that part specifically is about generics (T versus V), where again the order matters, not the name:

// Definition in file1.cs
partial void Foo<T>(string foo);

// Implementation in file2.cs
partial void Foo<V>(string bar)
{
  // method body
}