Mary
Mary

Reputation: 161

Creating partial class in C#

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

Upvotes: 6

Views: 7395

Answers (4)

Ramki
Ramki

Reputation: 11

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/

Upvotes: 1

aarona
aarona

Reputation: 37303

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.

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190941

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.

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

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.

Upvotes: 2

Related Questions