Whidbey The local type of the initial experience (partial type)

Source: Internet
Author: User
Tags anonymous definition bool modify new features visual studio
Whidbey The local type of the initial experience (partial type)
Visual Studio [Whidbey] has been preemptive [Express Beta 1] for some time, and has a free download on Microsoft's official website (download address: http://lab.msdn.microsoft.com/ vs2005/). As far as I am concerned, I like the new language of C # very much. Perhaps it is not to say that it is a new life, it is the refinement of the various languages of the past, perhaps it is standing on the shoulders of giants, so it seems so good. The experience version of C # 2.0 brings us new language features (generics: generics; Iterators: Iteration; Partial classes: local type; Anonymous methods: Anonymous methods, so that we can easily write simple and crisp code, of course, these new features bring us far more than simple and crisp code. This is only in the process of using our own experience and the exchange of other people understand.

Create a new two WindowsApplication1 with 2003 and 2005 respectively
A form class inherited from the System.Windows.Forms.Form class is created by default in 2003 and 2005 Solution Explorer Form1
So let's compare the Form1 code that is generated automatically for us by the two different IDE environments.
Select Form1.cs View Code
2003:
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.Button button1;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.button1 = new System.Windows.Forms.Button ();
This. SuspendLayout ();
//
Button1
//
This.button1.Location = new System.Drawing.Point (88, 72);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (72, 32);
This.button1.TabIndex = 0;
This.button1.Text = "Button1";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 273);
This. Controls.Add (This.button1);
This. Name = "Form1";
This. Text = "Form1";
This. ResumeLayout (FALSE);

}
#endregion

<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}



private void Button1_Click (object sender, System.EventArgs e)
{

}
}
2005:
Partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{

}
}
Look at the two environments Form1 code file Form1.cs file in the Form1 code is very different, 2005 only a little bit, the definition of Button1 No, Click event delegate There is no only one button1_click () Obviously there is a problem. If and we soon find that class Form1 is defined as partial, C # 2.0 new language feature local types. And then we'll do a little bit more. The Show All Files button on the Form1.cs IDE Solution Explorer will find that there are more files under the Form1.Designer.cs this is not in 2003 environment, see the file we will find the class Another part of Form1 's definition.

Partial class Form1
{
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.IContainer components = null;

<summary>
Clean up any being used.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing && (components!= null))
{
Components. Dispose ();
}
Base. Dispose (disposing);
}

#region Windows Form Designer generated code

<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This.button1 = new System.Windows.Forms.Button ();
This. SuspendLayout ();
//
Button1
//
This.button1.Location = new System.Drawing.Point (75, 49);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (96, 46);
This.button1.TabIndex = 0;
This.button1.Text = "Button1";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 273);
This. Controls.Add (This.button1);
This. Name = "Form1";
This. Text = "Form1";
This. ResumeLayout (FALSE);

}

#endregion

Private System.Windows.Forms.Button button1;
}
Now it seems as if the description of the 2005 pair of Form1 is complete, and the description of class Form1 in the 2005 Form1.cs and Form1.Designer.cs Two files is the description of the class 2003 in Form1.cs Form1. It seems that the partial type allows us to write a description of a class in different places and even write to two or more different files. Partial information is only useful to the compiler, the compiler sees at compile time that the description of a class is "broken" (partial), it will collect other fragments of the class elsewhere, and then assemble all the fragments of that class into a complete class and compile it. So partial is not reflected in the compiled IL. What kind of meaning does the partial type bring to us? We'll talk about it later.

#结束
qq:14754875
Email:tiy@citiz.net
Bbs:www.shixm.com/bbs




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.