共用方式為


比較各種語言的程式設計概念,含程式碼範例

這是一個無法以關鍵字做摘要的基本程式觀念的範例程式碼。 如需詳細資訊,請參閱 比較各種語言的關鍵字

程式碼範例可見於下列工作:

  • 指派陳述式

  • 註解

  • 條件陳述式

  • 宣告變數

  • For 迴圈

  • 泛型型別

  • 隱藏基底類別成員

  • 初始化實值型別

  • 以傳址方式傳遞的參數

  • 以傳值方式傳遞的參數

  • 選擇陳述式

  • 將物件參考設定為無

  • 結構化例外狀況處理

  • While 迴圈

宣告變數

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

Dim x As Integer
Public x As Integer = 10
Dim x = 10

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

int x;
int x = 10;
var x = 10;

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

int x;
int x = 10;

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

let x = 10

註解

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

' comment
x = 1   ' comment
Rem comment 

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

// comment

/* multiline
 comment */

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

// comment
/* multiline
 comment */

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

// comment
(* multiline
 comment *)

指派陳述式

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

nVal = 7

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

nVal = 7;

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

nVal = 7;

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

nVal <- 7

條件陳述式

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

If nCnt <= nMax Then
   nTotal += nCnt  ' Same as nTotal = nTotal + nCnt.
   nCnt += 1       ' Same as nCnt = nCnt + 1.
Else
   nTotal += nCnt
   nCnt -= 1       
End If

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

if(nCnt < nMax) {
 nTotal += nCnt;
 nCnt++;
 }
else {
   nTotal += nCnt;
   nCnt --;    
 };

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

if (nCnt <= nMax)
{
   nTotal += nCnt;
   nCnt++;
}
else
{
   nTotal +=nCnt;
   nCnt--;
}

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

if (nCnt <= nMax) then
    nTotal <- nTotal + nCnt
    nCnt <- nCnt + 1
else
    nTotal <- nTotal + nCnt
    nCnt <- nCnt - 1

選擇陳述式

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

Select Case n
   Case 0
      MsgBox ("Zero")  
     ' Visual Basic exits the Select at the end of a Case.
   Case 1
      MsgBox ("One")
   Case 2 
      MsgBox ("Two")
   Case Else
      MsgBox ("Default")
End Select

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

switch(n) {
 case 0:
  printf_s("Zero\n");
  break;
 case 1:
  printf_s("One\n");
  break;
 case 2:
  printf_s("Two\n");
  break;
 default:
  printf_s("?\n");}

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

switch(n) 
{
   case 0:
      Console.WriteLine("Zero");
      break;
   case 1:
      Console.WriteLine("One");
      break;
   case 2:
      Console.WriteLine("Two");
      break;
   default:
      Console.WriteLine("?");
      break;
}

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

比對 n 與

| 0 -> Console.WriteLine("Zero")

| 1 -> Console.WriteLine("One")

| 2 -> Console.WriteLine("Two")

| _ -> Console.WriteLine("?")

For 迴圈

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

For n = 1 To 10 
   MsgBox("The number is " & n)
Next

For Each prop In obj
    prop = 42
Next prop

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

for(int n=1; n<11; n++)
 printf_s("%d\n",n);

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

for (int i = 1; i <= 10; i++) 
   Console.WriteLine("The number is {0}", i);
foreach (int i in testArr) 
{
   Console.WriteLine(i);
}

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

for i = 1 to 10 do
    printfn "%d" i
for i in testCollection do
    printfn "%d" i

隱藏基底類別成員

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

Public Class BaseCls
   Public Z As Integer = 100   ' The element to be shadowed
   public Sub Test()
      System.Console.WriteLine("Test in BaseCls")
   End Sub
End Class

Public Class DervCls
   Inherits BaseCls
   Public Shadows Z As String = "*"   ' The shadowing element.
   public Shadows Sub Test()
      System.Console.WriteLine("Test in DervCls")
   End Sub
End Class

Public Class UseClasses
   Dim BObj As BaseCls = New DervCls()   ' DervCls widens to BaseCls. 
   Dim DObj As DervCls = New DervCls()   ' Access through derived class.
   Public Sub ShowZ()
      System.Console.WriteLine("Accessed through base class: " & BObj.Z)
      System.Console.WriteLine("Accessed through derived class: " & DObj.Z)
      BObj.Test()
      DObj.Test()
   End Sub 
End Class

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

#using <mscorlib.dll>
#include <stdio.h>
public __gc class BaseCls
{
public:
   int Z;   // The element to be hidden
   void Test()
   {
      printf_s("Test in BaseCls\n");
   }

};

public __gc class DervCls : public BaseCls
{
public:
   char Z;   // The hiding element
   void Test()
   {
      printf_s("Test in DervCls\n");
   }

};

public __gc class UseClasses
{
public:
   BaseCls * BObj;   // DervCls widens to BaseCls
   DervCls * DObj;   // Access through derived class
   void ShowZ()
   {
      BObj = new DervCls;
      BObj->Z = 100;
      DObj = new DervCls;
      DObj->Z = '*';
      printf_s("Accessed through base class: %d\n", BObj->Z);
      printf_s("Accessed through derived class: %c\n", DObj->Z);
      BObj->Test();
      DObj->Test();
   }
};

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

public class BaseCls
{
   public int Z = 100;   // The element to be hidden
   public void Test()
   {
      System.Console.WriteLine("Test in BaseCls");
   }
}

public class DervCls : BaseCls
{
   public new string Z = "*";   // The hiding element
   public new void Test()
   {
      System.Console.WriteLine("Test in DervCls");
   }
}

public class UseClasses
{
   BaseCls BObj = new DervCls();   // DervCls widens to BaseCls
   DervCls DObj = new DervCls();   // Access through derived class
   public void ShowZ()
   {
      System.Console.WriteLine("Accessed through base class: {0}", BObj.Z);
      System.Console.WriteLine("Accessed through derived class: {0}", DObj.Z);
      BObj.Test();
      DObj.Test();
   }
}

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

type BaseCls() =
    member this.Z = 100
    member this.Test() =
        System.Console.WriteLine("Test in BaseCls")

type DervCls() =
    inherit BaseCls()
    member this.Z = "*"
    member this.Test() =
       System.Console.WriteLine("Test in DervCls")

type UseClasses() =
    let BObj : BaseCls = new DervCls() :> BaseCls
    let DObj : DervCls = new DervCls()
    member this.ShowZ() =
        System.Console.WriteLine("Accessed through base class: {0}", BObj.Z)
        System.Console.WriteLine("Accessed through derived class: {0}", DObj.Z)
        BObj.Test()
        DObj.Test()

let useClassesObj = new UseClasses()
useClassesObj.ShowZ()

While 迴圈

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

While n < 100 ' Test at start of loop.
   n += 1     ' Same as n = n + 1.
End While '

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

while(int n < 100)
   n++;

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

while (n < 100)
   n++;

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

while n < 100 do
     n <- n + 1

以傳值方式傳遞的參數

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

Public Sub ABC(ByVal y As Long) ' The argument Y is passed by value.
' If ABC changes y, the changes do not affect x.
End Sub
   
ABC(x) ' Call the procedure.
' You can force parameters to be passed by value, regardless of how 
' they are declared, by enclosing the parameters in extra parentheses.
ABC((x))

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

testMethod(i,j);

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

/* Note that there is no way to pass reference types (objects) strictly by value. 
You can choose to either pass the reference (essentially a pointer), 
or a reference to the reference (a pointer to a pointer).*/
// The method:
void ABC(int x)
{
   ...
}
// Calling the method:
ABC(i);

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

let ABC(x) =
   ...
ABC(i)

以傳址方式傳遞的參數

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

Public Sub ABC(ByRef y As Long) 
' The parameter y is declared by by referece:
' If ABC changes y, the changes are made to the value of x.
End Sub

ABC(x) ' Call the procedure.

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

// Prototype of ABC that takes a pointer to integer.
int ABC(long *py);
ABC(&VAR);
// Prototype of ABC that takes a reference to integer.
int ABC(long &y);
ABC(VAR);

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

/* Note that there is no way to pass reference types (objects) strictly by value. 
You can choose to either pass the reference (essentially a pointer), 
or a reference to the reference (a pointer to a pointer).*/
/* Note also that unsafe C# methods can take pointers just like C++ methods. 
For details, see <MSHelp:link keywords="vclrfUnsafe" TABINDEX="0">unsafe</MSHelp:link>. */
// The method:
void ABC(ref int x)
{
   ...
}
// Calling the method:
ABC(ref i);

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

let ABC(x : int byref) =
   ...
ABC(&i)

結構化例外狀況處理

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

Try
   If x = 0 Then
      Throw New Exception("x equals zero")
   Else
      Throw New Exception("x does not equal zero")
   End If
Catch err As System.Exception
   MsgBox("Error: " & Err.Description)
Finally
   MsgBox("Executing finally block.")
End Try

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

      __try{
      if (x == 0)
         throw new Exception ("x equals zero");
      else
         throw new Exception ("x does not equal zero");
         }
      __catch(Exception e)
{
            Console.WriteLine("Caught Exception"); 
      }
      __finally
{
         Console.WriteLine("Executing finally block");
      }

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

// try-catch-finally
try
{
   if (x == 0)
      throw new System.Exception ("x equals zero");
   else
      throw new System.Exception ("x does not equal zero");
}
catch (System.Exception err)
{
   System.Console.WriteLine(err.Message);
}
finally
{
   System.Console.WriteLine("executing finally block");
}

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

try
    try
        if x = 0 then
             failwith "x equals zero"
        else
             failwith "x does not equal zero"
    with
        | Failure(msg) -> System.Console.WriteLine(msg)
finally
    printfn "executing finally block"

將物件參考設定為無

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

o = Nothing

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

o = nullptr;  // when compiling with /clr

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

o = null;

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

// for an option type
o <- None
// when you really want a null value
o <- null

初始化實值型別

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

Dim dt as New System.DateTime(2001, 4, 12, 22, 16, 49, 844)

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

System::DateTime dt = System::DateTime(2001, 4, 12, 22, 16, 49, 844);

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

System.DateTime dt = new System.DateTime(2001, 4, 12, 22, 16, 49, 844);

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

let dt = new System.DateTime(2001, 4, 12, 22, 16, 49, 844)

泛型型別

we7h0cz1.collapse_all(zh-tw,VS.110).gifVisual Basic

' Define a generic type
Public Class classMaker(Of t)
' Constrain a type parameter
Public Class classMaker(Of t As IComparable)
' Create an object from a generic type
Dim integerClass As New classMaker(Of Integer)

we7h0cz1.collapse_all(zh-tw,VS.110).gifC++

// Define a generic type
generic <typename T> ref class testList { ... };
// Constrain a type parameter
generic <typename T> where T : IComparable<T>
ref class testList { ... };
// Create an object from a generic type
testList<int>^ list = gcnew testList<int>();

we7h0cz1.collapse_all(zh-tw,VS.110).gifC#

// Define a generic type
public class testList<T>{...}
// Constrain a type parameter
public class testList<T> where T : IComparable<T>
// Create an object from a generic type
testList<int> list = new testList<int>();

we7h0cz1.collapse_all(zh-tw,VS.110).gifF#

// Define a generic type
type testList<'T> =
    ...
// Constraint a type parameter
type testList<'T when 'T :> IComparable<'T>>
// Create an object from a generic type
let list1 = new testList<int>()

請參閱

參考

比較各種語言的關鍵字

比較各種語言的資料型別

比較各種語言的運算子

比較各種語言和程式庫的控制項與可以透過程式設計的物件

其他資源

語言對等用法