c sharp

C# Partial Class

As the keyword “partial” suggests that it must be related to the splitting of class implementation. The partial classes work similarly to the normal classes, but the definition of these classes can be divided into parts within the same code (in Ubuntu editor) or in different files while using the Visual Studio. The use of partial class doesn’t affect the execution. On the other hand, it is very useful when working on a large-scale code project. Thus, we have decided to show the use of partial classes in C# within this article today. Let’s get started with implementing some C# examples to use partial class in our programs. For this, we need a C# file to create and execute our codes. Thus, we have opened the terminal of Ubuntu 20.04 and tried the “touch” query to make one with the “cs” extension.

Example # 01

We will be looking at the simplest example of using the concept of C# partial class in our code. So, we launched the empty C# file in the text editor. We have to start our C# code with the System library of C# via the “using” keyword. It will help us use the generic functions of C# in our code. We have created a partial class “Test” of public type with the keywords “class” and “partial” and split its implementation into two simple parts.

The first part of the partial Test class contains a declaration of two private type character variables, i.e., “a” and “b”. Also, it contains a class Test constructor having two parameter values of character type “a” and “b” passed by the main() function to set or initialize the value of “a” and “b” using the “this” object of C#. The first part of the partial class Test is completed here. The other part of the partial class Test is implementing a single user-defined function named “Show”. This function contains a single statement of “WriteLine” from the “Console” class of C# used to display the value of character variables “a” and “b” passed by the main() method.

Now, the implementation of the partial class second part is also completed here, and there are no parts left in the code for this class. We have created another “New” class in the code that will be the main execution class for all the other classes. We have defined a Main() function within this class to start the program’s execution in the shell of Ubuntu 20.04 after compilation. We have created an object “t” of the partial class “Test” using the class name and the keyword “new” as far as the method of object creation is concerned. We have passed two-character values to the constructor of the partial class Test here. After creating this object, the constructor function Test() of the first part of the partial class will be executed while execution of the code. It will initialize the values of variables “a” and “b” by passing them the values “R” and “M,” respectively. We have used the object “t” of a partial class Test to call the function Show() from the second part of a partial class Test. The Show() function of the partial class will be executed, and it will show the values assigned to variables “a” and “b” displayed on our Ubuntu 20.04 shell screen.

After saving our code with the Ctrl+S, we opened the terminal to compile it. Use the “mcs” compiler to compile the “partial.cs” code file. It has generated the “exe” file after the compilation, and we have used that “exe” file within the “mono” runtime command of C# for execution. It turns out that the use of partial classwork is quite the same as the simple merged class, i.e., displayed the values of a variable “a” and “b”.

Example # 02

Let’s get into a more enhanced example of C# to create and use partial class within the code. So, we have started this code with the same “System” library at the first line. We have created a public type partial class Calculate and split its definition into three separate parts in the code. The first part of the partial class “Calculate” contains the simple declaration of two double-type variables, v1 and v2, at the start. After this, it contains a simple Calculate() constructor function having two double type parametric values passed by the Main() function to assign values to the variables “v1” and “v2”. After the constructor function, the first part of the partial class also contains a user-defined function named “Show”. This function simply displays the values of double variables v1 and v2 assigned by the constructor function via the value passed by the parameter method at the shell on execution. The second part of the partial class “Calculate” contains a simple user-defined “Sum” function. This function calculates the sum of double variables v1 and v2 within the new variable “sum” that are defined and initialized in the first part of the partial class.

After this, the WriteLine() statement of the Console class has been used here to display the sum of both variables at the shell using the “sum” variable just calculated by v1 and v2. The last and third part of the partial class “Calculate” contains another function, “Subtract,” that has been calculating the subtraction result of both double variables v1 and v2 using the “-“ operator. The subtraction result would be saved to the new double variable “sub” and displayed at the shell on execution using the WriteLine() statement of the Console class.

The partial class implementation in parts has been completed now. After this, we have created a new class named “New” and started a Main() function implementation within it. We have created an object “t” for the partial class Calculate and passed it 2 double variables in the parameters. On creating this object, the constructor function got executed, and both double values were assigned to the variables v1 and v2.

The “show” function has been called with the use of object “t” to display the values of both variables. After this, the same object is used to call the function “Sum” from the second part of a partial class to display the sum of both variables on the shell screen. At last, the Subtract function from the third part of the partial class has been called to display the subtraction result of both variable values. Let’s save and run this code.

After the compilation and execution, the compiler merged the partial class and displayed the values of variable v1, v2, sum, and subtraction results of both variables.

Conclusion

We have discussed the simple concept of partial classes to split the code into parts to avoid complexity in our projects. We have discussed two quite simple yet brief examples of C# to do so. We discussed how a big class could be split into parts while each contains different functions and variables. These examples have demonstrated that partial class can increase the program’s efficiency and understandability of a developer.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.