今天时间有点早,所以上来在写一篇文章吧,继续上一篇的文章,在我们平时编程过程中有没有遇到过这样的一个情景,你定义的类只是用来封装一些相关的数据,但并不需要相关联的方法、事件和其他自定义的功能。同时,这个类仅仅在当前的应用程序中使用,而不需要在项目间重用。你所需要的只是一个“临时的”类型,现在我们来看看这个传统类的定义:
class="code_img_closed" src="/Upload/Images/2014072012/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('942a398e-53ff-4a1e-92ad-1e1865ae2ea5',event)" src="/Upload/Images/2014072012/2B1B950FA3DF188F.gif" alt="" /> 1 internal class oneClass从上面代码构建上看,这个类虽然说不上有多难,但是如果这个类有很多数据成员的话,那么还是要消耗相当时间的。为了解决这个问题微软给我们提出来一个新的概念叫匿名类型。
匿名类型:匿名类型提供了一种方便的方法,可用来将一组只读属性封装到单个对象中,而无需首先显式定义一个类型。 类型名由编译器生成,并且不能在源代码级使用。 每个属性的类型由编译器推断。
以下示例显示了用两个名为 Amount 和 Message 的属性进行初始化的匿名类型。
1 var v = new { Amount = 108, Message = "Hello" };从该例子可以看出来这个匿名类型看起来比传统类型要简单明了。
输出结果为:
QQaGIGAZpnQCAJoYlZaEITmpgkBAiwwXFyCzv3pR/2GSWB7TiDKnd/1+fh6OjoDPvofPXbvz0I/Jf/+t8bOJ2j7hRLHf0BsNVRtibK1sQ4mgWOZoHbD4lx+xd4gwvNgwstWozWoUUqLdqn0KxdbNYukAzuzwAJbz/6MbgDOP0xCAeiwWBTYKlj8DKbcBpVGExVlKmKNigjBPXKSL0iwlBEGIpwnRyjVoYjDdVIQzXSULUkVCWerxLNV4jmK0Tz5cL5cuF8mSBYKgiW9gVL+oIlfcF7fYG7vYG7vYHiHowifuAOP3C723+7y/91l//rLv9Xnf4vO31fdvi+aPfdavfdavfdbPPeaPPeaPVea/Fea5m71jJ3rXmusHnuarPnarPnc57nc57nCs9zhev5jOv+jOu+zHFf5rg/ZbsvsV2X2K6LLIyCJoz8Rmd+o/NCo/MC03mB6TxP0OCA/L2e5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kWEhJSLur+N096jCs9xhX+yAq0Lf7IShB5FhISUm7qwaMnfaPxXmO81xjvgQGXMR6MI89CQkLKST149KTPFIf0jmLMI89CQkLKTT149ERgSghMiT4c5FlISEi5qwePnggtCaEZQ2CKC0zIs5CQkHJVDx49EVtWRZZVkWUVt61EKIE8CwkJKSe18+iJxLoqtiQgIktCaEGehYSElKva+faJxLoqta5KrasS66rYuiq2roZXHyHPQkJCykXtfPtEZluV2ValtlUJBHkWEhJSzmrn2+/lY0n5WFI2lpSNJaW2ValtNYI8CwkJKTe18+33ivGkHALNy5ZEnoWEhJSj2v32e9XEmnJiTTGehMjHk9Ek8iwkJKSc1O6336sm11STa6qJpBK3rWjyEZqLBgkJKRe1+9336sk1iGoiqZxIKjHPQnP+ISEh5Z52v/u+f2q9f2pdM7WumVxTT6ypJpKx5CM0tzISElIu6uF33w/OrA9Mr/dPrfdPraknk+qJZCz56LfoPyyQkJByUA+/+35wen1wen1gen1ger1/ck0zmYytPUL/yYqEhJSLevjd99qZDe3MunZmfXB6bXB6bWAqubD26DfHzvzSp4aEhISUoYfffT9k3xi2bwzbN4Zm1rUza4PTyYW1R/+CPAsJCSkH9fC774dnU8OzKd1sanh2fci+NjSztrj26F+OfvhLnxoSEhJShr757nv9bErvSBkcKf3shm52fdi+trT+6H8iz0JCQso9/X9XYJ6b5QEljgAAAABJRU5ErkJggg==" alt="" align="left" />
匿名类型也可以嵌套使用,代码如下:
1 var Aemployee = new该类在这里就简单的说一下,该类在lambda表达式中用的比较多,在分享lambda表达式时在看会理解的更透彻一些。
使用它的限制条件:
1:匿名类型不支持事件、自定义方法和自定义重写
2:匿名类型是隐式封闭的(sealed)
3:匿名类型的实例创建只使用默认构造函数
4:匿名类型没有提供可供控制的类名称(使用var 定义的)
匿名方法:普通方法定义方式,因为方法的存在是为了 复用 一段代码,所以一般会给方法取个名字,这个方法的引用就可以通过 "方法名"调用。但是有的方法,不需要复用,仅仅是使用一次就够了,所以不需要方法名,这种方法就叫做匿名方法。匿名方法必须结合 委托使用。(潜在的意思就是:尽管没有方法名了,但方法的指针还是存放在了某个委托对象中)
C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方式。 expressions.">不过,本主题中有关匿名方法的信息同样也适用于 Lambda 表达式。 functionality not found in lambda expressions.">有一种情况下,匿名方法提供了 Lambda 表达式中所没有的功能。 您可使用匿名方法来忽略参数列表。 can be converted to delegates with a variety of signatures.">这意味着匿名方法可转换为具有各种签名的委托。 这对于 Lambda 表达式来说是不可能的。
看到这些估计大家都云里雾里吧,咱们直接晒代码来更深的理解一下:
首先我们写一个传统方式的一个简单例子:
1 public class NNFF比如下面的Sayhello方法只有在Ceshi方法体内调用,不再别的地方使用时该方法在外面单独写一个方法时给人感觉有点繁琐。下载我们再来看看匿名方法的写法:
1 public class NNFF从上面可以看出把输出内容的方法写到方法Ceshi里了,这样对于该方法是属于受保护的外界是没有办法调用的。
在下两次课分享lambda表达式时在举个更具体的例子,并对比一下,其实lambda表达式在某种程度上就是匿名方法。匿名方法的简写!
注意:
1.在编译后,会为每个匿名方法创建一个 私有的静态方法,然后将此静态方法传给 委托对象使用。
2.匿名方法:编译后会生成委托对象,生成方法,然后把方法装入委托对象,最后赋值给 声明的委托变量。
3.匿名方法可以省略参数:编译的时候 会自动为这个方法 按照 委托签名的参数 添加参数