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

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.
LVL 12
jazzIIIloveAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Minh Võ CôngConnect With a Mentor Commented:
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
 
NavneetConnect With a Mentor Commented:
Hi!

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

Thanks!
0
 
Ramkisan JagtapConnect With a Mentor Lead DeveloperCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
BlnGuyConnect With a Mentor Commented:
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
 
jazzIIIloveAuthor Commented:
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
 
NavneetConnect With a Mentor Commented:
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
 
NavneetCommented:
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
 
OrcbighterConnect With a Mentor Commented:
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
 
jazzIIIloveAuthor Commented:
Thanks guys.
0
All Courses

From novice to tech pro — start learning today.