孤獨者 - 心在黑暗裡旅行 ...... ... 至尊會員
遊蕩的過客 - 枉入紅塵若許年 .. ... - UID
- 18273
- 帖子
- 12122
- 精華
- 1273
- 積分
- 240737
- 金幣
- 492641
- 威望
- 5001
- 推廣
- 0
- 閱讀權限
- 99
- 來自
- 尋覓中
- 註冊時間
- 2006-6-24
- 最後登錄
- 2015-5-27
|
1樓
大 中
小 發表於 2006-12-12 23:19
《Microsoft ASP.NET 教學》語言支援
語言支援Microsoft .NET 平台目前對三種語言提供內建支援:C#、Visual Basic 和 JScript。 這個教學課程的習題和程式碼範例中,示範如何使用 C#、Visual Basic 和 JScript 建置 .NET 應用程式。如需其他語言的語法的詳細資訊,請參閱 .NET Framework SDK 的完整文件。 下列表格可以幫助您瞭解這個教學課程中的程式碼範例,和三個語言之間的差異: 變數宣告 int x;String s;String s1, s2;Object o;Object obj = new Object();public String name;Dim x As IntegerDim s As StringDim s1, s2 As StringDim o 'Implicitly ObjectDim obj As New Object()Public name As Stringvar x : int;var s : String;var s1 : String, s2 : String;var o;var obj : Object = new Object();var name : String; | C# | VB | JScript | |
陳述式 Response.Write("foo");Response.Write("foo")Response.Write("foo"); | C# | VB | JScript | |
註解 // This is a comment/*Thisisamultilinecomment*/' This is a comment' This' is' a' multiline' comment// This is a comment/*Thisisamultilinecomment*/ | C# | VB | JScript | |
存取索引屬性 String s = Request.QueryString["Name"];String value = Request.Cookies["key"];Dim s, value As Strings = Request.QueryString("Name")value = Request.Cookies("Key").Value'Note that default non-indexed properties'must be explicitly named in VBvar s : String = Request.QueryString("Name");var value : String = Request.Cookies("key"); | C# | VB | JScript | |
宣告索引屬性 // Default Indexed Propertypublic String this[String name] { get { return (String) lookuptable[name]; }}' Default Indexed PropertyPublic Default ReadOnly Property DefaultProperty(Name As String) As String Get Return CStr(lookuptable(name)) End GetEnd PropertyJScript 不支援建立索引屬性或預設索引屬性。若要在 JScript 中模擬這些屬性,請使用函式宣告來代替。雖然您無法在 C# 中使用索引屬性語法來存取函式,但它仍會提供 Visual Basic 的效果。public function Item(name:String) : String { return String(lookuptable(name));} | C# | VB | JScript | |
宣告簡單屬性 public String name { get { ... return ...; } set { ... = value; }}Public Property Name As String Get ... Return ... End Get Set ... = Value End SetEnd Propertyfunction get name() : String { ... return ...;}function set name(value : String) { ... = value;} | C# | VB | JScript | |
宣告和使用列舉型別 // Declare the Enumerationpublic enum MessageSize { Small = 0, Medium = 1, Large = 2}// Create a Field or Propertypublic MessageSize msgsize;// Assign to the property using the Enumeration valuesmsgsize = Small;' Declare the EnumerationPublic Enum MessageSize Small = 0 Medium = 1 Large = 2End Enum' Create a Field or PropertyPublic MsgSize As MessageSize' Assign to the property using the Enumeration valuesMsgSize = small// Declare the Enumerationpublic enum MessageSize { Small = 0, Medium = 1, Large = 2}// Create a Field or Propertypublic var msgsize:MessageSize;// Assign to the property using the Enumeration valuesmsgsize = Small; | C# | VB | JScript | |
列舉集合 foreach ( String s in coll ) { ...}Dim S As StringFor Each S In Coll ...Nextvar s:String;for ( s in coll ) { ...} | C# | VB | JScript | |
宣告和使用方法 // Declare a void return functionvoid voidfunction() { ...}// Declare a function that returns a valueString stringfunction() { ... return (String) val;}// Declare a function that takes and returns valuesString parmfunction(String a, String b) { ... return (String) (a + b);}// Use the Functionsvoidfunction();String s1 = stringfunction();String s2 = parmfunction("Hello", "World!");' Declare a void return functionSub VoidFunction() ...End Sub' Declare a function that returns a valueFunction StringFunction() As String ... Return CStr(val)End Function' Declare a function that takes and returns valuesFunction ParmFunction(a As String, b As String) As String ... Return CStr(A & B)End Function' Use the FunctionsVoidFunction()Dim s1 As String = StringFunction()Dim s2 As String = ParmFunction("Hello", "World!")// Declare a void return functionfunction voidfunction() : void { ...}// Declare a function that returns a valuefunction stringfunction() : String { ... return String(val);}// Declare a function that takes and returns valuesfunction parmfunction(a:String, b:String) : String { ... return String(a + b);}// Use the Functionsvoidfunction();var s1:String = stringfunction();var s2:String = parmfunction("Hello", "World!"); | C# | VB | JScript | |
自訂屬性 // Stand-alone attribute[STAThread]// Attribute with parameters[DllImport("ADVAPI32.DLL")]// Attribute with named parameters[DllImport("KERNEL32.DLL", CharSet=CharSet.Auto)]' Stand-alone attribute<STAThread>' Attribute with parameters<DllImport("ADVAPI32.DLL")>' Attribute with named parameters<DllImport("KERNEL32.DLL", CharSet:=CharSet.Auto)>// Stand-alone attributeSTAThreadAttribute// Attribute with parametersDllImportAttribute("ADVAPI32.DLL")// Attribute with named parametersDllImportAttribute("KERNEL32.DLL", CharSet=CharSet.Auto) | C# | VB | JScript | |
陣列 String[] a = new String[3]; a[0] = "1"; a[1] = "2"; a[2] = "3"; String[][] a = new String[3][3]; a[0][0] = "1"; a[1][0] = "2"; a[2][0] = "3"; Dim a(2) As String a(0) = "1" a(1) = "2" a(2) = "3" Dim a(2,2) As String a(0,0) = "1" a(1,0) = "2" a(2,0) = "3" var a : String[] = new String[3]; a[0] = "1"; a[1] = "2"; a[2] = "3"; var a : String[][] = new String[3][3]; a[0][0] = "1"; a[1][0] = "2"; a[2][0] = "3"; | C# | VB | JScript | |
初始化 String s = "Hello World";int i = 1;double[] a = { 3.00, 4.00, 5.00 };Dim s As String = "Hello World"Dim i As Integer = 1Dim a() As Double = { 3.00, 4.00, 5.00 }var s : String = "Hello World";var i : int = 1;var a : double[] = [ 3.00, 4.00, 5.00 ]; | C# | VB | JScript | |
If 陳述式 if (Request.QueryString != null) { ...}If Not (Request.QueryString = Nothing) ...End Ifif (Request.QueryString != null) { ...} | C# | VB | JScript | |
Case 陳述式 switch (FirstName) { case "John" : ... break; case "Paul" : ... break; case "Ringo" : ... break; default: ... break;}Select Case FirstName Case "John" ... Case "Paul" ... Case "Ringo" ... Case Else ...End Selectswitch (FirstName) { case "John" : ... break; case "Paul" : ... break; case "Ringo" : ... break; default: ... break;} | C# | VB | JScript | |
For 迴圈 for (int i=0; i<3; i++) a(i) = "test"; Dim I As Integer For I = 0 To 2 a(I) = "test" Nextfor (var i : int = 0; i < 3; i++) a = "test"; | C# | VB | JScript | |
While 迴圈 int i = 0;while (i<3) { Console.WriteLine(i.ToString()); i += 1;}Dim I As IntegerI = 0Do While I < 3 Console.WriteLine(I.ToString()) I += 1Loopvar i : int = 0;while (i < 3) { Console.WriteLine(i); i += 1;} | C# | VB | JScript | |
例外處理 try { // Code that throws exceptions} catch(OverflowException e) { // Catch a specific exception} catch(Exception e) { // Catch the generic exceptions} finally { // Execute some cleanup code}Try ' Code that throws exceptionsCatch E As OverflowException ' Catch a specific exceptionCatch E As Exception ' Catch the generic exceptionsFinally ' Execute some cleanup codeEnd Trytry { // Code that throws exceptions} catch(e:OverflowException) { // Catch a specific exception} catch(e:Exception) { // Catch the generic exceptions} finally { // Execute some cleanup code} | C# | VB | JScript | |
字串串連 // Using StringsString s1;String s2 = "hello";s2 += " world";s1 = s2 + " !!!";// Using StringBuilder class for performanceStringBuilder s3 = new StringBuilder();s3.Append("hello");s3.Append(" world");s3.Append(" !!!");' Using StringsDim s1, s2 As Strings2 = "hello"s2 &= " world"s1 = s2 & " !!!"' Using StringBuilder class for performanceDim s3 As New StringBuilder()s3.Append("hello")s3.Append(" world")s3.Append(" !!!")// Using Stringsvar s1 : String;var s2 : String = "hello";s2 += " world";s1 = s2 + " !!!";// Using StringBuilder class for performancevar s3:StringBuilder = new StringBuilder();s3.Append("hello");s3.Append(" world");s3.Append(" !!!"); | C# | VB | JScript | |
事件處理常式委派 void MyButton_Click(Object sender, EventArgs E) {...}Sub MyButton_Click(Sender As Object, E As EventArgs)...End Subfunction MyButton_Click(sender : Object, E : EventArgs) {...} | C# | VB | JScript | |
宣告事件 // Create a public eventpublic event EventHandler MyEvent;// Create a method for firing the eventprotected void OnMyEvent(EventArgs e) { MyEvent(this, e);}' Create a public eventPublic Event MyEvent(Sender as Object, E as EventArgs)' Create a method for firing the eventProtected Sub OnMyEvent(E As EventArgs) RaiseEvent MyEvent(Me, E)End SubJScript 不支援建立事件。JScript 只能透過宣告事件處理常式委派並以及將這些委派加入其他控制項的事件中來消耗事件。 | C# | VB | JScript | |
對事件加入或移除事件處理常式 Control.Change += new EventHandler(this.ChangeEventHandler);Control.Change -= new EventHandler(this.ChangeEventHandler);AddHandler Control.Change, AddressOf Me.ChangeEventHandlerRemoveHandler Control.Change, AddressOf Me.ChangeEventHandlerControl.Change += this.ChangeEventHandler;Control.Change -= this.ChangeEventHandler; | C# | VB | JScript | |
轉型 MyObject obj = (MyObject)Session["Some Value"];IMyObject iObj = obj;Dim obj As MyObjectDim iObj As IMyObjectobj = Session("Some Value")iObj = CType(obj, IMyObject)var obj : MyObject = MyObject(Session("Some Value"));var iObj : IMyObject = obj; | C# | VB | JScript | |
轉換 int i = 3;String s = i.ToString();double d = Double.Parse(s);Dim i As IntegerDim s As StringDim d As Doublei = 3s = i.ToString()d = CDbl(s)' See also CDbl(...), CStr(...), ...var i : int = 3;var s : String = i.ToString();var d : double = Number(s); | C# | VB | JScript | |
類別定義與繼承 using System;namespace MySpace { public class Foo : Bar { int x; public Foo() { x = 4; } public void Add(int x) { this.x += x; } override public int GetNum() { return x; } }}// csc /out:librarycs.dll /t:library// library.csImports SystemNamespace MySpace Public Class Foo : Inherits Bar Dim x As Integer Public Sub New() MyBase.New() x = 4 End Sub Public Sub Add(x As Integer) Me.x = Me.x + x End Sub Overrides Public Function GetNum() As Integer Return x End Function End ClassEnd Namespace' vbc /out:libraryvb.dll /t:library' library.vbimport System;package MySpace { class Foo extends Bar { private var x : int; function Foo() { x = 4; } function Add(x : int) { this.x += x; } override function GetNum() : int { return x; } }}// jsc /out:libraryjs.dll library.js | C# | VB | JScript | |
實作介面 public class MyClass : IEnumerable { ... IEnumerator IEnumerable.GetEnumerator() { ... }}Public Class MyClass : Implements IEnumerable ... Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator ... End FunctionEnd Classpublic class MyClass implements IEnumerable { ... function IEnumerable.GetEnumerator() : IEnumerator { ... }} | C# | VB | JScript | |
類別定義與 Main 方法 using System;public class ConsoleCS { public ConsoleCS() { Console.WriteLine("Object Created"); } public static void Main (String[] args) { Console.WriteLine("Hello World"); ConsoleCS ccs = new ConsoleCS(); }}// csc /out:consolecs.exe /t:exe console.csImports SystemPublic Class ConsoleVB Public Sub New() MyBase.New() Console.WriteLine("Object Created") End Sub Public Shared Sub Main() Console.WriteLine("Hello World") Dim cvb As New ConsoleVB End SubEnd Class' vbc /out:consolevb.exe /t:exe console.vbclass ConsoleCS { function ConsoleCS() { print("Object Created"); } static function Main (args : String[]) { print("Hello World"); var ccs : ConsoleCS = new ConsoleCS(); }}// jsc /out:consolejs.exe /exe console.js | C# | VB | JScript | |
標準模組 using System;public class Module {public static void Main (String[] args) { Console.WriteLine("Hello World");}}// csc /out:consolecs.exe /t:exe console.csImports SystemPublic Module ConsoleVB Public Sub Main() Console.WriteLine("Hello World") End SubEnd Module' vbc /out:consolevb.exe /t:exe console.vbprint("Hello World");// jsc /out:consolejs.exe /exe console.js | C# | VB | JScript | |
|