Special Class Of C# Series - Part One - Partial Class

Introduction

This article will help you understand what partial class is and what class is used in C# language. Also, I will explain the various uses of the partial class, benefits of using the partial class, and the appropriate place to use the same, with an example.

What is a partial class in C#?

In C#, everything is considered as a separate physical file which has the extension of .cs [CSharp]. This language of C# also provides you with the ability which can have a single class implemented in multiple or different physical (.cs) files.

How is that possible to have a single class in multiple files? Yes, that is achievable using the "partial" modifier keyword in the language of the code. This partial keyword modifier can also be applied to any of the class, method, interface, and structure.

Benefits of Class

 

  • The modularity of the code can be extensively increased.
  • Quality of code maintainability is increased.
  • Isolation of the larger part of the code is achieved.
  • A single big class, method, or interface can be broken down into multiple partial files based on the module of its functionality or the grouping on some work.
  • Easy to debug and edit the code in future.
  • It helps to cover the code during unit test execution.

 

There are many other benefits once you start using the partial class

Where to use the partial class?

This is up to the developer who makes code. Even if you don’t use the partial class, nothing will stop you from the execution of the code.

When you implement the partial class in your application, the above said benefits will be achieved and also, the other developers can easily understand your code.

Now, I answer your question, i.e., when you use the partial keyword in the application.

 

  • When you find the code in a file keeps growing
  • When you want to have a set of logic grouped into a file and separate logics in another file.
  • When you need to segregate the module of task activities in separate files which belongs to the same class.

 

How this partial class looks

A single class which can be subdivided into multiple classes.

C#

Yes, you are right! We are just making a complex object into multiple smaller parts.

Example for Partial class

C#

Original Code of Program

  1. using System;  
  2. using static System.Console;  
  3.   
  4. namespace Sample  
  5. {  
  6.     /// <summary>  
  7.     /// The partial Class 1  
  8.     /// </summary>  
  9.     partial class StoreManagement  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int orderNum, quantity;  
  14.             double total;  
  15.             const double PRICE_EACH = 3.99;  
  16.             GetData(out orderNum, out quantity);  
  17.             total = quantity * PRICE_EACH;  
  18.             WriteLine("Order #{0}. Quantity ordered = {1}", orderNum, quantity);  
  19.             WriteLine("Total is {0}", total.ToString("C"));  
  20.             SayThanks();  
  21.             Console.WriteLine("\n\nEnter to exit!");  
  22.             Console.ReadLine();  
  23.         }  
  24.   
  25.     }  
  26.   
  27.     /// <summary>  
  28.     /// The partial Class 2  
  29.     /// </summary>  
  30.     partial class StoreManagement  
  31.     {  
  32.         public static void GetData(out int order, out int amount)  
  33.         {  
  34.             String s1, s2;  
  35.             Write("Enter order number ");  
  36.             s1 = ReadLine();  
  37.             Write("Enter quantity ");  
  38.             s2 = ReadLine();  
  39.             order = Convert.ToInt32(s1);  
  40.             amount = Convert.ToInt32(s2);  
  41.         }  
  42.   
  43.     }  
  44.   
  45.     /// <summary>  
  46.     /// The partial Class 3  
  47.     /// </summary>  
  48.     partial class StoreManagement  
  49.     {  
  50.         public static void SayThanks()  
  51.         {  
  52.             Console.WriteLine("Thank you!");  
  53.         }  
  54.   
  55.     }  
  56. }  

Output

C#

Partial Class code of Program

C#

Partial Class 1

  1. /// <summary>  
  2.     /// The partial Class 1  
  3.     /// </summary>  
  4.     partial class StoreManagement  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             int orderNum, quantity;  
  9.             double total;  
  10.             const double PRICE_EACH = 3.99;  
  11.             GetData(out orderNum, out quantity);  
  12.             total = quantity * PRICE_EACH;  
  13.             WriteLine("Order #{0}. Quantity ordered = {1}", orderNum, quantity);  
  14.             WriteLine("Total is {0}", total.ToString("C"));  
  15.             SayThanks();  
  16.             Console.WriteLine("\n\nEnter to exit!");  
  17.             Console.ReadLine();  
  18.         }  
  19.   
  20.     }  

Partial Class 2

C#

  1. /// <summary>  
  2.     /// The partial Class 2  
  3.     /// </summary>  
  4.     partial class StoreManagement  
  5.     {  
  6.         public static void GetData(out int order, out int amount)  
  7.         {  
  8.             String s1, s2;  
  9.             Write("Enter order number ");  
  10.             s1 = ReadLine();  
  11.             Write("Enter quantity ");  
  12.             s2 = ReadLine();  
  13.             order = Convert.ToInt32(s1);  
  14.             amount = Convert.ToInt32(s2);  
  15.         }  
  16.   
  17.     }  

Partial Class 3

C#

  1. /// <summary>  
  2.   /// The partial Class 3  
  3.   /// </summary>  
  4.   partial class StoreManagement  
  5.   {  
  6.       public static void SayThanks()  
  7.       {  
  8.           Console.WriteLine("Thank you!");  
  9.       }  
  10.   
  11.   }  

Partial Classes Program Output

C#

Conclusion

There is literally no change in the output but the way the code is implemented is a big task here.

If you have any queries, please comment.

Happy Learning!