.NET, C#

C# Tutorial – Partial method

I have some code to begin with Partial method in C# 3.0

using System;

namespace PartialMethod
{
    partial class MyClass
    {
        public MyClass()
        {
            MyMethod();
        }

        partial void MyMethod();
    }

    partial class MyClass
    {
        partial void MyMethod()
        {
            Console.WriteLine("Hello world!!!");
        }
    }

    // We only have one method definition
    //partial class MyClass
    //{
    //    partial void MyMethod()
    //    {
    //        Console.WriteLine("Hello world!!!");
    //    }
    //}

    // If we don't declare method declaration yet, we cannot write its definition
    //partial class MyNewClass
    //{
    //    partial void MyMethod()
    //    {
    //        Console.WriteLine("Hello world!!!");
    //    }
    //}

    class Program
    {
        static void Main()
        {
            var myClass = new MyClass();
            // By default, all partial method is private, we cannot use it outside of its class
            // myClass.MyMethod();

            Console.ReadKey();
        }
    }
}

Happy coding!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s