Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Partial Class, Interface or Struct in C Sharp with example

Partial Class, Interface or Struct in C Sharp with example

07 Oct 2024
Intermediate
121K Views
6 min read
Learn with an interactive course and practical hands-on labs

Best Free C# Course: Learn C# In 21 Days

Partial Class, Interface or Struct: An Overview

C# Partial Classes, Interfaces, and Structs are important for developers aiming to create modular, maintainable, and efficient code. Imagine having the capability to split your classes across multiple files, define clear contracts with interfaces, or use lightweight data structures with structs, all while ensuring better code organization. These features in C# provide a flexible way to structure applications, enhancing readability and scalability without adding unnecessary complexity.

In this C# tutorial, I’ll explain Partial Classes, Interfaces, and Structs, how they streamline code organization, and why mastering them is essential for writing clean, scalable C# applications.

What is Partial Class?

We have different types of classes in C#, an abstract class, partial class, static class, sealed class, nested class & concrete class. We will focus on Partial class here
types of classes in C#
  • A partial class is a particular feature of C#
  • Partial Class provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. 
  • It is created by using a partial keyword. 
  • The Partial keyword is also useful to split the functionality of methods, interfaces, or structures into multiple files.

What is Interface?

  • It is created using the keyword ‘interface’ in C#
  • It is a contract and that’s why all the Methods, properties, Indexers, and Events which are a part of the interface must be implemented by the class or struct, which implements the interface. 
  • It can contain only the signature of Methods, Properties, Indexers, and Events.

What is Struct?

  • We can create a struct, using the keyword struct.
  • Aa struct is a value type and it is directly stored in the stack of the memory.

Note: 

We can declare a partial method inside the partial class or the partial struct. So, we can have partial interface, partial class & partial struct but the partial method can only be written inside Partial struct and Partial class but not in Partial interface. As shown in the below figure.

Partial Class, Interface or Struct

Why Partial Class, Interface, or Struct?

  1. Allow more than one developer to work simultaneously on the same class, structure, or interface.

  2. Partial classes are particularly helpful for customizing auto-generated code by the IDE. Whenever the IDE generates the code then the tool may define some partial class, interface, methods, and further customization of partial class, the interface is done by the developers without messing with the system-generated code.

Key Points about Partial Class, Interface, or Struct

  1. During code compilation, all the parts should be available to form the final class, interface, or struct.

  2. Any member declared in one part will be available to all other parts.

  3. If any part has Inheritance, then it applies to the entire class.

  4. Different parts of a class or struct may inherit from different interfaces.

  5. If any part is declared abstract, then the whole class, interface, or struct is considered abstract.

  6. If any part is declared sealed, then the whole class, interface, or struct is considered sealed.

Read More - C# Interview Questions And Answers

Auto-Generated Partial Class in Asp.Net by IDE

Whenever you add an Asp.Net web form or web page to your web application or website then Visual Studio automatically adds a partial class to your page code. This code behind the class is Inherited by the ASPX page. Let's elaborate on this in C# Compiler.

  public partial class Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 // your's code
 }
}

Partial Class, Interface, or Struct Example

  class MyNamespace
{
 partial class Example
 {
 void Test() { //write your code }
 }
 partial class Example
 {
 void Test2() { //write your code }
 }
}   
  partial interface IExample
{
 void ITest();
}
 partial interface IExample
{
 void ITest2();
}   
  partial struct SExample
{
 void STest() 
{ 
//write your code 
}
}
 partial struct SExample
{ 
void STest2() 
{ 
//write your code 
}
}   

Note: The partial modifier is not available for delegate or enum.
Conclusion:

I hope you will enjoy these tricks while programming C#. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, Consider our C# Programming Course for a better understanding of all C# concepts.

FAQs

Partial class, interface, and structure were introduced in C# 2.0. Now it is possible to split the definition of a class, interface, and structure over more than one source file. 

It can break the functionality of a single class into many files. 

Implementing only part of an interface and delegating the rest to an embedded interface of the same type is perfectly valid

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this