http://www.ondotnet.com/pub/a/dotnet/2001/06/14/csharp_4_java.html
1. 簡單類型
C#中的primitive type也是object的子類,因此可以直接調用ToString 或 GetType。
如Int是System.Int32的假名,後者繼承了System.Object
注意:C#中簡單類型仍然是pass-by-value
2. 常量定義
Java:static final
C#:
const int TWO = 2;
the keyword “readonly”(在聲明處或建構函式中可以賦值,可以是動態賦值)
3. switch語句中的差異
C#中不允許fall-through, 否則會有編譯錯誤。
In C#, an explicit break or goto case to a
different case is required somewhere in the case to explicitly define the
control flow.
如
switch(n)
{
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
4.特有的迴圈語句 foreach
實現了System.Collections.IEnumerable
介面的object都可以使用foreach, 如array.
5.C#中,所有的異常都是run-time exceptions; 都繼承了System.Exception.
6. 函數參數修飾符: params, ref, out
(1) params(其實只是為了簡化把數組作為函數參數的過程)
方法的最後一個參數如果是數組,可以在這個參數前加params關鍵詞。
public void
methodCaller( params int[] a );
and the method can be called with any of
methodCaller( 1 );
methodCaller( 1, 2,
3, 4, 5 );
Inside methodCaller, the parameters can be accessed through the
"a" array defined.
7. Property(對應於Java class裡的一個屬性及getter/setter)
定義
private int property;
public int Property {
get {
return this.property;
}
set {
// value is an implicit
variable generated by the
// compiler to represent the parameter
this.property = value;
}
}
使用
int currentValue = Property;
Property = newValue;
8. Extra Accessibility Modifier
internal - this is a new one to Java programmers, as this means that a
member is accessible from the entire assembly. All the objects you define
inside a .cs file (you can define more than one object inside the .cs file,
unlike Java, where you can usually define only one object) have a handle to the
internal member.
protected
internal - think of this one as the union of
protected and internal, as the item is is modifying is either protected or
internal. This item can be accessed from the entire assembly, or within objects
which derive from this class.
9. C#中的類繼承文法
class D : B, C {
// B,C可以是class,也可以時interface;如果是一個是class,一個是interface,class要放在interface前面。
}
10 C#中的Struct
*
structs are passed by value instead of by reference
*
structs cannot extend anything (nor can anything extend them) -- they may,
however, implement interfaces
*
structs may not define a constructor with no parameters
*
structs that define constructors with parameters must explicitly define all
fields before they return control to the caller
11. C#中的base對應Java中的super
12. Overriding Methods
All methods in an object are
"final" (in the Java sense of the word) by default. In order for a
method to be overridden by an inheriting object, the original method needs to
be marked "virtual" and all methods overriding the virtual method in
the inherited classes must be marked "override." For example:
public class A : Object {
public virtual void toOverride() { }
}
public class B : A {
public override void toOverride() { }
}
is required in C# for the compiler not to
complain when defining the inheriting class.
13. 類型轉換
public class FloorDouble : Object {
private double value;
public FloorDouble( double value ) {
this.value = Math.Floor( value );
}
public double Value {
get {
return this.value;
}
}
public static explicit operator
FloorDouble( double value ) {
return new FloorDouble( value );
}
public static implicit operator
double( FloorDouble value ) {
return value.Value;
}
}
// this class can be used by
FloorDouble fl = (FloorDouble)10.5
double d = fl;
14 操作符重載
15. C#裡的namespace
(1)
不能引入單獨的一個類
(2)
alias for long namespace
using short = a.really.long.namespace.theClass;
(3)不與檔案系統中的結構對應