Search code examples
c#.netpartial-classes

Why cant partial methods be public if the implementation is in the same assembly?


According to MSDN Documentation for partial classes :

Partial methods are implicitly private

So you can have this

// Definition in file1.cs
partial void Method1();

// Implementation in file2.cs
partial void Method1()
{
  // method body
}

But you can't have this

// Definition in file1.cs
public partial void Method1();

// Implementation in file2.cs
public partial void Method1()
{
  // method body
}

But why is this? Is there some reason the compiler can't handle public partial methods?


Solution

  • Because the MS compiler team did not have a requirement to implement this feature.

    Here is a possible scenario of what happen inside MS, because VS uses code gen for a lot of its features, one day a MS code gen developer decided that he/she needed to have partial methods so that the code generated api could be extended by outsiders, and this requirement led the compiler team to act and deliver.