体验Visual Studio 2005之局部类型

2016-02-19 17:07 10 1 收藏

生活已是百般艰难,为何不努力一点。下面图老师就给大家分享体验Visual Studio 2005之局部类型,希望可以让热爱学习的朋友们体会到设计的小小的乐趣。

【 tulaoshi.com - 编程语言 】

  Visual Studio 2005 [Whidbey] 抢先体验版 [Express Beta 1 ] 出来有一段时间了,并且在微软的官方网站上有免费的下载(下载地址:http://lab.msdn.microsoft.com/vs2005/)。就本人而言是非常喜欢c#这一新生的语言的。也许并不能说它是新生的,它是对以往各种语言的提炼,或许它是站在巨人的肩膀上的,所以才显得如此的优秀。伴随体验版而来的c# 2.0 给我们带来了新的语言特性(generics:泛型; iterators:迭代; partial classes:局部类型; anonymous methods:匿名方法;),使我们能更容易的编写出简洁明快的代码,当然这些新特性给我们带来的远不止简洁明快的代码。这只有在我们使用的过程中自己体会和别人的交流中了解。

  分别用2003和2005 新建两个WindowsApplication1。

  2003和2005解决方案资源管理器中都会默认建立一个从System.Windows.Forms.Form 类继承的窗体类Form1。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  那我们比较下两个不同的IDE环境为我们自动生成的Form1的代码是怎么样的。

  选中Form1.cs察看代码

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  2003:

  

public class Form1 : System.Windows.Forms.Form{ private System.Windows.Forms.Button button1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() {  //  // Windows 窗体设计器支持所必需的  //  InitializeComponent();  //  // TODO: 在 InitializeComponent 调用后添加任何构造函数代码  // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) {  if( disposing )  {   if (components != null)   {    components.Dispose();   }  }  base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </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> /// 应用程序的主入口点。 /// </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) { }}

  察看两个环境下Form1的代码文件 Form1.cs文件里对Form1的代码差别很大,2005中只有那么一点点,对button1的定义没有,Click事件委托也没有只有一个button1_Click()显然是有问题的。如果而且我们很快发现Class Form1是被定义成 partial 的也就是C# 2.0种的新的语言特征 局部类型。然后我们再点一下2005 IDE 解决方案资源管理器上的Show All Files按钮,会发现Form1.cs下多了个文件 Form1.Designer.cs 这是2003环境下是没有的, 察看该文件我们会发现对Class Form1的另一部份定义。

  

partial class Form1{ /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources 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 method for Designer support - do not modify /// the contents of this method 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;}

  现在好像2005对Form1的描述好像全了,2005中Form1.cs 和 Form1.Designer.cs 两个文件中对Class Form1的描述相加就是 2003 Form1.cs 中对Class Form1的描述。由此看来 partial 类型可以使我们把对某个类的描述写在不同地方,甚至写到两个或多个不同的文件中去。partial 信息只对编译器有用,编译器在编译时看到对某个类的描述是碎的(partial 的),它会去其他地方收集该类的其他碎片,然后把所有的该类的碎片组合成完整的一个类,再对其编译。所以partial 体现不到编译好的 IL中去的。至于partial类型给我们带来怎么样的意义呢?我们以后再讨论。

来源:https://www.tulaoshi.com/n/20160219/1614256.html

延伸阅读
标签: Web开发
这节我们学习三个控件,他们分别是:HiddenField,Literal,LinkButton 我们这里的演示用LinkButton ,因为这个控件在用法上和BUTTON 差不多,就长相有点希奇,也许是控件们性开放,HyperLink和Button瞎整,结果就有了LinkButton ,可你别说,他虽然这样名不正言不顺的出来了,可他的贡献是很不错的,所以,得表扬! 我们是首先学...
标签: Web开发
这节我们讲广告控件 AdRotator 他需要一个XML文件来存放广告信息 这个XML文件节点参数说明: ImageUrl 图片地址 NavigateUrl 连接地址,广告导航去的地方 AlternateText 当鼠标放图片上显示的提示文字 Keyword 该条广告的关键词 Impressions 权重,就是说显示的频率权重 那实际我们还可以给他添加...
VS2005 beta版已经发布,遗憾的是下载非常困难,采用的是网络安装模式,要先下载一个安装程序,选择要安装的项目后,开始一点一点下载安装,10M的网速也要大约8个小时(主要是ms服务器慢,单线程),幸运的是有人找到了它的每个模块的下载地址,用FLASHGET多线程下载太爽了,大家一起分享吧! 听说Visual Web Developer 2005 非常牛,是DM和AS...

经验教程

364

收藏

99
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部