Creating partial class in C#

可紊 提交于 2019-12-10 04:34:07

问题


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

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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/



来源:https://stackoverflow.com/questions/5007756/creating-partial-class-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!