Timesaver: Awesome New C# Property Syntax Shortcut
Linked In recommendations: Your job happiness Barometer

Strange C# 3.0 Feature: Partial Methods ... ... What ?!!

At first it was hard to believe or even think up of the concept of what might be a partial method. How can you have a partial method? How does one write it ... by omitting some code? Looks like the C# team have given birth to strange new language feature called partial methods.

Before I go any further there are 2 rules regarding partial methods that must be said.

1. Partial methods can only defined in a partial class

2. Partial method return type must be void and cannot contain any out parameter

Perhaps the naming of partial method has been wrong. It would have been better to call it skeleton method. A partial method is a method that can be called in a function and the signature of the function may be defined but may not have any body. It is like defining a method signature which we may implement later.

For example lets assume we have a class called Foo which has a method called do something and looks like code below

partial class Foo
{
    public void DoSomething(string someParam)
    {            
        // Do something important here ...some logic
        CallSomeMethodSomewhere(param);
    }
}

It is visible that the we are going to do something important inside the method. While we are writing this method we may decide that we want to log the input parameters but at the time we do not want to worry about the logging logic right now and and would rather worry about the method logic we are writing. Now this is where we might want to write a partial method. Let try to how a partial method is declared and used.

partial class Foo
{
    public void DoSomething(string someParam)
    {            
        // Log the input
        Log("DoSomething", someParam);
        // Do something important here
        CallSomeMethodSomewhere(param);
    }

    partial void Log(string functionName, string parameters); }

Now this is a perfectly valid code and will show no compilation errors. Since we have not yet defined the body of the function "Log" the C# 3.0 compiler will just ignore our function call. So a partial method lets us define a method signature and call it even when there is no implementation. Also compiler does not emit any IL for the call that we made. So the code shown below is fully ignored.

Log("DoSomething", someParam); 

Now after we are done with implementing out class, we have decided to write the function
and provide a function body. So now our code looks like this ...

partial class Foo
{     public void DoSomething(string someParam)     {                     // Log the input
        Log("DoSomething", someParam);         // Do something important here
        CallSomeMethodSomewhere(param)     }

    private void Log(string functionName, string parameters)     {
        Console.WriteLine(functionName + ":" + parameters);     }
}

So this way we are not changing the original code at all rather providing the implementation of the function later. One use of the function might be to write functions in you class and call them, while someone else may provide the concrete implementation in a function later. This will allow two persons co-ordinate and use virtual placeholders for code. This is a cleaner approach to write to-do functions later rather than providing a method body and throwing not implemented exception later.


Well ... anyway I found this to be a strange feature of C# 3.0. What did you think?

kick it on DotNetKicks.com

Comments

Egil Hansen

Interesting, so when would you use this instead of interfaces? Only during development like a to do list, or in libraries/abstract classes as well?

Either way, they are no interface replacement simply be cause of the two conditions listed above.

Mark Heath

I think this feature is intended mainly for automatic code generation, where a tool makes one half of the partial class, and allows the user to implement partial functions if they want to.

dann

I didn't know about this feature and this article is presenting it quite well. Probably it can be useful when writing a code generator. For daily tasks I think is better to use the standard programming ways (interfaces or throwing a NotImplementedException)

The comments to this entry are closed.