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();
}
}
}