Solved

c# Partial class? Why do I need it? Will I need it?

Posted on 2011-09-13
9
228 Views
Last Modified: 2012-05-12
Hi there;

Simple as it's..
In C# why and will I need Partial class?

Bring it on with your word of your own.

Thanks.

Kind regards.
0
Comment
Question by:jazzIIIlove
9 Comments
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 84 total points
ID: 36528253
It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable:

When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx
0
 
LVL 15

Assisted Solution

by:navneethegde
navneethegde earned 166 total points
ID: 36528608
Hi!

Good explanation.
http://www.devx.com/dotnet/Article/22603

Thanks!
0
 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 83 total points
ID: 36528780
In my understanding, the partial keyword does nothing but allow a class to be split between several source files. Is there any reason to do this other than for code organization? I've seen it used for that in generated UI classes.

It seems a poor reason to create a whole keyword. If a class is big enough to require multiple files, it probably is doing too much. I thought that perhaps you could use it to partially define a class for another programmer somewhere to complete, but it would be better to make an abstract class.
0
 

Assisted Solution

by:BlnGuy
BlnGuy earned 83 total points
ID: 36536103
Yes, it's true. The "partial" keyword does nothing else than allowing to split up the code of a single class into several files.

Sure, classes should be held as small as possible, but especially in UI classes splitting up the code into a part that just contains design and another part that contains functionality is very usefull to keep your code clean.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36541655
Ok, here is the question as the partial class are partioning the code into several cs files, the question is that how can compiler have the idea which partial class is the class to be followed by means of static members or even constructors?

I mean e.g., what if the files are in different projects or in seperate hierarchies?

Kind regards.
0
 
LVL 15

Assisted Solution

by:navneethegde
navneethegde earned 166 total points
ID: 36541852
Hi!

While compilation the compiler will build the same class name in the different file as single code unit.

using System;
public partial class PartialClass
{
     public PartialClass()
    {
        Console.WriteLine("PartialClass Constructor");
    }
    public void Display()
    {
        Console.WriteLine("PartialClass - Display()");
    }
}
public partial class PartialClass
{
    /*
    public PartialClass()
    {
    Console.WriteLine("PartialClass Constructor1");
    }
    */
    public PartialClass(string strName)
    {
        Console.WriteLine("Name: " + strName);
    }
    public void Print()
    {
        Console.WriteLine("PartialClass - Print()");
    }
}
class PartialCons
{
    public static void Main(string[] args)
    {
        PartialClass oPartialClass = new PartialClass();
        oPartialClass.Display();
        oPartialClass.Print();
        PartialClass oPartialClass1 = new PartialClass("Erode Senthilkumar");
    }
}

Here the example shows the same default signature cannot have more than once in the every class. Because it will became ambiguity method while compiler compile these files into the single unit. But we can load the constructor in the different signatures in the every partial class.

Thanks!

0
 
LVL 15

Expert Comment

by:navneethegde
ID: 36541857
Also to clarify

All the partial types meant to be the part of same type must be defined within a same assembly and module.

Thanks!
0
 
LVL 9

Assisted Solution

by:Orcbighter
Orcbighter earned 84 total points
ID: 36558711
In my last project I wrote a gui front-end to a database on a server.
The application was complex with complex structures. In one maintenance function, maintaining an account, the account details was a class made up of contact information, account information, and so on.
We used a tab control to split the various related parts of the account into discrete vieable pieces. One tab had the contact details, another tab had a table with a list of liniked accounts.
Partial classes were perfect for this type of layout.
The partial class could be used across all the tabs, with each table just detailing with its part of the class.
That is one example. There are many more.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36717450
Thanks guys.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now