Introduction
Let's start with the interesting astronomical theory that some astrophysicists believe that there are multiverses. This theory is even backed by math as well.
Basically, a multiverse is a group of universes, just like our universe is just part of a multiverse group. Multiverse & Astronomy are pretty interesting topics in themselves. But today we are going to learn a similar concept in C#.
In C# there is the concept of partial classes. Let's say we have one class named Earth. In the multiple universe theory, every universe will have their own earth. That means the earth is present at different spaces at the same time.
The same way in C#, one class can be presented at different locations, but still can be treated as one single class by the compiler. Such classes are known as partial classes.
Basically, I can have one class in multiple files and it will get compiled at the same time.
Note
We first need to understand that the file name and class name are 2 different things. The file name can be different from the class it contains & one file can have multiple classes or interfaces, enums for that matter.
Let's try to have 1 class in 3 different files. See the image for reference:
The Logical View

The Actual View

First, create new a class named PlanetEarth. File name & class name will be the same.
File name : PlanetEarth.cs : defind Earth's properties.
- namespace LeranCSharp.Entities
- {
- public partial class PlanetEarth
- {
- public string Volume { get; set; }
- public string Mass { get; set; }
- public string Age { get; set; }
- }
- }
- This class has 3 properties. which defines Earth.
- It is decorated by Partial keyword, which tells the compiler, that this is just one part of PlanetEarth, there may be other classes with same name exist.
Same Namespace
I want you to bring close attention to the namespace. There are 2 folders: 1. BusinessLogic, 2. Entities.
Let's create a new class in BusinessLogic's folder,
File Name: PrintEarthProperties.cs : prints Earth's properties.

- Even though this newly created class was partial, still compiler was unable to find it relative partial class hence this class was unable to access properties that we created in Entities PlanetEarth's class.
- There is one rule, All partial class must have the same namespace.
- Let's change the namespace to namespace to LeranCSharp.Entities & try again. we don't need to move the class out of the folder just replace the namespace.
- namespace LeranCSharp.Entities
- {
- partial class PlanetEarth
- {
- public void DisplayProperties()
- {
- System.Console.WriteLine("Volume: "+ Volume + " Mass: "+ Mass + " Age: "+ Age);
- }
- }
- }
- Now the compiler can allow this class is to access properties such as Volume, Mass & Age. Even if they are not actually present in this file. That's the answer to our mystery. Because it is a partial class, it can access the properties and methods of the same partial classes from different files.
- More than 2 Partial classes

Can we have more than 2 instances of one partial class? Of course we can.



Bohdan StupakPosted May 20, 2021, 8:18 AM
I've enjoyed the article and particularly the multiverse analogy :) You've done a great job explaining this concept! Still, maybe it's just my experience or I tend to think that in general partial classes is a sign of violated single responsibility principle. Even in the example of partial class you've provided nothing suggests that partial classes should be used. You may employ clean architecture advocated by Bob Martin and StudentBusiness logic may be a different class somewhere in domain while POCO will reside on the infrastructure level. It will even enable having different set of properties for Student on both layers if needed. Anyways, this is still a nicely written article! Congratulations!