Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#

Partial Methods - An Uncommon Note

Rate me:
Please Sign up or sign in to vote.
4.58/5 (9 votes)
27 Sep 2010CPOL3 min read 19.5K   7   8
A note about Partial methods

I think most people working in C# must know how Partial classes work. Partial classes allow you to divide your own class in one or more files but during runtime the files would be merged to produce a single coherent object. By this way, the classes could be made very small and also easily maintainable.

The most important part of a Partial class is with designers. While you work in Visual Studio, you must have found a portion of code that is generated automatically by the designer. Designers create a class which keeps on refreshing itself when certain modifications are made in the Designer itself. Partial classes allow you to create an alternate implementation of the same class so that the code you add to the class does not goes out when the Designer is refreshed.

Partial methods are another new addition to .NET languages that allow you to declare / define the same method more than once. Generally Microsoft introduced the concept of Partial method to deal with Designer generated code and to allow reserving the name of a method that one needed to be implemented later in original classes. By this way, the Designer will not produce any warning without implementing the actual method, but will allow future developers to implement or write their own logic on the same method that is defined.

C#
partial class MyPartialClass
{
    public void MyNormalMethod()
    {
        Console.WriteLine("Inside first Normal Method");
    }
    partial void MyPartialMethod(); 	//It is declared and will be 
				//defined later in another partial class
}

Let us suppose the MyPartialClass is generated by the designer. It declares the method MyPartialMethod which I have not defined yet.

C#
partial class MyPartialClass
{
    //public void MyNormalMethod()
    //{
    //    Console.WriteLine("Inside second Normal Method");
    //}
    partial void MyPartialMethod()
    {
        Console.WriteLine("Inside second Partial Method");
    }
}

In another implementation of MyPartialClass, I have defined the method MyPartialMethod which holds the actual implementation of the code. So if I create an object of MyPartialClass, it will hold both methods MyNormalMethod and MyPartialMethod and when it is called, it calls appropriately.

Another thing to note, the implementation of Partial method is not mandatory. If you do not define the partial method in another partial class, it will eventually be eliminated completely in runtime IL.

Limitations of a Partial Method

  1. Partial Method is by default private. Hence you can only call a partial method from within the partial class.

    C#
    partial class MyPartialClass
    {
        public void MyNormalMethod()
        {
            Console.WriteLine("Inside first Normal Method");
            this.MyPartialMethod();
        }
        partial void MyPartialMethod(); 	//It is declared and will be defined 
    				//later in another partial class
    }

    Thus even if you do not declare the method MyPartialMethod, you can even call it from within the class. During runtime, CLR will automatically call if it is implemented or will eliminate the call completely from the system.

  2. Partial method needed to be declared inside a Partial class. A partial class is only capable of redefining the part later in another file. So it is essential to mark the class as partial as well when it has a partial method.
  3. Cannot be marked as extern.
  4. Must have a void return type and also cannot use out parameter as argument.
  5. Partial method does not allow to write as virtual, sealed, new, override or extern.

Since partial method is created to generate methods in designer, most of the designers highly use partial methods. Even though it is very rarely used in real life, it would be worth it if you know about it.

For further reference:

Thank you for reading my post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Comments and Discussions

 
GeneralMy vote of 3 Pin
Trellium6-Oct-10 13:32
Trellium6-Oct-10 13:32 
GeneralMy vote of 5 Pin
Yury Goltsman5-Oct-10 10:50
Yury Goltsman5-Oct-10 10:50 
GeneralPartial methods in practice.... Pin
Jens Chr Juul Jensen5-Oct-10 10:27
Jens Chr Juul Jensen5-Oct-10 10:27 
GeneralMy vote of 3 Pin
EngleA5-Oct-10 5:16
EngleA5-Oct-10 5:16 
GeneralMy vote of 3 Pin
774655-Oct-10 4:05
774655-Oct-10 4:05 
GeneralMy vote of 4 Pin
JH644-Oct-10 16:12
JH644-Oct-10 16:12 
GeneralMy vote of 2 Pin
Crawfis4-Oct-10 12:56
Crawfis4-Oct-10 12:56 
GeneralNice article Pin
Crawfis4-Oct-10 12:55
Crawfis4-Oct-10 12:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.