日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#与java对比

發(fā)布時間:2025/3/21 C# 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#与java对比 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#c#課時01 ##c#與java對比 ###創(chuàng)建: 文件-新建-項目-VisualC#-控制臺應(yīng)用程序。 ###結(jié)構(gòu): C#:

using System; namespace HelloWorld {class Hello {static void Main() {Console.WriteLine("Hello World!");Console.WriteLine("Press any key to exit.");Console.ReadKey();}} }

JAVA:

import java.io.*; package cn.easycomm.test; public class HelloWorld{ public static void main(String[] args) { System.out.println("Hello World!"); } }
  • namespace與package
  • using與import
  • 注:using有另一種用法

  • Main與main
  • ###數(shù)據(jù)類型:

  • C# 提供 Java 中可用的所有數(shù)據(jù)類型,并增加了對無符號數(shù)字和新的 128 位高精度浮點類型的支持。

  • Java 的?boolean?在 C# 中稱為?bool

  • 常量,Java 使用?final?字段修飾符聲明此類變量,而 C# 則使用?const?關(guān)鍵字

    const int NUM = 1; //c#public static final int NUM = 1; //java
  • 字符串,Java用equals,C#可以直接用==或!=

  • 轉(zhuǎn)義字符,都使用?\?,C#中字符串開始前使用?@?聲明字符串則不需轉(zhuǎn)義字符

  • ###運算符

  • C# 提供 Java 支持的所有適用的運算符
  • C# 中可用但 Java 中沒有的一些新運算符(checked,unchecked...)
  • ###流控制

  • 在 Java 和 C# 這兩種語言中,if?else?完全相同

  • switch,C# 要求在每個?case?的末尾都使用?break,?case?中可以使用字符串變量

  • 在 C# 和 Java 中,for?循環(huán)的語法和操作相同

  • C#中引入了foreach?,Java中使用的是for

    C#

    static void Main(){string[] arr= new string[] {"Jan", "Feb", "Mar"};foreach (string s in arr){System.Console.WriteLine(s);}}

    Java

    for (String x : list) { System.out.println(x); }
  • while?和?do...while?語句的語法和操作是相同的

  • ###參數(shù)傳遞

  • 在 Java 和 C# 中,引用對象的方法參數(shù)始終都是通過引用傳遞的,而基元數(shù)據(jù)類型參數(shù)(C# 中的值類型)是通過值傳遞的。

  • 在 C# 中,若要通過引用傳遞值類型,需要指定關(guān)鍵字?ref?或?out

    ref

    class TestRef{private static void Add(int i, ref int result){result += i;return;}static void Main(){int total = 20;System.Console.WriteLine("Original value of 'total': {0}", total);Add(10, ref total);System.Console.WriteLine("Value after calling Add(): {0}", total);}}Original value of 'total': 20Value after calling Add(): 30

    out

    class TestOut{private static void Add(int i, int j, out int result){// The following line would cause a compile error:// System.Console.WriteLine("Initial value inside method: {0}", result);result = i + j;return;}static void Main(){int total = 20;System.Console.WriteLine("Original value of 'total': {0}", total);Add(33, 77, out total);System.Console.WriteLine("Value after calling Add(): {0}", total);}}Original value of 'total': 20Value after calling Add(): 110
  • ###屬性

  • get?,set方法

    public class Animal{public string Age { get; set; }private string name;public string Species{get{return name;}set{name = value;}}}
  • 訪問屬性

    animal.Species = "Lion"; // set accessorSystem.Console.WriteLine(animal.Species); // get accessor
  • ###數(shù)組

  • 定義初始化

    int[] arr2Lines; //int arr2[]; //compile errorarr2Lines = new int[5] {1, 2, 3, 4, 5};int[] arr1Line = {1, 2, 3, 4, 5};
  • ###繼承與接口

  • 在 C# 中,繼承及接口實現(xiàn)均由 : 運算符定義,此運算符與 Java 中的 extends 和 implements 等效
  • base與super,訪問基類
  • ###異常

  • C# 中的異常處理與 Java 中的異常處理非常相似

  • Exception 為所有異常類的基類

    try{// code to open and read a file}catch (System.IO.FileNotFoundException e){// handle the file not found exception first}catch (System.IO.IOException e){// handle any other IO exceptions second}catch{// a catch block without a parameter// handle all other exceptions last}finally{// this is executed whether or not an exception occurs// use to release any external resources}
  • ###C#高級技術(shù)

  • 屬性(類似Java中的批注)

    [System.Serializable()] //可以被序列化public class Employee {public int ID;public string Name; [System.NonSerialized()] public int Salary; }
  • 事件與委托(把方法當(dāng)成類型,傳遞方法)

  • LINQ查詢表達式

  • Lambda表達式

  • ##2、c#常用操作

    ###常用集合

  • ArrayList

    http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.arraylist_methods(v=vs.90).aspx

  • Hashtable

    http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.hashtable(v=vs.100).aspx

  • Dictionary

    http://msdn.microsoft.com/zh-cn/library/vstudio/ms132468(v=vs.90).aspx?###文件操作

  • 文件讀寫

    class TestFileIO{static void Main() {string fileName = "test.txt"; // a sample file name// Delete the file if it exists.if (System.IO.File.Exists(fileName)){System.IO.File.Delete(fileName);}// Create the file.using (System.IO.FileStream fs = System.IO.File.Create(fileName, 1024)) {// Add some information to the file.byte[] info = new System.Text.UTF8Encoding(true).GetBytes("This is some text in the file.");fs.Write(info, 0, info.Length);}// Open the file and read it back.using (System.IO.StreamReader sr = System.IO.File.OpenText(fileName)) {string s = "";while ((s = sr.ReadLine()) != null) {System.Console.WriteLine(s);}}}}
  • ###數(shù)據(jù)庫

  • 連接數(shù)據(jù)庫

    using System;using System.Data;using System.Data.SqlClient;class Program{static void Main(){string connectionString = "Data Source=(local);Initial Catalog=Northwind;"+ "Integrated Security=SSPI";string queryString = "SELECT CategoryID, CategoryName FROM dbo.Categories;";using (SqlConnection connection = new SqlConnection(connectionString)){SqlCommand command = connection.CreateCommand();command.CommandText = queryString;try{connection.Open();SqlDataReader reader = command.ExecuteReader();while (reader.Read()){Console.WriteLine("\t{0}\t{1}",reader[0], reader[1]);}reader.Close();}catch (Exception ex){Console.WriteLine(ex.Message);}}}}
  • ###Links

  • C#(針對Java開放人員)

    http://msdn.microsoft.com/zh-cn/library/ms228358(v=vs.90).aspx

  • C#編程指南

    http://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx

  • from:?https://github.com/xfans/Csharp_java_book/blob/master/csharp01.md

    總結(jié)

    以上是生活随笔為你收集整理的c#与java对比的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。