C# में Partial Class क्या है? Rules, Advantages और Example के साथ

By Mandeep Rohit

Published On:

Date:

PARTIAL CLASS IN C#

अगर आप C# सीख रहे हो या .NET पर काम कर रहे हो, तो आपने partial class शब्द जरूर सुना होगा। यह C# का एक बहुत काम का feature है, खासकर तब जब किसी class का code बहुत बड़ा हो जाता है और उसे एक ही file में संभालना मुश्किल लगने लगता है।

इस Post में हम समझेंगे कि partial class क्या होती है, क्यों उपयोग की जाती है, इसके फायदे क्या हैं, और इसके जरूरी rules क्या हैं

Partial Class क्या होती है?

Partial class, C# की एक खास feature है। यह हमें यह सुविधा देती है कि हम एक ही class की functionality को कई अलग-अलग files में लिख सकें। जब application compile होती है, तो ये सभी files मिलकर एक ही class बन जाती हैं।

Partial class बनाने के लिए partial keyword का उपयोग किया जाता है।

इसी keyword का उपयोग करके आप किसी struct या interface को भी दो या उससे ज्यादा files में बाँट सकते हो।

हम partial class का उपयोग तब करते हैं जब किसी class का code बहुत ज्यादा बड़ा हो जाता है और उसे manage करना मुश्किल होने लगता है।

Partial Class की जरूरत क्यों पड़ती है?

मान लो आप एक बड़े project पर काम कर रहे हो। एक class में हजारों lines का code है। अब:

  • Code पढ़ना मुश्किल
  • Manage करना मुश्किल
  • Multiple developers का एक साथ काम करना मुश्किल

ऐसी स्थिति में partial class बहुत काम आती है।

Partial Class के फायदे (Advantages)

1) Multiple developers एक साथ काम कर सकते हैं

अगर project बड़ा है, तो कई developers एक ही class पर अलग-अलग files में एक साथ काम कर सकते हैं।

2) Large project में code management आसान हो जाता है

Class को अलग-अलग files में बाँट देने से code को समझना और manage करना आसान हो जाता है।

3) Visual Studio खुद Partial Class का उपयोग करता है

जब आप Visual Studio में कोई Web Form add करते हो, तो दो .cs files अपने आप बनती हैं:

  1. WebForm1.aspx.cs → इसमें developer का code होता है
  2. WebForm1.aspx.designer.cs → इसमें system द्वारा generate किया गया code होता है

जैसे कि web form पर जो controls आप drag and drop करते हो, उनकी declarations इसी file में होती हैं।

Partial Class का एक छोटा Example

File 1: Student_Part1.cs

public partial class Student
{
public void GetName()
{
Console.WriteLine("My name is Rahul");
}
}

File 2: Student_Part2.cs

public partial class Student
{
public void GetAge()
{
Console.WriteLine("I am 25 years old");
}
}

Compile होने के बाद यह एक ही class मानी जाएगी।

C# में Partial Class के Rules

जितनी भी files में class को बाँटा गया है, उन सभी में partial keyword होना जरूरी है।

सभी partial class definitions एक ही namespace और assembly में होनी चाहिए।

सभी parts की accessibility (जैसे public, private) एक जैसी होनी चाहिए।

अगर class का कोई एक part abstract declare किया गया है, तो पूरी class abstract मानी जाएगी।

अगर class का कोई एक part sealed declare किया गया है, तो पूरी class sealed मानी जाएगी।

Partial Class से जुड़े और Rules

अगर किसी एक part में class inherit की गई है, तो पूरी class उसी class को inherit मानी जाएगी।

ध्यान रहे: C# multiple class inheritance को support नहीं करता।

Partial class के अलग-अलग parts में अलग base class specify नहीं की जा सकती।

लेकिन अलग-अलग parts में अलग base interfaces specify किए जा सकते हैं।

Partial class के किसी भी part में declare किए गए members, बाकी सभी parts में available होते हैं।

इस तरह partial class का उपयोग बड़े projects में code को साफ-सुथरा, manageable और team-friendly बनाने के लिए किया जाता है।


Partial Class in C# (English Summary)

A partial class is a special feature of C#.
It 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.
A partial class is created by using a partial keyword.
The partial keyword can also be used to split a struct or an interface over two or more files.
We use partial class when code of a class contains so many lines to manage.

Advantages of Partial Class

Multiple developers can work simultaneously with a single class in separate files.
When working on large projects, spreading a class over separate files allows programmers to work on it simultaneously.
Visual Studio uses partial classes to separate, automatically generated system code from the developer’s code. For Example when you add a webform, two .CS files are generated.

  1. WebForm1.aspx.cs – Contains the developer code.
  2. WebForm1.aspx.designer.cs – Contains the system generated code. For example, declarations for the controls that you drag and drop on the webform

Rules For Partial Class

  • All the parts spread across different files, must use the partial keyword.
  • All the partial class definitions must be in the same assembly and namespace.
  • All the parts must have the same accessibility like public or private, etc.
  • If any part is declared abstract then the whole class is declared of the abstract type.
  • If any part is declared sealed then the whole class is declared of the sealed type.
  • If any of the parts inherit a class, then the entire type inherits the class.
  • C# does not support multiple class inheritance. Different parts of the partial class, must not specify different base classes.
  • Different parts of the partial class can specify different base interfaces.
  • Any member that are declared in a partial definition are available to all of the other parts of the partial class.

Leave a Comment