讓C#眼紅的VB.NET屬性

來源:互聯網
上載者:User

標籤:沒有   ase   asp   trick   compiler   .net   base   iter   soft   

 

什麼是XML Literal呢?
"XML Literal"是在VB.NET 9.0引入的一個用於構建XML對象的新特性。VB.net有了如此神器,現在輪到C#程式員羨慕了。當然這篇文章不是說如何使用XML Literal的,關於這個新特性的更多資訊可以參考MSDN:
http://msdn.microsoft.com/en-us/library/bb384629.aspx


有了XML Literal之後,我們如何用它解決VB.Net中多行字串的問題呢?來看樣本:

 

Dim sourceText As String =      <string>            Imports Microsoft.VisualBasic          Imports System          Imports System.Collections          Imports Microsoft.Win32          Imports System.Linq          Imports System.Text          Imports Roslyn.Compilers          Imports System.ComponentModel          Imports System.Runtime.CompilerServices          Imports Roslyn.Compilers.VisualBasic                    Namespace HelloWorld            Module Program              Sub Main(args As String())                Console.WriteLine("Hello, World!")              End Sub            End Module          End Namespace      </string>  

  

  

 

對比一下前面的代碼,有沒有發自內心的想說出“我靠”兩個字?

 

 

 

比較明顯的變化就是沒有了As String的顯式型別宣告,然後後面調用.Value屬性以確定這個變數確實是字串。實際上與上面代碼相比,這兩段代碼在編譯、運行時並沒有什麼區別。也許第二種更漂亮些?

 

 

更厲害的是,我們還可以這樣寫:

Dim contact2 =         <contact>          <name>Patrick Hines</name>          <%= From p In phoneNumbers2             Select <phone type=<%= p.Type %>><%= p.Number %></phone>           %>        </contact>

  

所以以後不要嘲笑VB.NET 不能方便的寫字串了!!

另外吐槽VB.NET 不能多行語句寫在一行的我也要說一下,這個在VB6之前就出現的產物你們不知道?且看:

Dim a, b As String : a = 1 : b = "abc"

  

 

讓C#眼紅的VB.NET屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.