您的位置:首页 > 编程语言 > C语言/C++

C++/CLI与C#常用语法对比(转)

2004-10-16 12:35 281 查看

(转)Zeng Yi's Blog

Kenny Kerr 一篇名为C++: The Most Powerful Language for .NET Framework Programming<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

文章中的对比表,十分清晰的展示了版本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^ get()
    {
        return m_value;
    }
    void set(String^ value)
    {
        m_value = value;
    }
}
string Name
{
    get
    {
        return m_name;
    }
    set
    {
        m_name = value;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ c# string struct class object