C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: If you remove the partial modifier, you will get an error at compile time.
Error text: The namespace "<global namespace>" already contains a definition for "A".
NamespaceTip: To fix this, you can either use the partial keyword, or change one of the class names.
C# program that uses partial class
class Program
{
static void Main()
{
A.A1();
A.A2();
}
}
Contents of file A1.cs: C#
using System;
partial class A
{
public static void A1()
{
Console.WriteLine("A1");
}
}
Contents of file A2.cs: C#
using System;
partial class A
{
public static void A2()
{
Console.WriteLine("A2");
}
}
Output
A1
A2
And: You will find that the class A is present. Class A will contain the methods A1 and A2 in the same code block.
Thus: Partial classes are precisely equivalent to a single class with all the members.
Compiled result of A1.cs and A2.cs: C#
internal class A
{
// Methods
public static void A1()
{
Console.WriteLine("A1");
}
public static void A2()
{
Console.WriteLine("A2");
}
}