[VB.NET]VB 10.0 Statement Lambdas
'呼叫addLambdas
Console.WriteLine(addLambdas(123, 456))</pre></div><p> </p><p>同樣的程式我們也可以明確的定義回傳值型態</p><p> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:15d67606-728e-475d-991c-500cc1d0da0d" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code">
Dim addLambdas = Function(num1, num2) As Integer 也可以明確的指定帶入的參數型態 Dim addLambdas = Function(num1 As Integer, num2 As Integer) As Integer 若有要傳遞參考的需求,也可以在函式參數前加上ByRef Dim addLambdas = Function(ByRef num1 As Integer, num2 As Integer) As Integer Sub LambdasSub Lambdas使用上就跟一般的Lambdas運算式一樣,不同的是Sub Lambdas呼叫後不會有回傳值。就跟副程式一樣是沒有回傳值的,使用上只需把Lambdas運算式的Function關鍵字改為Sub即可。跟副程式的寫法類似。Sub Lambdas跟一般的Lambdas一樣,除了有Multiline lambdas外 ‘宣告addLambdas Dim addLambdas = Sub(num1, num2) Console.WriteLine(num1 + num2) End Sub
'呼叫addLambdas
addLambdas(123, 456)</pre></div><p> </p><p>也有Singleline lambdas</p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:047e6683-0e50-457d-81c8-53c4a1fcf163" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code">
'宣告addLambdas
Dim addLambdas = Sub(num1, num2) Console.WriteLine(num1 + num2)</pre></div><p> </p><p>使用上都大同小異</p><p> </p><h2>注意事項</h2><p><strong>1.要指定參數型別時,必需同時指定所有參數型別。</strong></p><p>若在使用上只指定部份參數型,而未指定所有參數的型別,此時編譯器會提示 "All parameters must be explicitly typed if any of them are explicitly typed" 的錯誤。</p><p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" width="609" height="37" src="\images\posts\9989\image_thumb.png" /></a></p><p> </p><p><strong>2.不支援Optional關鍵字</strong></p><p>若在參數前面加上Optional,則編譯器會提示的 "'Lambdas' params cannot be declared 'Optional'" 錯誤。</p><p><a rel="lightbox" href="http://files.dotblogs.com.tw/larrynung/0908/VB10.0MultilineLambdas_11D09/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" width="516" height="37" src="\images\posts\9989\image_thumb_1.png" /></a></p><p> </p><p><strong>3.不支援ParamArray關鍵字</strong></p><p>若在參數前面加上ParamArray,則編譯器會提示的 "'Lambdas' params cannot be declared ParamArray" 錯誤。</p><p><a rel="lightbox" href="http://files.dotblogs.com.tw/larrynung/0908/VB10.0MultilineLambdas_11D09/image_6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" width="413" height="106" src="\images\posts\9989\image_thumb_2.png" /></p>