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 Clas

  • 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

using System;  
using static System.Console;  
  
namespace Sample  
{  
    /// <summary>  
    /// The partial Class 1  
    /// </summary>  
    partial class StoreManagement  
    {  
        static void Main(string[] args)  
        {  
            int orderNum, quantity;  
            double total;  
            const double PRICE_EACH = 3.99;  
            GetData(out orderNum, out quantity);  
            total = quantity * PRICE_EACH;  
            WriteLine("Order #{0}. Quantity ordered = {1}", orderNum, quantity);  
            WriteLine("Total is {0}", total.ToString("C"));  
            SayThanks();  
            Console.WriteLine("\n\nEnter to exit!");  
            Console.ReadLine();  
        }  
  
    }  
  
    /// <summary>  
    /// The partial Class 2  
    /// </summary>  
    partial class StoreManagement  
    {  
        public static void GetData(out int order, out int amount)  
        {  
            String s1, s2;  
            Write("Enter order number ");  
            s1 = ReadLine();  
            Write("Enter quantity ");  
            s2 = ReadLine();  
            order = Convert.ToInt32(s1);  
            amount = Convert.ToInt32(s2);  
        }  
  
    }  
  
    /// <summary>  
    /// The partial Class 3  
    /// </summary>  
    partial class StoreManagement  
    {  
        public static void SayThanks()  
        {  
            Console.WriteLine("Thank you!");  
        }  
  
    }  
}  

Output

C#

 

Partial Class 1

/// <summary>  
    /// The partial Class 1  
    /// </summary>  
    partial class StoreManagement  
    {  
        static void Main(string[] args)  
        {  
            int orderNum, quantity;  
            double total;  
            const double PRICE_EACH = 3.99;  
            GetData(out orderNum, out quantity);  
            total = quantity * PRICE_EACH;  
            WriteLine("Order #{0}. Quantity ordered = {1}", orderNum, quantity);  
            WriteLine("Total is {0}", total.ToString("C"));  
            SayThanks();  
            Console.WriteLine("\n\nEnter to exit!");  
            Console.ReadLine();  
        }  
  
    }  

Partial Class 2

/// <summary>  
    /// The partial Class 2  
    /// </summary>  
    partial class StoreManagement  
    {  
        public static void GetData(out int order, out int amount)  
        {  
            String s1, s2;  
            Write("Enter order number ");  
            s1 = ReadLine();  
            Write("Enter quantity ");  
            s2 = ReadLine();  
            order = Convert.ToInt32(s1);  
            amount = Convert.ToInt32(s2);  
        }  
  
    }  

Partial Class 3

/// <summary>  
  /// The partial Class 3  
  /// </summary>  
  partial class StoreManagement  
  {  
      public static void SayThanks()  
      {  
          Console.WriteLine("Thank you!");  
      }  
  
  }  

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!


Similar Articles