java与c#语言class编写区别

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

参考:
https://blog.csdn.net/xrongzhen/article/details/5477075

https://blog.csdn.net/qq_39657909/article/details/80781481

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值