1.扩展方法 (Extension Methods)

给Person类扩展Print方法

  1. Public Module PersonExtension
  2. <System.Runtime.CompilerServices.Extension()> _
  3. Public Sub Print(ByVal aPerson As Person)
  4. If aPerson IsNot Nothing Then
  5. Console.WriteLine("Istance of Person : " & aPerson.ToString())
  6. Else
  7. Console.WriteLine("No instance of Person.")
  8. End If
  9. End Sub
  10. End Module
  11. Public Class Person
  12. Private m_Name As String
  13. Public Property Name() As String
  14. Get
  15. Name = m_Name
  16. End Get
  17. Set(ByVal value As String)
  18. m_Name = value
  19. End Set
  20. End Property
  21. Public Overrides Function ToString() As String
  22. ToString = Name
  23. End Function
  24. End Class
  25. Public Class Student : Inherits Person
  26. Private m_ClassGroup As String
  27. Public Property ClassGroup() As String
  28. Get
  29. ClassGroup = m_ClassGroup
  30. End Get
  31. Set(ByVal value As String)
  32. m_ClassGroup = value
  33. End Set
  34. End Property
  35. Public Overrides Function ToString() As String
  36. ToString = Name & " (" & ClassGroup & ")"
  37. End Function
  38. End Class
  39. Public Class Example1
  40. Public Shared Sub Main()
  41. Dim person1 As Person = New Person With {.Name = "John"}
  42. Dim person2 As Person = New Student _
  43. With {.Name = "Jane", _
  44. .ClassGroup = "Visual Basic .NET"}
  45. Dim person3 As Person
  46. '
  47. person1.Print() ' (1)
  48. person2.Print()
  49. person3.Print()
  50. '
  51. Console.ReadLine()
  52. End Sub
  53. End Class

Output

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

对接口进行扩展

  1. Public Interface SomeInterface
  2. Sub SomeFirstMethod()
  3. End Interface
  4. Public Module SomeInterfaceExtension
  5. <System.Runtime.CompilerServices.Extension()> _
  6. Public Sub SomeSecondMethod(ByVal aSomeInterface As SomeInterface)
  7. Console.WriteLine("SomeInterface.SomeSecondMethod() implementation.")
  8. End Sub
  9. End Module
  10. Public Class SomeClass : Implements SomeInterface
  11. Public Sub SomeFirstMethod() Implements SomeInterface.SomeFirstMethod
  12. Console.WriteLine("SomeClass.SomeFirstMethod() implementation.")
  13. End Sub
  14. End Class
  15. Public Class Example3
  16. Public Shared Sub Main()
  17. Dim object1 As New SomeClass
  18. object1.SomeFirstMethod()
  19. object1.SomeSecondMethod()
  20. '
  21. Console.ReadLine()
  22. End Sub
  23. End Class

一次扩展到哦个对象

  1. Public Interface Interface1
  2. End Interface
  3. Public Interface Interface2
  4. End Interface
  5. Public Module InterfaceExtensions
  6. <System.Runtime.CompilerServices.Extension()> _
  7. Public Sub Method1(ByVal aInterface1 As Interface1)
  8. Console.WriteLine("Interface1.Method1")
  9. End Sub
  10. <System.Runtime.CompilerServices.Extension()> _
  11. Public Sub Method2(ByVal aInterface2 As Interface2)
  12. Console.WriteLine("Interface2.Method2")
  13. End Sub
  14. End Module
  15. Public Class Class1 : Implements Interface1, Interface2
  16. End Class
  17. Public Class Example4
  18. Public Shared Sub Main()
  19. Dim object1 As New Class1
  20. object1.Method1()
  21. object1.Method2()
  22. '
  23. Console.ReadLine()
  24. End Sub
  25. End Class

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

用New进行初始化

  1. Class Person
  2. Private m_Name As String
  3. Public Property Name() As String
  4. Get
  5. Name = m_Name
  6. End Get
  7. Set(ByVal value As String)
  8. m_Name = value
  9. End Set
  10. End Property
  11. Private m_Address As Address
  12. Public Property Address() As Address
  13. Get
  14. Address = m_Address
  15. End Get
  16. Set(ByVal value As Address)
  17. m_Address = value
  18. End Set
  19. End Property
  20. End Class
  21. Class Address
  22. Private m_Street As String
  23. Public Property Street() As String
  24. Get
  25. Street = m_Street
  26. End Get
  27. Set(ByVal value As String)
  28. m_Street = value
  29. End Set
  30. End Property
  31. Private m_City As String
  32. Public Property City() As String
  33. Get
  34. City = m_City
  35. End Get
  36. Set(ByVal value As String)
  37. m_City = value
  38. End Set
  39. End Property
  40. Private m_ZipCode As String
  41. Public Property ZipCode() As String
  42. Get
  43. ZipCode = m_ZipCode
  44. End Get
  45. Set(ByVal value As String)
  46. m_ZipCode = value
  47. End Set
  48. End Property
  49. End Class
  50. Class Counter
  51. Public Sub New(ByVal value As Integer)
  52. m_Value = value
  53. End Sub
  54. Private m_Value As Integer
  55. Public ReadOnly Property Value() As Integer
  56. Get
  57. Value = m_Value
  58. End Get
  59. End Property
  60. Private m_StepValue As Integer
  61. Public Property StepValue() As Integer
  62. Get
  63. StepValue = m_StepValue
  64. End Get
  65. Set(ByVal value As Integer)
  66. m_StepValue = value
  67. End Set
  68. End Property
  69. Public Sub Raise()
  70. m_Value += StepValue
  71. End Sub
  72. End Class
  73. Class Example
  74. Public Shared Sub Main()
  75. Dim person1 As Person = New Person() With {.Name = "John"}
  76. Console.WriteLine(person1.Name)
  77. '
  78. Dim person2 As Person = New Person() ' (1)
  79. With person2 ' (1)
  80. .Name = "Paul" ' (1)
  81. End With ' (1)
  82. '
  83. Dim person3 As Person = New Person() _
  84. With {.Name = "Jane", _
  85. .Address = New Address() _
  86. With {.City = "New York"}} ' (2)
  87. Console.WriteLine(person3.Name)
  88. Console.WriteLine(person3.Address.City)
  89. '
  90. Dim counter1 As Counter = New Counter() With {.StepValue = } ' (3)
  91. Console.WriteLine(counter1.Value)
  92. counter1.Raise()
  93. Console.WriteLine(counter1.Value)
  94. '
  95. Console.ReadLine()
  96. End Sub
  97. End Class

3.匿名类型(Anonymous Types)

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

  1. Option Infer On
  2. Option Strict On
  3. Class Example1
  4. Public Shared Sub Main()
  5. Dim address1 = New With {.City = "New York", .Street = "Parklane"}
  6. Console.WriteLine(address1.City)
  7. Console.WriteLine(address1.Street)
  8. '
  9. Dim address2 = New With {.City = "London", .Street = "Oxford Street"}
  10. Console.WriteLine(address2.City)
  11. Console.WriteLine(address2.Street)
  12. '
  13. Dim address3 = New With {.City = "San Fransico", .Street = }
  14. Console.WriteLine(address3.City)
  15. Console.WriteLine(address3.Street)
  16. '
  17. Dim address4 = New With {.City = "Paris"}
  18. Console.WriteLine(address4.City)
  19. '
  20. Dim city As String = "Brussels"
  21. Dim address5 = New With {city} ' (1)
  22. Console.WriteLine(address5.City)
  23. address5.City = "Amsterdam"
  24. Console.WriteLine(address5.City)
  25. '
  26. Dim someInstance As New SomeFirstClass
  27. Dim address6 = New With {someInstance.City, someInstance.Street()}
  28. Console.WriteLine(address6.City)
  29. Console.WriteLine(address6.Street)
  30. '
  31. Dim address7 = New With {someInstance.City, someInstance.Street(), _
  32. someInstance.Number} ' (2)
  33. Console.WriteLine(address7.City)
  34. Console.WriteLine(address7.Street)
  35. Console.WriteLine(address7.Number)
  36. '
  37. Dim address8 = New With {SomeSecondClass.City, SomeSecondClass.Street, _
  38. SomeSecondClass.Number} ' (3)
  39. Console.WriteLine(address8.City)
  40. Console.WriteLine(address8.Street)
  41. Console.WriteLine(address8.Number)
  42. '
  43. Console.WriteLine(address1.GetType().Equals(address2.GetType()))
  44. Console.WriteLine(address1.GetType().Equals(address3.GetType()))
  45. Console.WriteLine(address1.GetType().Equals(address4.GetType()))
  46. Console.WriteLine(address4.GetType().Equals(address5.GetType()))
  47. Console.WriteLine(address1.GetType().Equals(address6.GetType()))
  48. '
  49. Console.ReadLine()
  50. End Sub
  51. End Class
  52. Class SomeFirstClass
  53. Public Number As Integer
  54. Public ReadOnly Property City() As String
  55. Get
  56. City = "someCity"
  57. End Get
  58. End Property
  59. Public Function Street() As String
  60. Street = "someStreet"
  61. End Function
  62. End Class
  63. Class SomeSecondClass
  64. Public Shared Number As Integer
  65. Public Shared ReadOnly Property City() As String
  66. Get
  67. City = "someCity"
  68. End Get
  69. End Property
  70. Public Shared Function Street() As String
  71. Street = "someStreet"
  72. End Function
  73. End Class

4.局部方法 (Partial Methods)

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

  1. Class SomeClass
  2. Partial Private Sub somePartialMethod1()
  3. End Sub
  4. Partial Private Sub somePartialMethod2()
  5. End Sub
  6. '
  7. Private Sub somePartialMethod1() ' (1)
  8. Console.WriteLine("somePartialMethod1")
  9. End Sub
  10. '
  11. Public Sub TestSomePartialMethod1()
  12. somePartialMethod1()
  13. End Sub
  14. Public Sub TestSomePartialMethod2()
  15. somePartialMethod2()
  16. End Sub
  17. End Class
  18. Partial Class SomeClass
  19. Private Sub somePartialMethod2() ' (2)
  20. Console.WriteLine("somePartialMethod2")
  21. End Sub
  22. End Class
  23. Class Example1
  24. Public Shared Sub Main()
  25. Dim someObject As New SomeClass
  26. someObject.TestSomePartialMethod1()
  27. someObject.TestSomePartialMethod2()
  28. '
  29. Console.ReadLine()
  30. End Sub
  31. End Class

5.Lambda表达式

Delegate实例可以指向静态方法

  1. Option Infer On
  2. Option Strict On
  3. Public Class Example1
  4. Public Delegate Function SomeDelegate(ByVal value As Integer) As Integer
  5. Public Shared Sub Main()
  6. Dim value1 As Integer
  7. '
  8. Dim delegate1 As SomeDelegate = _
  9. New SomeDelegate(AddressOf SomeFunction) ' (1)
  10. value1 = delegate1.Invoke()
  11. Console.WriteLine(value1)
  12. '
  13. Dim delegate2 As SomeDelegate = AddressOf SomeFunction ' (2)
  14. value1 = delegate2.Invoke()
  15. Console.WriteLine(value1)
  16. '
  17. Dim delegate3 = New SomeDelegate(AddressOf SomeFunction) ' (3)
  18. value1 = delegate3.Invoke()
  19. Console.WriteLine(value1)
  20. '
  21. Dim delegate4 As Func(Of Integer, Integer) = _
  22. New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (4)
  23. value1 = delegate4.Invoke()
  24. Console.WriteLine(value1)
  25. '
  26. Dim delegate5 As Func(Of Integer, Integer) = _
  27. AddressOf SomeFunction ' (5)
  28. value1 = delegate5.Invoke()
  29. Console.WriteLine(value1)
  30. '
  31. Dim delegate6 = _
  32. New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (6)
  33. value1 = delegate6.Invoke()
  34. Console.WriteLine(value1)
  35. '
  36. Console.ReadLine()
  37. End Sub
  38. Public Shared Function SomeFunction(ByVal value As Integer) As Integer
  39. SomeFunction = value *
  40. End Function
  41. End Class

通过Lambda定义完整的方法

  1. Public Class Example2
  2. Public Shared Sub Main()
  3. Dim value1 As Integer
  4. '
  5. Dim delegate1 As Example1.SomeDelegate = _
  6. Function(value As Integer) value * ' (1)
  7. value1 = delegate1.Invoke()
  8. Console.WriteLine(value1)
  9. '
  10. Dim delegate2 As Func(Of Integer, Integer) = _
  11. Function(value As Integer) value * ' (2)
  12. value1 = delegate2.Invoke()
  13. Console.WriteLine(value1)
  14. '
  15. Dim delegate3 = Function(value As Integer) value * ' (3)
  16. value1 = delegate3.Invoke()
  17. Console.WriteLine(value1)
  18. '
  19. value1 = (Function(value As Integer) value * ).Invoke()
  20. Console.WriteLine(value1)
  21. '
  22. Console.WriteLine((Function(value As Integer) value * ).Invoke())
  23. '
  24. Console.ReadLine()
  25. End Sub
  26. End Class

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

  1. Public Class Example4
  2. Public Shared Sub Main()
  3. Dim delegate1 = Function(arg1 As Integer) _
  4. Function(arg2 As Integer) arg1 + arg2
  5. Dim value1 As Integer = delegate1.Invoke().Invoke()
  6. Console.WriteLine(value1)
  7. '
  8. Console.ReadLine()
  9. End Sub
  10. End Class

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

以下为LinQ to Object的例子。

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

  1. Option Infer On
  2. Option Strict On
  3. Public Class Person
  4. Private m_Name As String
  5. Public Property Name() As String
  6. Get
  7. Return m_Name
  8. End Get
  9. Set(ByVal value As String)
  10. m_Name = value
  11. End Set
  12. End Property
  13. Private m_City As String
  14. Public Property City() As String
  15. Get
  16. Return m_City
  17. End Get
  18. Set(ByVal value As String)
  19. m_City = value
  20. End Set
  21. End Property
  22. Private m_IsMale As Boolean
  23. Public Property IsMale() As Boolean
  24. Get
  25. Return m_IsMale
  26. End Get
  27. Set(ByVal value As Boolean)
  28. m_IsMale = value
  29. End Set
  30. End Property
  31. Public Overrides Function ToString() As String
  32. ToString = "Female "
  33. If IsMale Then ToString = "Male "
  34. ToString &= Name & ", from " & City & "."
  35. End Function
  36. End Class
  37. Public Class Example1
  38. Public Shared Sub Main()
  39. Dim persons As Person() = New Person() { _
  40. New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
  41. New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
  42. New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
  43. New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
  44. '
  45. Dim personsNY As IEnumerable(Of Person) = _
  46. persons.Where(Function(person) person.City = "New York") _
  47. .OrderBy(Function(person) person.Name) _
  48. .Select(Function(person) person)
  49. '
  50. For Each person As Person In personsNY
  51. Console.WriteLine(person)
  52. Next
  53. '
  54. Console.ReadLine()
  55. End Sub
  56. End Class

利用LinQ表达式

  1. Public Class Example2
  2. Public Shared Sub Main()
  3. Dim persons As Person() = New Person() { _
  4. New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
  5. New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
  6. New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
  7. New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
  8. '
  9. Dim personsNY As IEnumerable(Of Person) = _
  10. From person In persons _
  11. Where person.City = "New York" _
  12. Order By person.Name _
  13. Select person
  14. '
  15. For Each person As Person In personsNY
  16. Console.WriteLine(person)
  17. Next
  18. '
  19. Console.ReadLine()
  20. End Sub
  21. 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. Urllib3 库详解

    文档:http://urllib3.readthedocs.io/en/latest/

  2. Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)

    概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...

  3. js关于弹也遮罩层

    1:什么是遮罩层 遮罩层:我是弹也一个(遮罩层)还有一个(内容层),下面上图片看一效果 我们看到一个灰蒙蒙的遮盖(其实也是一个层)还有一个层(也就是我们展示的内容). 2:  弹出层效果居中分析 在这 ...

  4. 用css3实现社交分享按钮

    以前实现按钮一般都是用图片来实现的,特别是一些拥有质感的按钮,今天练习了一些相关方面的的例子,用css3来实现Social Media Buttons html代码如下 <div class=& ...

  5. [hadoop读书笔记] 第四章 Hadoop I/O操作

    P92 压缩 P102 序列化 序列化:将结构化对象转为字节流便于在网上传输或写到磁盘进行永久性存储的过程 用于进程之间的通信或者数据的永久存储 反序列化:将字节流转为结构化对象的逆过程 Hadoop ...

  6. this小案例

    public class Son extends Parent { public String name="jack"; public void init(){ super.ini ...

  7. selenium+java+chrome环境搭建

    我只能说因为版本冲突,简直太折腾了,而搜了无数个博友的帖子才找到正确条案,就不能好好的写篇文章吗? 最近真的是太闲太闲了,平时没事总得搞点技术,不然心里感觉好空虚, 最近看上了selenium,所以试 ...

  8. applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found

    applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found 解决办法: 1 ...

  9. 捋一捋Spring Web的源码思路

    Servlet前提 Java规定了Servlet Container为每一个web app创建一个Servlet Context:而Servlet Context中又包含了诸多Servlet -- 其 ...

  10. e615. Finding the Next Focusable Component

    public Component findNextFocus() { // Find focus owner Component c = KeyboardFocusManager.getCurrent ...