java語言轉c#語言開發中,更多區別參考第二個網址,提供代碼參考:
using System;
using System.Linq;
using System.Text;
// using 關鍵字類似import java.util.Collection
using System.Collections;
namespace ConsoleApplication1
{
// sealed關鍵字作用類似final, 使用 : 進行繼承(extends)或者實現(implements)
sealed class Program : CollectionBase
{
// const關鍵字作用類似static final
const string cst = "cst";
// Main 方法類似 main, c#可以無參,有返回值
static void Main()
{
// bool 數據類型類似 boolean, 包裝類型都是Boolean
bool b = false;
// string 數據類型可以小寫,類似String
string s = "1";
// c#使用hashtable實現鍵值對存儲,不需要設置鍵值的數據類型,add()方法設置值
Hashtable table = new Hashtable();
table.Add("1", 1);
table.Add("2", 2);
// 不支持 int arr[] = { 1, 2, 3 };
int[] arr = { 1, 2, 3 };
// 四種遍歷hashtable方法
// 遍歷獲取key
foreach (string key in table.Keys)
{
Console.WriteLine(key);
}
// 遍歷獲取value
foreach (int value in table.Values)
{
Console.WriteLine(value);
}
// 遍歷獲取鍵值對
foreach (DictionaryEntry de in table)
{
Console.WriteLine(de.Key);
Console.WriteLine(de.Value);
}
// 迭代器獲取鍵值
IDictionaryEnumerator enumerator = table.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Key);
Console.WriteLine(enumerator.Value);
}
// 控制檯輸出,類似System.out.println();
Console.WriteLine(s);
// 支持格式化輸出數據, 參考第一個網址格式
Console.WriteLine("{0:C3}\t{1:D2}", 2, 31);
// 從控制檯讀取字符, 使控制檯不自動關閉
Console.Read();
}
// 構造函數
Program() {
// base類似super關鍵字
base.Clear();
}
}
}