Partial Class, Interface or Struct in C Sharp with example

Partial Class, Interface or Struct in C Sharp with example

30 Mar 2024
Intermediate
119K Views
6 min read
Learn via Video Course & by Doing Hands-on Labs

C# Programming For Beginners Free Course

Partial Class, Interface or Struct: An Overview

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. Moreover, the other parts of the class, structure, or interface should be defined in the same namespace or assembly. All the parts must have the partial keyword and the same access modifier such as public, private, and so on.

 For more about partial method refer to the article Partial Methods in C Sharp with examples. However, In this C# Tutorial, we will explore more about Partial Class, Interface, or Struct which will include a partial class in c# with an example, the need for a partial class, the requirement of a partial class in c#, when the partial class is used in C#.

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

  1. 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

Q1. Can we have partial interface in C#?

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. 

Q2. What are the partial classes in C#?

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

Q3. Can you partially implement an interface?

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

Take our free csharp skill challenge to evaluate your skill

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

GET CHALLENGE

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

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this