C++CLI与C#常用语法对比

    xiaoxiao2021-12-14  19

    转载自:http://www.cnblogs.com/areliang/archive/2011/11/16/2251573.html

    十分清晰的展示了版本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;     } }

    转载请注明原文地址: https://ju.6miu.com/read-965617.html

    最新回复(0)