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. 浏览器自己主动填表安全漏洞:查看浏览器保存的password

    我通常会使用浏览器保存自己的帐号和password,下次登录就无需又一次输入,很方便.而像傲游这种浏览器还提供了自己主动同步功能,让我一个傲游帐号.就带着互联网上全部帐号password去旅行. 昨天 ...

  2. webpack5--css 打包

    1.在 src 文件夹下面新建 css 文件夹,创建 common.css body{ background-color: #f2f2f2; } a{ color: #188eee; text-dec ...

  3. 利用R里的options函数进行光标和数字位数设置

    用R写代码时,打字水平不高,有时候不知道乱按了一些键(现在我还不知道哪个键),光标就变成了加粗的竖直线,又改不回去.这种情况下我们可以用options函数进行光标设置,例如:options(promp ...

  4. 安卓程序代写 网上程序代写[原]自定义View

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...

  5. USB学习笔记连载(七):CY7C68013A 无法识别的可能原因

    最近一直在调试视频 采集卡,和PC端连接的是USB接口,使用的是cypress的CY7C68013A-56PVXC. //======================================= ...

  6. python爬虫数据-下载图片经典案例

    '''Urllib 模块提供了读取web页面数据的接口,我们可以像读取本地文件一样读取www和ftp上的数据.首先,我们定义了一个getHtml()函数: urllib.urlopen()方法用于打开 ...

  7. 用OpenGL绘制平滑着色的三角形与相交区域的混合着色

    一.三角形的绘制 在OpenGL中,面是由多边形构成的.三角形可能是最简单的多边形,它有三条边.可以使用GL_TRIANGLES模式通过把三个顶点连接到一起而绘出三角形. 使用GL_TRIANGLE_ ...

  8. 多媒体开发之ftp---一个很现实的需求把ftp转换成rtmp协议做点播

    http://www.dy2018.com/i/96131.html# http://www.hdpfans.com/thread-15684-1-1.html ftp://xc:xc@dz.dl12 ...

  9. R 语言 相关入门资料

    <R语言基础语法入门>: http://www.xueqing.tv/upload/april-training/day1/index.html#1

  10. js 创建多行字符串

    function heredoc(fn) { ,-).join('\n') + '\n' } var tmpl = heredoc(function(){/* !!! 5 html include h ...