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 :
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
- “Start” then ”All programs”, Select “ Microsoft Visual Studio”
- “File” then ”New” , Select Project (or) Ctrl+Shift+N
- After Clicking the Following Windows appears.
Fig: 2
Step 2
- Right click on solution Explorer then“Add”, select “class”
- It will show the Window given below in Fig 3.
- Create a class named as Myclass (or choose the name of your wish)
- It will show a window as given below in Fig 4
Fig: 3
Fig: 4
Step 4
- Create a partial class using the partial Keyword.
- Create two different methods in the same class name.
- Create a Method, then call the class named as testclass, and implement the methods.
Fig: 5
Step 5 - Finding the Error
- The same method name declared in the partial class shows an error message.
- It will show in the screenshot as shown below.
Fig: 6
Coding
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Threading.Tasks;
-
- namespace Partial.Classes
- {
- public partial class Myclass
- {
- public int Add (int x,int y)
- {
- return (x + y);
- }
-
- }
- Public partial class Myclass
- {
- Public int Sub (int x, int y)
- {
- return (x - y);
- }
- }
- public class Testclass
- {
- public void Test()
- {
- Myclass Omyclass = new Myclass();
- Omyclass.Add(10, 20);
- Omyclass.Sub(25, 5);
- }
- }
- }
Summary
In this article, I discussed the partial class in C#. I have written this article focusing on beginners and students.