In this example, I am discussing the following regarding partial method and Partial class- Partial Method with/without the implementation and how the call to the partial method resolved
- Where the partial method can be defined
- Partial Method usage exploration
Let me first give the definition/example.
namespace PartialMethod1
{
class Program
{
static void Main(string[] args)
{
PartialMethodClass cls = new PartialMethodClass();
cls.Action();
// it is not allowed to call partial method external to the class
// cls.DebugPrint();
}
}
// This class is the container class of one Partial Method
// In this example, we are going to demonstrate that you can
// have a partial method declaration, but the partial method
// declaration does not have necessary to be implemented,
// which means that it is just a stub and can be ignored by the compiler.
//
public partial class PartialMethodClass
{
public void Action()
{
DebugPrint("PartialMethodClass::Action()");
}
// the reason why the partial method does not have any access modifiers
// such as
// * virtual
// * abstract
// * override
// * new
// * sealed
// * extern
//
partial void DebugPrint(string message);
}
// This is a partial static partial method
// which exist to help adding some partial method ?
// QUESTION
// is it possible to have partial method in the Extension Method?
public static class PartialMethodsExtensionClass
{
// because a partial class can only appear on a Partial class,
// and mostly the Extension class name is different from the base class
// which menas you cannot implementing the partial class in Extension Method
//
//static partial void DebugPrint(this PartialMethodClass cls, string message)
//{
// System.Console.WriteLine("PartialMethodClass: " + message);
//}
}
// While you can have the Partial method defined somewhere else(restricted to be
// defined in a partial class that matches the previous definition)
// or you can NOT to have the partial class definition, in which case,
// the call to partial method will be optimized away
//
public partial class PartialMethodClass
{
#if PARTIAL_METHOD
partial void DebugPrint(string message)
{
System.Console.WriteLine("PartialMethodClasss: " + message);
}
#endif
}
}
So the answer to the above question includes
If there is no partial method implementation, the call to partial method will be optimized away (nature of compiled language)
Partial method can only be declared/implemented in the partial class, it is allowed to have multiple 'partial class definition'; It is not allowed to have any access modifier on the partial method, you cannot access the Partial method from outside of the partial class; And you can NOT define partial method across assembly boundaries.
// This is assembly "PartialMethod2"
namespace PartialMethod2
{
class Program
{
static void Main(string[] args)
{
}
}
}
namespace PartialMethod1
{
public partial class PartialMethodClass
{
// this will failed to compile
partial void DebugPrint(string message)
{
System.Console.WriteLine("PartialMethodClasss: " + message);
}
}
}
Since you basically cannot define/use partial method across assembly boundary, it render it less useful to define some contract for client to implement the partial methods, however, it does help you to organize your code file more wisely.