Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-13
9
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 336 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:Navneet
Navneet earned 664 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 332 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Assisted Solution

by:BlnGuy
BlnGuy earned 332 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:Navneet
Navneet earned 664 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:Navneet
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 336 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

661 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