`
tiandirensoon
  • 浏览: 596576 次
文章分类
社区版块
存档分类
最新评论

C# 2.0 的partial

 
阅读更多
partial 关键字的作用是将你的 class 分为多个部分,编译器会将多个部分拼到一起去。

public partial class SampleClass
...{
public void MethodA()
...{
}
}

public partial class SampleClass
...{
public void MethodB()
...{
}
}



public class SampleClass
...{
public void MethodA()
...{
}
public void MethodB()
...{
}
}


是等价的。

我猜想这个东西出现的初衷是为了解决掉:“窗体设计器生成的代码”这个令人讨厌的 region。

对我们来说,在团队开发当中这个东西或许也会有点用处。

我观察了一下生成的 IL 代码,使用 partial 生成的代码并没有什么特殊的标记,这说明 partial 纯粹是语言的特性,CLR 完全不知道这么个玩意的存在,这也就意味着不要指望将 partial class 编译为 assembly 或者 module 什么的再与其他的人写的 partial class 去进行拼接:它只能在编译的时候起作用。

为了考验一下编译器,我试着让某一个 partial class 不显式指定父类,发现代码仍然能够正确的通过编译,编译器会按照某一个显式指定了父类的 partial 进行编译,只有在多个 partial 指定了不同父类时才会报错。同样,如果多个 partial 指定了自相矛盾的修饰符的话,编译时也会报错的。

有意思的是,我们还可以写以下这样的代码:

public partial class Sample
...{
public partial class SampleSon
...{
public partial class SampleGrandson
...{
}
}
}
public partial class Sample
...{
public partial class SampleSon
...{
public partial class SampleGrandson
...{
}
}
}
分享到:
评论

相关推荐

    ASP.NET 2.0中的partial

    ASP.NET 2.0中的partial 1. 什么是局部类型? C# 2.0 引入了局部类型的概念。局部类型允许我们将一个类、结构或接口分成几个部分,分别实现在几个不同的.cs文件中。

    C#中partial关键字的作用

    C# 2.0 引入了局部类型的概念。局部类型允许我们将一个类、结构或接口分成几个部分,分别实现在几个不同的.cs文件中。 局部类型适用于以下情况: (1) 类型特别大,不宜放在一个文件中实现。 (2) 一个类型中的一部分...

    详解C# partial 关键字的使用

    C# 2.0 引入了局部类型的概念。局部类型允许我们将一个类、结构或接口分成几个部分,分别实现在几个不同的.cs文件中。 局部类型适用于以下情况: (1) 类型特别大,不宜放在一个文件中实现。 (2) 一个类型中的一部分...

    C# partial关键字说明

    C# 中可以将类、结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可。 1. 什么是局部类型? C# 2.0 引入了局部类型的概念。局部类型允许我们将一个类、结构或接口分成几个部分,分别实现...

    Essential C# 4.0, 3rd Edition Mar 2010

    Methods and parameters–including extension methods, partial meth­ods, and C# 4.0’s optional and named parameters Generics, concurrent collections, and custom collections with iterators Delegates,...

    C#4.0本质论(第3版)

    Methods and parameters–including extension methods, partial meth­ods, and C# 4.0’s optional and named parameters Generics, concurrent collections, and custom collections with iterators Delegates, ...

    The_CSharp_Programming_Language_Third_Edition

    LINQ, in turn, builds heavily on some of the features that were introduced in C# 2.0, including generics, iterators, and partial types. Another change in the third edition is that the specification ...

    Essential C# 4.0 (第三版)

    Topics intended for beginners and advanced readers are clearly marked, and the book includes indexes of C# versions (2.0, 3.0, and 4.0), which make it easy for readers to reference topics specific to...

    c#是什么呢?初学者看看吧

    泛型无疑是C#2.0最重大的改进,它的出现赋予了C#代码更强的类型安全,更好的复用,更高的效率和更清晰的约束。  2、 匿名方法:匿名方法允许我们将代码直接与委托实例相关联,使委托实例化工作更加直观和方便。在我...

    C#中分部类和分部方法的应用

    分部类(Partial Class)在C#2.0引入,分部方法(Partial Method)在C#3.0引入,这两个语法特性都具有相同的特性:将类(或)方法的代码分散在多个地方。 1.分部类的特性和应用 1.1分部类的定义 在定义一个类时,...

    ASP.NET 2.0 跟我一起学Visual.Studio2005 2/9

    C# 2.0语法除泛型之外,还新增了例如:分部类、匿名方法、迭代器、可空类型等一系列的新功能,本课程将会介绍近十几个C# 2.0语言和编译器的新增功能,并以实例加以说明。 跟我一起学Visual Studio 2005(4):VS 2005...

    ASP.NET 2.0 跟我一起学Visual.Studio2005 1/9

    C# 2.0语法除泛型之外,还新增了例如:分部类、匿名方法、迭代器、可空类型等一系列的新功能,本课程将会介绍近十几个C# 2.0语言和编译器的新增功能,并以实例加以说明。 跟我一起学Visual Studio 2005(4):VS 2005...

    How.to.Become.a.Csharp.Programmer.B00QJ2TNB0.epub

    The C# 2005 version (also referred to as C# 2.0) added even more powerful features to the language such as generics, partial classes, and much more. The new features of C# 3.0 that were released in ...

    一个采用AJAX (ASP.NET 2.0)实现子窗体关闭父窗体异步刷新程序例子

    Partial update of parent page via AJAX (ASP.NET 2.0) on close of a child window - conditionally

    C#语法篇(上)

    随着DOT NET Framework 2.0的发布,C#也从1.1版升级到2.0版。增加了不少新的有用的特性,如Partial、泛型等。本课程将会实例讲解这些语法的用法和特性。

    C#语法篇(下)

    随着DOT NET Framework 2.0的发布,C#也从1.1版升级到2.0版。增加了不少新的有用的特性,如Partial、泛型等。本课程将会实例讲解这些语法的用法和特性。

    C#读取JPEG图片的Exif信息

    [Serializable] public class EXIF { #region -- Class level members -- // Class level members. private Image _picture; #endregion #region -- Constructors -- // Constructors. ...

    c#图书管理系统 数据库SQL2000

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "2.0.0.0")] [Serializable()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Component...

    C#实现窗体与子线程的交互的方法

    本文实例简述了C#实现窗体与子线程间通讯的方法,对于C#初学者有一定的借鉴价值。具体方法如下: 一般来说窗体上的UI在默认情况下不允许使用子线程(或者其它非创建控件的UI线程)去控制(这在...public partial class

    ASP.NET 2.0,C#—-图像特效处理

    利用.NET 提供的类,如...public partial class WebForm4 : System.Web.UI.Page { // 原始图片路径 private string path; private System.Drawing.Bitmap bitmap; private System.Drawing.Graphics graphics; st

Global site tag (gtag.js) - Google Analytics