Partial Classes in C# step by step

Introduction

Hello coders welcome to learn csharp step by step, a place where you can find all c# related videos, articles and other self learning materials. Apart from c-sharp we do cover other microsoft technologies like sharepoint, msbi, mcv, wcf, wpf, linq and many more for more information you can visit our parent site i.e. Questpond.

Questpond : An e-learning firm since 2002 and focused only on microsoft technologies, here you get complete one-to-one training or group trainings with a minimul training fees, before joining to our course you can watch our demo videos to understand our style of teaching. If you don't want training then also you can do self learning thru our DVDs and videos and e-books. For more info contact us on below mentioned details.

Warch our C# Video i.e. learn csharp in 100 hours to start with csharp programming.

Here in this tutorial session we will learn more about use of partial classes and methods in c-sharp step by step.

What are partial classes

In order to declare partial classes we just need to declare "partial" keyword before "class" keyword while declaring any class, an example is shown below.


partial class Calculation{
///Code Goes here
}

partial interface ICalculation{
///Code Goes here
}

partial struct StrCalculation{
///Code Goes here
}

Partial splits class code into multiple parts, using partial keyword we can split an interface, struct or class into one or more parts.

Use of partial keyword to split any class code into one or more files.

When partial code is compiled on CLR, multiple classes or interfaces or struct with partial keyword will be compiled into single unit or it is considered as single class, interface and struct.

For CLR it is a single class only but for us it will be two separated file.

The biggest advantage use of partial keyword is that you can separate UI design code and business logic code for ease of working and understanding.

Multiple developers can also work simultaneously like one creating logic and other working on designer part

It is also helpful to embed our custom logic code under framework auto generated code. This i'm taking about designer file aspx and linq DBML autogenerated file.

Larger classes can be split into smaller classes for easy to understand and maintain.

Interfaces can also split into multiple code to share it with multiple developers which further help in fast application development.

Sealed Property : If a partial class is sealed then the entire class will be sealed.

Abstract Property : If a partial class is abstract then the entire class will be considered as an abstract class.

Name of both partial classes should be same only.

One or more Partial classes with same name should be declared under one namespace scope only. Different files is fine but should be under same namespace.

Partial class using example

For this example we will create a console application and we will demonstrate partial example step by step.

Step 1

Create a simple console application and give a nice name to it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PartialClasses
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Step 2

Let's create a partial class "clsCalculation" and add methods to it. Here in this example we will create two partial classes "clsCalculation" and we will see how CLR is going combine both classes under one roof.

Partial Class 1

 partial class clsCalculation
    {
        public void Addition(int a, int b)
        {
            Console.WriteLine("Output is {0}", a + b);
        }

        public void Substraction(int a, int b)
        {
            Console.WriteLine("Output is {0}", a - b);
        }

    }

Partial Class 2

    partial class clsCalculation
    {
        public void Multiplication(int a, int b)
        {
            Console.WriteLine("Output is {0}", a * b);
        }

        public void Division(int a, int b)
        {
            Console.WriteLine("Output is {0}", a / b);
        }

    }
 

Step 3

To demonstrate or to check that CLR really combines both partial classes into a single unit or not. So for that what we will do that we will create an object of clsCalculation and call in a Main method of a console application and check wheather all methods are getting combined or not.

There you see in the above image that the moment we have created partial class of same name, CLR automatically detected and treated it as single unit.

 class Program
    {
        static void Main(string[] args)
        {
            clsCalculation objCal = new clsCalculation();
            objCal.Multiplication(5, 6);
        }
    }

So friends as you see this is best advantage of use of partial classes when working with multiple developers or working with huge team or working with an auto generated code. It also help us for ease of understanding, maintaining and handling of class code.

Partial class Auto Generated Code

We were discussing above for an auto generated code so let me tell what is an auto generated code, A code which generates dynamically when any changes or updates occur on VS2015 UI screen is known as auto-generated code or dynamic system code.

The best example to see it, let's create a simple web application and check how system has generated auto code.

Let's create a simple ASP.NET Web Application.

After successfully created web application, go to solution explorer and check default.aspx page. You will get two files, default.aspx.cs file and default.aspx.designer.cs file if you open that both files and see you will find one autogenerated partial class in designer.cs file and other partial class in default.cs file where we will do our custom coding.

Default.aspx.cs

Here we will do our custom coding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Default.aspx.designer.cs

//------------------------------------------------------------------------------
// auto-generated
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
///auto-generated
//------------------------------------------------------------------------------

namespace WebApplication1
{


    public partial class _Default
    {
    }
}

Interesting part is partial class _Default which is same in both files. So hey friends this all about partial classes hope you have understood this session if you have any doubts feel free to touch base with us in a below mentioned contact details.

+91-22-66752917
+91 9967590707
questpond@questpond.com / questpond@yahoo.com