1.扩展方法 (Extension Methods)

给Person类扩展Print方法

Public Module PersonExtension
<System.Runtime.CompilerServices.Extension()> _
Public Sub Print(ByVal aPerson As Person)
If aPerson IsNot Nothing Then
Console.WriteLine("Istance of Person : " & aPerson.ToString())
Else
Console.WriteLine("No instance of Person.")
End If
End Sub
End Module
Public Class Person
Private m_Name As String
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public Overrides Function ToString() As String
ToString = Name
End Function
End Class
Public Class Student : Inherits Person
Private m_ClassGroup As String
Public Property ClassGroup() As String
Get
ClassGroup = m_ClassGroup
End Get
Set(ByVal value As String)
m_ClassGroup = value
End Set
End Property
Public Overrides Function ToString() As String
ToString = Name & " (" & ClassGroup & ")"
End Function
End Class
Public Class Example1
Public Shared Sub Main()
Dim person1 As Person = New Person With {.Name = "John"}
Dim person2 As Person = New Student _
With {.Name = "Jane", _
.ClassGroup = "Visual Basic .NET"}
Dim person3 As Person
'
person1.Print() ' (1)
person2.Print()
person3.Print()
'
Console.ReadLine()
End Sub
End Class

Output

 Istance of Person : John
Istance of Person : Jane (Visual Basic .NET)
No instance of Person.

对接口进行扩展

Public Interface SomeInterface
Sub SomeFirstMethod()
End Interface
Public Module SomeInterfaceExtension
<System.Runtime.CompilerServices.Extension()> _
Public Sub SomeSecondMethod(ByVal aSomeInterface As SomeInterface)
Console.WriteLine("SomeInterface.SomeSecondMethod() implementation.")
End Sub
End Module
Public Class SomeClass : Implements SomeInterface
Public Sub SomeFirstMethod() Implements SomeInterface.SomeFirstMethod
Console.WriteLine("SomeClass.SomeFirstMethod() implementation.")
End Sub
End Class
Public Class Example3
Public Shared Sub Main()
Dim object1 As New SomeClass
object1.SomeFirstMethod()
object1.SomeSecondMethod()
'
Console.ReadLine()
End Sub
End Class

一次扩展到哦个对象

Public Interface Interface1
End Interface
Public Interface Interface2
End Interface
Public Module InterfaceExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Sub Method1(ByVal aInterface1 As Interface1)
Console.WriteLine("Interface1.Method1")
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Sub Method2(ByVal aInterface2 As Interface2)
Console.WriteLine("Interface2.Method2")
End Sub
End Module
Public Class Class1 : Implements Interface1, Interface2
End Class
Public Class Example4
Public Shared Sub Main()
Dim object1 As New Class1
object1.Method1()
object1.Method2()
'
Console.ReadLine()
End Sub
End Class

2.对象初始化器(Object Initializers)

用New进行初始化

Class Person
Private m_Name As String
Public Property Name() As String
Get
Name = m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Address As Address
Public Property Address() As Address
Get
Address = m_Address
End Get
Set(ByVal value As Address)
m_Address = value
End Set
End Property
End Class
Class Address
Private m_Street As String
Public Property Street() As String
Get
Street = m_Street
End Get
Set(ByVal value As String)
m_Street = value
End Set
End Property
Private m_City As String
Public Property City() As String
Get
City = m_City
End Get
Set(ByVal value As String)
m_City = value
End Set
End Property
Private m_ZipCode As String
Public Property ZipCode() As String
Get
ZipCode = m_ZipCode
End Get
Set(ByVal value As String)
m_ZipCode = value
End Set
End Property
End Class
Class Counter
Public Sub New(ByVal value As Integer)
m_Value = value
End Sub
Private m_Value As Integer
Public ReadOnly Property Value() As Integer
Get
Value = m_Value
End Get
End Property
Private m_StepValue As Integer
Public Property StepValue() As Integer
Get
StepValue = m_StepValue
End Get
Set(ByVal value As Integer)
m_StepValue = value
End Set
End Property
Public Sub Raise()
m_Value += StepValue
End Sub
End Class
Class Example
Public Shared Sub Main()
Dim person1 As Person = New Person() With {.Name = "John"}
Console.WriteLine(person1.Name)
'
Dim person2 As Person = New Person() ' (1)
With person2 ' (1)
.Name = "Paul" ' (1)
End With ' (1)
'
Dim person3 As Person = New Person() _
With {.Name = "Jane", _
.Address = New Address() _
With {.City = "New York"}} ' (2)
Console.WriteLine(person3.Name)
Console.WriteLine(person3.Address.City)
'
Dim counter1 As Counter = New Counter() With {.StepValue = } ' (3)
Console.WriteLine(counter1.Value)
counter1.Raise()
Console.WriteLine(counter1.Value)
'
Console.ReadLine()
End Sub
End Class

3.匿名类型(Anonymous Types)

用With关键字进行类属性的定义和赋值

Option Infer On
Option Strict On
Class Example1
Public Shared Sub Main()
Dim address1 = New With {.City = "New York", .Street = "Parklane"}
Console.WriteLine(address1.City)
Console.WriteLine(address1.Street)
'
Dim address2 = New With {.City = "London", .Street = "Oxford Street"}
Console.WriteLine(address2.City)
Console.WriteLine(address2.Street)
'
Dim address3 = New With {.City = "San Fransico", .Street = }
Console.WriteLine(address3.City)
Console.WriteLine(address3.Street)
'
Dim address4 = New With {.City = "Paris"}
Console.WriteLine(address4.City)
'
Dim city As String = "Brussels"
Dim address5 = New With {city} ' (1)
Console.WriteLine(address5.City)
address5.City = "Amsterdam"
Console.WriteLine(address5.City)
'
Dim someInstance As New SomeFirstClass
Dim address6 = New With {someInstance.City, someInstance.Street()}
Console.WriteLine(address6.City)
Console.WriteLine(address6.Street)
'
Dim address7 = New With {someInstance.City, someInstance.Street(), _
someInstance.Number} ' (2)
Console.WriteLine(address7.City)
Console.WriteLine(address7.Street)
Console.WriteLine(address7.Number)
'
Dim address8 = New With {SomeSecondClass.City, SomeSecondClass.Street, _
SomeSecondClass.Number} ' (3)
Console.WriteLine(address8.City)
Console.WriteLine(address8.Street)
Console.WriteLine(address8.Number)
'
Console.WriteLine(address1.GetType().Equals(address2.GetType()))
Console.WriteLine(address1.GetType().Equals(address3.GetType()))
Console.WriteLine(address1.GetType().Equals(address4.GetType()))
Console.WriteLine(address4.GetType().Equals(address5.GetType()))
Console.WriteLine(address1.GetType().Equals(address6.GetType()))
'
Console.ReadLine()
End Sub
End Class
Class SomeFirstClass
Public Number As Integer
Public ReadOnly Property City() As String
Get
City = "someCity"
End Get
End Property
Public Function Street() As String
Street = "someStreet"
End Function
End Class
Class SomeSecondClass
Public Shared Number As Integer
Public Shared ReadOnly Property City() As String
Get
City = "someCity"
End Get
End Property
Public Shared Function Street() As String
Street = "someStreet"
End Function
End Class

4.局部方法 (Partial Methods)

'定义局部方法 需要用 Partial 做修饰符
'局部方法不一定总是有执行内容的,也就是说定义的方法 可以一句操作语句都没有
'局部方法不可以有返回值
'局部方法可以是静态(Shared)方法
'局部方法可以包含参数,参数可以包含以下修饰词:ByRef,Optional
'局部方法必须是私有(Private)方法

Class SomeClass
Partial Private Sub somePartialMethod1()
End Sub
Partial Private Sub somePartialMethod2()
End Sub
'
Private Sub somePartialMethod1() ' (1)
Console.WriteLine("somePartialMethod1")
End Sub
'
Public Sub TestSomePartialMethod1()
somePartialMethod1()
End Sub
Public Sub TestSomePartialMethod2()
somePartialMethod2()
End Sub
End Class
Partial Class SomeClass
Private Sub somePartialMethod2() ' (2)
Console.WriteLine("somePartialMethod2")
End Sub
End Class
Class Example1
Public Shared Sub Main()
Dim someObject As New SomeClass
someObject.TestSomePartialMethod1()
someObject.TestSomePartialMethod2()
'
Console.ReadLine()
End Sub
End Class

5.Lambda表达式

Delegate实例可以指向静态方法

Option Infer On
Option Strict On
Public Class Example1
Public Delegate Function SomeDelegate(ByVal value As Integer) As Integer
Public Shared Sub Main()
Dim value1 As Integer
'
Dim delegate1 As SomeDelegate = _
New SomeDelegate(AddressOf SomeFunction) ' (1)
value1 = delegate1.Invoke()
Console.WriteLine(value1)
'
Dim delegate2 As SomeDelegate = AddressOf SomeFunction ' (2)
value1 = delegate2.Invoke()
Console.WriteLine(value1)
'
Dim delegate3 = New SomeDelegate(AddressOf SomeFunction) ' (3)
value1 = delegate3.Invoke()
Console.WriteLine(value1)
'
Dim delegate4 As Func(Of Integer, Integer) = _
New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (4)
value1 = delegate4.Invoke()
Console.WriteLine(value1)
'
Dim delegate5 As Func(Of Integer, Integer) = _
AddressOf SomeFunction ' (5)
value1 = delegate5.Invoke()
Console.WriteLine(value1)
'
Dim delegate6 = _
New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (6)
value1 = delegate6.Invoke()
Console.WriteLine(value1)
'
Console.ReadLine()
End Sub
Public Shared Function SomeFunction(ByVal value As Integer) As Integer
SomeFunction = value *
End Function
End Class

通过Lambda定义完整的方法

Public Class Example2
Public Shared Sub Main()
Dim value1 As Integer
'
Dim delegate1 As Example1.SomeDelegate = _
Function(value As Integer) value * ' (1)
value1 = delegate1.Invoke()
Console.WriteLine(value1)
'
Dim delegate2 As Func(Of Integer, Integer) = _
Function(value As Integer) value * ' (2)
value1 = delegate2.Invoke()
Console.WriteLine(value1)
'
Dim delegate3 = Function(value As Integer) value * ' (3)
value1 = delegate3.Invoke()
Console.WriteLine(value1)
'
value1 = (Function(value As Integer) value * ).Invoke()
Console.WriteLine(value1)
'
Console.WriteLine((Function(value As Integer) value * ).Invoke())
'
Console.ReadLine()
End Sub
End Class

对Lambd表达式中嵌入Lambd表达式

Public Class Example4
Public Shared Sub Main()
Dim delegate1 = Function(arg1 As Integer) _
Function(arg2 As Integer) arg1 + arg2
Dim value1 As Integer = delegate1.Invoke().Invoke()
Console.WriteLine(value1)
'
Console.ReadLine()
End Sub
End Class

6.语言集成查询 (Lambda or Inline Functions)

以下为LinQ to Object的例子。

从persons查找"New York",并按Name排序

Option Infer On
Option Strict On
Public Class Person
Private m_Name As String
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_City As String
Public Property City() As String
Get
Return m_City
End Get
Set(ByVal value As String)
m_City = value
End Set
End Property
Private m_IsMale As Boolean
Public Property IsMale() As Boolean
Get
Return m_IsMale
End Get
Set(ByVal value As Boolean)
m_IsMale = value
End Set
End Property
Public Overrides Function ToString() As String
ToString = "Female "
If IsMale Then ToString = "Male "
ToString &= Name & ", from " & City & "."
End Function
End Class
Public Class Example1
Public Shared Sub Main()
Dim persons As Person() = New Person() { _
New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
'
Dim personsNY As IEnumerable(Of Person) = _
persons.Where(Function(person) person.City = "New York") _
.OrderBy(Function(person) person.Name) _
.Select(Function(person) person)
'
For Each person As Person In personsNY
Console.WriteLine(person)
Next
'
Console.ReadLine()
End Sub
End Class

利用LinQ表达式

Public Class Example2
Public Shared Sub Main()
Dim persons As Person() = New Person() { _
New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
'
Dim personsNY As IEnumerable(Of Person) = _
From person In persons _
Where person.City = "New York" _
Order By person.Name _
Select person
'
For Each person As Person In personsNY
Console.WriteLine(person)
Next
'
Console.ReadLine()
End Sub
End Class

VB2008新特性的更多相关文章

  1. SQL Server 2014 新特性——内存数据库

    SQL Server 2014 新特性——内存数据库 目录 SQL Server 2014 新特性——内存数据库 简介: 设计目的和原因: 专业名词 In-Memory OLTP不同之处 内存优化表 ...

  2. ElasticSearch 5学习(10)——结构化查询(包括新特性)

    之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...

  3. [干货来袭]C#6.0新特性

    微软昨天发布了新的VS 2015 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下也是昨天发布的新的C#6.0的部分新特性吧.. ...

  4. CSS3新特性应用之结构与布局

    一.自适应内部元素 利用width的新特性min-content实现 width新特性值介绍: fill-available,自动填充盒子模型中剩余的宽度,包含margin.padding.borde ...

  5. 【译】Meteor 新手教程:在排行榜上添加新特性

    原文:http://danneu.com/posts/6-meteor-tutorial-for-fellow-noobs-adding-features-to-the-leaderboard-dem ...

  6. 跨平台的 .NET 运行环境 Mono 3.2 新特性

    Mono 3.2 发布了,对 Mono 3.0 和 2.10 版本的支持不再继续,而且这两个分支也不再提供 bug 修复更新. Mono 3.2 主要新特性: LLVM 更新到 3.2 版本,带来更多 ...

  7. Atitit opencv版本新特性attilax总结

    Atitit opencv版本新特性attilax总结 1.1. :OpenCV 3.0 发布,史上功能最全,速度最快的版1 1.2. 应用领域2 1.3. OPENCV2.4.3改进 2.4.2就有 ...

  8. es6 新特性2

    es6其他几个非常有用的新特性. import export 这两个家伙对应的就是es6自己的module功能. 我们之前写的Javascript一直都没有模块化的体系,无法将一个庞大的js工程拆分成 ...

  9. ES6 新特性

    ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...

随机推荐

  1. JVM调优总结(一):基本概念

    一.数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型. 基本类型的变量保存原始值,即:他代表的值就是数值本身: 而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对 ...

  2. Eclipse下进行SVN提交时报“svn: 过期”错误的解决办法

    http://www.thinksaas.cn/group/topic/105323/ ———————————————————————————————————————————————————————— ...

  3. 微信小程序——加入购物车弹层

    对于网上商城,加入购物车是一个必备功能了.俺今天就来说下在微信小程序里如何造一个购物车弹层. 先上图: 主要用到的微信API:wx.createAnimation(OBJECT) 说下思路: 1.wx ...

  4. 记录一下寄几个儿的greendao数据库升级,可以说是非常菜鸡了嗯

    之前使用的greendao数据库存储服务器所有的历史推送消息,但是后来消息需要加几个新的字段 举个栗子,比如要新增红色框住的字段到数据库中: 本仙女作为一只思想成熟的菜鸡,当然是加了字段就赶紧重新往里 ...

  5. What Great .NET Developers Ought To Know (More .NET Interview Questions)

    A while back, I posted a list of ASP.NET Interview Questions. Conventional wisdom was split, with ab ...

  6. 9、Qt 事件处理机制

    原文地址:http://mobile.51cto.com/symbian-272812.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生 ...

  7. 【C#】自定义容器控件,设置界面控件,支持设计器拖入控件

    先上效果图: 1.先重写设置界面的控件功能: public partial class SetterControl : UserControl { public SetterControl() { I ...

  8. hostapd作为radius服务器

    使用hostapd作为radius服务器,用于企业wifi加密认证. 参考链接: http://www.cnblogs.com/claruarius/p/5902141.html 去网上下载hosta ...

  9. Android N: jack server failed

    在服务器上编译Android N.出现如下错误. Android N 编译时会使用到 jack server,同一台服务器上,各个用户都需要为 jack server 指定不同的端口,否则会产生端口冲 ...

  10. Mybatis系列(二):优化MyBatis配置文件中的配置和解决字段名与实体类属性名不相同的冲突

    原文链接:http://www.cnblogs.com/xdp-gacl/p/4264301.html     http://www.cnblogs.com/xdp-gacl/p/4264425.ht ...