从vc++ 到 visual c#.net

本文探讨了从VC++转向Visual C#.NET的过程中,如何理解和适应两者在设计与实现上的异同。主要涉及WinForm、Object基础类、事件、继承关系、多线程同步和Web服务等方面,阐述C#.NET的特性和优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从vc++ 到 visual c#.net

lucky2all@gmail.com

抛去他们之间概念与基础层面的差别,只是从设计,实现角度来比较他们之间的异同点,
能够从vc++顺利的过渡到C#.net.


在mfc中我们一般从窗口 CWnd, 基础类CObject的几大特性,消息机制,对象之间的继承关系,多线程的同步互斥来理解
mfc中间的机理,然后到模仿,设计,编写自己的类库。

在C#中,我们同样也可以从 WinForm, Object基础类,事件,对象之间的关系,多线程的同步互斥来学习。

1. System.Windows.Forms.Form
   
Toolbox/Properties的配合,使设计满意的Form变得简单。

2. Object

可以从他的metadata中的summary来理解,至于c#为何在他的基础类中提供这些方法,我们可以在实践中体会。
namespace System
{
    // Summary:
    //     Supports all classes in the .NET Framework class hierarchy and provides low-level
    //     services to derived classes. This is the ultimate base class of all classes
    //     in the .NET Framework; it is the root of the type hierarchy.
    [Serializable]
    [ComVisible(true)]
    [ClassInterface(2)]
    public class Object
    {
        // Summary:
        //     Initializes a new instance of the System.Object class.
        [ReliabilityContract(3, 1)]
        public Object();

        // Summary:
        //     Determines whether the specified System.Object is equal to the current System.Object.
        //
        // Parameters:
        //   obj:
        //     The System.Object to compare with the current System.Object.
        //
        // Returns:
        //     true if the specified System.Object is equal to the current System.Object;
        //     otherwise, false.
        public virtual bool Equals(object obj);
        //
        // Summary:
        //     Determines whether the specified System.Object instances are considered equal.
        //
        // Parameters:
        //   objB:
        //     The second System.Object to compare.
        //
        //   objA:
        //     The first System.Object to compare.
        //
        // Returns:
        //     true if objA is the same instance as objB or if both are null references
        //     or if objA.Equals(objB) returns true; otherwise, false.
        public static bool Equals(object objA, object objB);
        //
        // Summary:
        //     Serves as a hash function for a particular type. System.Object.GetHashCode()
        //     is suitable for use in hashing algorithms and data structures like a hash
        //     table.
        //
        // Returns:
        //     A hash code for the current System.Object.
        public virtual int GetHashCode();
        //
        // Summary:
        //     Gets the System.Type of the current instance.
        //
        // Returns:
        //     The System.Type instance that represents the exact runtime type of the current
        //     instance.
        public Type GetType();
        //
        // Summary:
        //     Creates a shallow copy of the current System.Object.
        //
        // Returns:
        //     A shallow copy of the current System.Object.
        protected object MemberwiseClone();
        //
        // Summary:
        //     Determines whether the specified System.Object instances are the same instance.
        //
        // Parameters:
        //   objB:
        //     The second System.Object to compare.
        //
        //   objA:
        //     The first System.Object to compare.
        //
        // Returns:
        //     true if objA is the same instance as objB or if both are null references;
        //     otherwise, false.
        [ReliabilityContract(3, 2)]
        public static bool ReferenceEquals(object objA, object objB);
        //
        // Summary:
        //     Returns a System.String that represents the current System.Object.
        //
        // Returns:
        //     A System.String that represents the current System.Object.
        public virtual string ToString();
    }
}


3. 继承关系

C# 中不存在多继承,
 class A
{
}

class B{}

class C : A, B {}
是不会在C#中出现的,在C++的多继承一般也用的很少,理解起来也比较麻烦。

但C#中,

如果class B 为一个interface
interface _B {}

则子类c可以这样定义 class c : A, _B {}, 这样也达到了多继承的效果。

c#中public, protected, private的继承关系与c++是一致的。

static 函数/变量 的用法也相同。

virtual 的用法也是一致的,纯虚的定义有点差别c#中是 abstract virtual method(); c++中是 virtual method() =0;

C#中类可以被定为sealed属性类,表明他不能再被继承了,
这有点类似c++中如果一个类的析构函数如果不是virtual, 那么它就告诉你,最好不要继承我了。



4.事件

c#中可以通过 +=, -=来添加,去除多个事件处理体,非常方便。

5.线程同步/互斥

c#中 Thread来创建一个线程

Thread t1 = new Thread (new ThreadStart (Func))

通过 ReaderWriterLock,  Mutex, Monitor, Interlocked, AutoResetEvent等来同步互斥,

与c++的 mutex, event, Critical_Section等类似,不过更清晰。


6.Web service
C#可作为web的设计语言,vs2005在设计/发布方面都有很好的支持。

7.Enterprise lib
open source, 提炼的企业级开发库,很有用,根据需要自己定制。

8.其它

clickonce发布,ado.net/smartclient等,慢慢体验。 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值