Partial class in C# Explained
A partial
class is useful to split the functionality of a particular class into
multiple class files and all these files will be combined into one single class
file when the application is compiled.
While working on large
scale projects, multiple developers want to work on the same class file at the same time. To solve
this problem, C# provides an ability to spread the functionality of a
particular class into multiple class files using partial keyword.
In C#, we can
use partial keyword to
split the definition of a particular class, structure, interface or
a method over two or more source files.
Following is the
example of splitting the definition of Person class into two class files, Person1.cs and Person2.cs.
First file (Person1.cs)
public partial class Person
{
private string Name;
private string
Location;
public Person(string name, string location)
{
Name =name;
Location = location;
}
}
From above sample code, we created a
partial class called Person in Person1.cs
class file using partial
keyword with required variables and constructor.
Second File (Person2.cs)
public partial class Person
{
public void
GetPersonDetails()
{
Console.WriteLine("Name: " + Name);
Console.WriteLine("Location: " + Location);
}
}
If you observed from the above code, we
created a partial class called Person in Person2.cs class file using partial
keyword with GetPersonDetails() method.
When we execute the
above code, the compiler will combine these two partial classes into one Person class as shown below.
public class Person
{
private string Name;
private string
Location;
public Person(string name, string location)
{
Name = name;
Location = location;
}
public void
GetPersonDetails()
{
Console.WriteLine("Name: " + Name);
Console.WriteLine("Location: " + Location);
}
}
This is how the compiler will combine
all the partial classes into single class while executing the application in C#.
Rules to Implement Partial Class
In C#, we need to follow certain rules
to implement a partial class in our applications.
·
To
split the functionality of class, structure, interface or a method over
multiple files we need to use partial
keyword and
all files must be available at compile time to form the final type.
- The
partial
modifier can only appear immediately before the keywords class, struct or interface. - All parts of partial type definitions
must be in the same namespace or
assembly.
- All parts of partial type definitions
must have the same accessibility, such as public, private, etc.
- If any partial part is declared as abstract, sealed or base, then the whole type is
considered as abstract or sealed or base based on the defined
type.
- In C#, different parts can have
different base types
but the final type will inherit all the base types.
- Nested partial types are allowed in
partial type definitions.
Partial Class Example in C#
The following code is an example of
defining partial classes using partial
keyword
in C# language.
using System;
namespace CsharpnaijaTutorial
{
public partial class Person
{
private string Name;
private string
Location;
public Person(string name, string location)
{
this.Name = name;
this.Location = location;
}
}
public partial class Person
{
public void
GetPersonDetails()
{
Console.WriteLine("Name: " + Name);
Console.WriteLine("Location: " + Location);
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person("Musa Sule", "Abuja");
person.GetPersonDetails();
Console.WriteLine("\nPress Enter Key to Exit..");
Console.ReadLine();
}
}
}
When you run the above
C# program, we will get the result as shown below.
Thank you
References
1.
Tutlane
Comments
Post a Comment