I like to compare partial methods with abstract methods for easy understanding. Both method types don't declare body, but one exception is abstract methods needs to be implemented, but not partial methods need not be.
Consider below example
public partial class MyPartial
{
partial void Add(int a, int b, ref int result);
}
public abstract class MyAbstract
{
public abstract int Add(int a, int b);
}
public partial class MyPartial
{
partial void Add(int a, int b, ref int result)
{
result = a + b;
}
}
public abstract class MyImplementedAbstract : MyAbstract
{
public override int Add(int a, int b)
{
return a + b;
}
}
Some glaring facts
1. Partial method cannot return, but can take ref.
2. Partial method cannot have access modifier, by default it is Private.
3. If Partial methods are not declared, compiler ignores it.
So where are these getting used
1. Only scenario, i can think of is 'developing code generators'.
No comments:
Post a Comment