Search This Blog

Partial Classes In C#

We discussed about classes in our earlier posts and I am assuming that you know fairly about classes by now. Now we will be discussing a different type of class that is, partial class. It's name reveals a bit about it which says partial i.e not complete. Let's see what it is and why partial class is useful?

What Is A Partial Class?

In C# we have various keywords that signify some unique .Net features. One such feature is to split a class into two or more physical files which will be automatically combined into one single file after compilation.

To do this we have partial keyword. It is used to declare a partial class as shown below

//First part of partial class
public partial class School
{
public void CountStudents()
{
}
}
//Second part of partial class
public partial class School
{
public void CountTeachers()
{
}
}
//It will be compiled as one class as below
public class School
{
public void CountStudents()
{
}
public void CountTeachers()
{
}
}

Why We Need Partial Class?

There are times when splitting up the files help and partial classes become desirable.

When we have large projects then splitting up the files help multiple programmers to code simultaneously over different physical files.

You can segregate different sections of a class if the file is big enough and has multiple functionalities.

When you work with windows forms, visual studio generates two separate files for you. One is NewForm.designer.cs and other is plain NewForm.cs file. You can notice a partial keyword written there.

How To Use Partial Class?

To make a class partial all you need to have is partial keyword, as shown in above example where partial keyword is written before class keyword and that's it. You can make as many partial classes as you want but there are few per-requisites:

Partial keyword is to be used in each part of partial class.

All parts of the partial class should be inside the same namespace.

Physical file name can be different but class name should be same for all the partial classes that we want to be compiled as one class.

Comments

Post a Comment

Popular posts from this blog

Great android apps require great design. For great design you need to master layouts. Layout is one of the basic building block of Android User Interface. Lets jump right in and try designing layout for our app - Just Maths.

Just Maths has 3 screens - Main Menu Screen, Game Screen and Score Screen. Each of the screen is designed using Relative Layout and Linear Layout. There are other layouts as well like Frame Layout, Table Layout and Grid Layout. But you can design anything using only Relative Layout and Linear Layout. These layouts are nothing but View Groups.

Want to build Android Apps really fast?
With Android Studio and a blend of Java and XML, you will be up and running and coding Great Android Apps in no time.
Let's get started. We will be creating a very simple app named Just Maths that will ask user a Maths question. User either presses (✔) or (X) to answer the question. This is going to be a project based learning wherein you will have something concrete in the end.

Here's is a quick way of implementing Auto-Complete in your webpage. Auto-Complete dropdowns are some textbox type looking elements on webpage. When you type on these textboxes you will be prompted with value options just like in dropdowns. The good part of using this method is you need not to using any jQuery for this. The bad part is this will only work in modern browsers which support HTML5.