Partial Class In C#

Introduction

 
A partial class is a single class that can be split into two (or) more files. That's how multiple programmers can work on the same class using different files. Each part of a partial class should be in the same namespace. You need to use the partial keyword in each part of the partial class. I have written this article focusing on students and beginners.
 

Flow Chart :

Partial Class In C#
Fig: 1
 
Simple Keyword:
 
Partial class is a single class  that can be split into two or more files.

How we can create a project and partial class

 
Step 1
  1. “Start” then ”All programs”, Select “ Microsoft Visual Studio”
  2. “File” then ”New” , Select Project (or) Ctrl+Shift+N
  3. After Clicking the Following Windows appears.
Partial Class In C#
Fig: 2
 
Step 2
  1. Right click on solution Explorer then“Add”, select “class”
  2. It will show the Window given below in Fig 3.
  3. Create a class named as Myclass (or choose the name of your wish)
  4. It will show a window as given below in Fig 4
Partial Class In C#
Fig: 3
 
Partial Class In C#
Fig: 4
 
Step 4 
  1. Create a partial class using the partial Keyword.
  2. Create two different methods in the same class name.
  3. Create a Method, then call the class named as testclass, and implement the methods.
Partial Class In C#
Fig: 5
 
Step 5 - Finding the Error
  1. The same method name declared in the partial class shows an error message.
  2. It will show in the screenshot as shown below.
Partial Class In C#
Fig: 6
 
Coding
  1. Using System;  
  2. Using System.Collections.Generic;  
  3. Using System.Linq;  
  4. Using System.Text;  
  5. Using System.Threading.Tasks;  
  6.   
  7. namespace Partial.Classes  
  8. {  
  9.    public partial class Myclass  
  10.    {  
  11.         public int Add (int x,int y)  
  12.         {  
  13.             return (x + y);  
  14.         }  
  15.   
  16.    }  
  17.    Public partial class Myclass  
  18.    {  
  19.         Public int Sub (int x, int y)  
  20.         {  
  21.             return (x - y);  
  22.         }  
  23.    }  
  24.    public class Testclass  
  25.    {  
  26.         public void Test()  
  27.         {  
  28.             Myclass Omyclass = new Myclass();  
  29.             Omyclass.Add(10, 20);  
  30.             Omyclass.Sub(25, 5);  
  31.         }  
  32.    }  

Summary

 
 In this article, I discussed the partial class in C#. I have written this article focusing on beginners and students.


Similar Articles