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. Extjs4.x MVC开发模式,效率提高的两大秘诀

    最近做MVC开发的,遇到一个蛋疼的问题,每次加载模块都需要耗时3~4秒钟,才可以显示出完整的页面,通过监控,发现主要还是在Controller里慢,加载js文件等都是非常快的,但一到controlle ...

  2. Redis管道传输

    Redis是一个TCP服务器,并支持请求/响应协议.redis的一个请求完成需要下面的步骤: 客户端发送一个查询到服务器,并从套接字中读取,通常在封闭的方式,对服务器的响应. 服务器处理命令并将响应返 ...

  3. 即时通信(IM)和实时通信(RTC)的区别

    即时通信(IM=nstant messaging)和实时通信(rtc=Real-time communication)都是一套网络通信系统,其本质都是对信息进行转发.其最大的不同点,是对信息传递的时间 ...

  4. C++ vector清空元素的三种方法

    #include <iostream> #include <vector> using namespace std; //STL vector的几种清空容器(删除)办法 voi ...

  5. XmlnsDefinition for a Cool Namespace Mapping

    In XAML, when you want to reference a CLR type, you have to add a namespace mapping that maps the XM ...

  6. 自然语言交流系统 phxnet团队 创新实训 个人博客 (九)

    情感倾向可认为是主体对某一客体主观存在的内心喜恶,内在评价的一种倾向.它由两个方面来衡量:一个情感倾向方向,一个是情感倾向度. 情感倾向方向也称为情感极性.在微博中,可以理解为用户对某客体表达自身观点 ...

  7. 针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)

    错误: 针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_A ...

  8. 嵌入式开发之zynq---Zynq PS侧sd驱动

    http://blog.chinaunix.net/uid-29404121-id-4217026.html http://blog.chinaunix.net/uid-29709984-id-430 ...

  9. Java调试那点事[转]

    转自云栖社区:https://yq.aliyun.com/articles/56?spm=5176.100239.blogcont59193.11.jOh3ZG# 摘要: 该文章来自于阿里巴巴技术协会 ...

  10. image 和 barplot 的组合

    代码示例: par(mfrow = c(1, 2)) par(mar = c(1, 10, 1, 0), las = 1, ann = F) image(1:10, 1:10, matrix(samp ...