Creating partial class in C#

7.3k views Asked by At

I am a student and I dont know what a partial class is. The following code belongs to a partial class

I automatically created the partial class:

public partial class EGUI: Form
{     
    private OleDbConnection dbConn;   // Connectionn object
    private OleDbCommand dbCmd;       // Command object
    private OleDbDataReader dbReader; // Data Reader object
    private Emp Edetails;
    private string sConnection;
    private string sql;
}
4

There are 4 answers

1
aarona On BEST ANSWER

First of all, THIS: http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx

Second of all, it allows a way for a programmer to collect specific pieces of code into two different files. A really good example of this is when creating an ASP.NET WebForm. The WebForm will have a file for your event handlers and such (Button_Click etc) and then you will have an extra file that contains the declarations of the ASP.NET controls you are using on your page. This keeps the code you CARE about in one file and the more obvious 'auto-generated' stuff in another.

Correct me if I'm wrong, but I also believe partial classes may allow you to do something similar to 'monkey patching' because it exposes the class in such a way that you can add new methods, variables etc and have access to the classes private members.

0
Darin Dimitrov On

A partial class is a class whose definition could be split among different files inside the same project/assembly. For example Visual Studio Forms Designer uses this functionality extensively in order to split the design time controls that you have placed on the form from the actual code, and once the project is compiled the two source files are merged to emit the resulting class.

0
Daniel A. White On

Here is the definitive answer: http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx

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.

0
Ramki On

Check this out. There is a simple example for beginners on partial classes and partial methods.

http://w3mentor.com/learn/asp-dot-net-c-sharp/object-oriented-concepts-c-sharp/partial-class-and-partial-method-in-c/