Search code examples
c#-3.0partial-classespartial-methods

Calling Partial Methods in C#


i was recently digging on new partial methods in c#3.0, i understood the use of partial class, that it could be chunked into multiple file one contain the definition and other declaration, but i wanted to know,i created a partial class like below:

in class1.cs
partial class A
{
   partial void Method();
}
in class2.cs
partial class A
{
  partial void Method()
  {
    Console.WriteLine("Hello World");
  }
}
now in class3.cs
class MainClass
{
  static void Main()
  {
    A obj = new A();
    obj.Method(); //Here i cannot call the "Method" method.
  }
}

then whats the use of creating partial method, i read on MSDN that, at runtime, compiler compiles the class into one, in that case compiler should be getting the "Method" method implementation also, then why it dont allow me to call the "Method" method in the main method, can anyone correct me if i am wrong, and tell me why i am unable to call this partial method in main.


Solution

  • From MSDN

    No access modifiers or attributes are allowed. Partial methods are implicitly private.

    It's a private method, so you can't call it from main.