Solved

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

Posted on 2011-09-13
9
229 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

23 Experts available now in Live!

Get 1:1 Help Now