Kenny Kerr 一篇名为C++: The Most Powerful Language for .NET Framework Programming文章中的对比表,十分清晰的展示了版本2语言中设计的简洁和与原生语言的接近。值得参考:
描述 |
C++/CLI |
C# |
创建引用类型的对象 |
ReferenceType^ h = gcnew ReferenceType; |
ReferenceType h = new ReferenceType(); |
创建值类型的对象 |
ValueType v(3, 4); |
ValueType v = new ValueType(3, 4); |
引用类型在堆栈上 |
ReferenceType h; |
N/A |
调用Dispose方法 |
ReferenceType^ h = gcnew ReferenceType; delete h; |
ReferenceType h = new ReferenceType(); ((IDisposable)h).Dispose(); |
实现Dispose方法 |
~TypeName() {} |
void IDisposable.Dispose() {} |
实现Finalize 方法 |
!TypeName() {} |
~TypeName() {} |
装箱(Boxing) |
int^ h = 123; |
object h = 123; |
拆箱(Unboxing) |
int^ hi = 123; int c = *hi; |
object h = 123; int i = (int) h; |
定义引用类型 |
ref class ReferenceType {}; ref struct ReferenceType {}; |
class ReferenceType {} |
定义值类型 |
value class ValueType {}; value struct ValueType {}; |
struct ValueType {} |
使用属性 |
h.Prop = 123; int v = h.Prop; |
h.Prop = 123; int v = h.Prop; |
定义属性 |
property String^ Name |
string Name |