Solved

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

Posted on 2011-09-13
9
232 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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