VB2008新特性
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新特性的更多相关文章
- SQL Server 2014 新特性——内存数据库
SQL Server 2014 新特性——内存数据库 目录 SQL Server 2014 新特性——内存数据库 简介: 设计目的和原因: 专业名词 In-Memory OLTP不同之处 内存优化表 ...
- ElasticSearch 5学习(10)——结构化查询(包括新特性)
之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...
- [干货来袭]C#6.0新特性
微软昨天发布了新的VS 2015 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下也是昨天发布的新的C#6.0的部分新特性吧.. ...
- CSS3新特性应用之结构与布局
一.自适应内部元素 利用width的新特性min-content实现 width新特性值介绍: fill-available,自动填充盒子模型中剩余的宽度,包含margin.padding.borde ...
- 【译】Meteor 新手教程:在排行榜上添加新特性
原文:http://danneu.com/posts/6-meteor-tutorial-for-fellow-noobs-adding-features-to-the-leaderboard-dem ...
- 跨平台的 .NET 运行环境 Mono 3.2 新特性
Mono 3.2 发布了,对 Mono 3.0 和 2.10 版本的支持不再继续,而且这两个分支也不再提供 bug 修复更新. Mono 3.2 主要新特性: LLVM 更新到 3.2 版本,带来更多 ...
- Atitit opencv版本新特性attilax总结
Atitit opencv版本新特性attilax总结 1.1. :OpenCV 3.0 发布,史上功能最全,速度最快的版1 1.2. 应用领域2 1.3. OPENCV2.4.3改进 2.4.2就有 ...
- es6 新特性2
es6其他几个非常有用的新特性. import export 这两个家伙对应的就是es6自己的module功能. 我们之前写的Javascript一直都没有模块化的体系,无法将一个庞大的js工程拆分成 ...
- ES6 新特性
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...
随机推荐
- Urllib3 库详解
文档:http://urllib3.readthedocs.io/en/latest/
- Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)
概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...
- js关于弹也遮罩层
1:什么是遮罩层 遮罩层:我是弹也一个(遮罩层)还有一个(内容层),下面上图片看一效果 我们看到一个灰蒙蒙的遮盖(其实也是一个层)还有一个层(也就是我们展示的内容). 2: 弹出层效果居中分析 在这 ...
- 用css3实现社交分享按钮
以前实现按钮一般都是用图片来实现的,特别是一些拥有质感的按钮,今天练习了一些相关方面的的例子,用css3来实现Social Media Buttons html代码如下 <div class=& ...
- [hadoop读书笔记] 第四章 Hadoop I/O操作
P92 压缩 P102 序列化 序列化:将结构化对象转为字节流便于在网上传输或写到磁盘进行永久性存储的过程 用于进程之间的通信或者数据的永久存储 反序列化:将字节流转为结构化对象的逆过程 Hadoop ...
- this小案例
public class Son extends Parent { public String name="jack"; public void init(){ super.ini ...
- selenium+java+chrome环境搭建
我只能说因为版本冲突,简直太折腾了,而搜了无数个博友的帖子才找到正确条案,就不能好好的写篇文章吗? 最近真的是太闲太闲了,平时没事总得搞点技术,不然心里感觉好空虚, 最近看上了selenium,所以试 ...
- applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found
applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found 解决办法: 1 ...
- 捋一捋Spring Web的源码思路
Servlet前提 Java规定了Servlet Container为每一个web app创建一个Servlet Context:而Servlet Context中又包含了诸多Servlet -- 其 ...
- e615. Finding the Next Focusable Component
public Component findNextFocus() { // Find focus owner Component c = KeyboardFocusManager.getCurrent ...