Are methods also serialized along with the data members in .NET?

前端 未结 4 2026
执念已碎
执念已碎 2020-12-06 11:29

The title is obvious, I need to know if methods are serialized along with object instances in C#, I know that they don\'t in Java but I\'m a little new to C#. If they don\'t

4条回答
  •  暖寄归人
    2020-12-06 12:12

    It may be easier to understand if you've learned C. A class like

    class C
    {
        private int _m;
        private int _n;
    
        int Meth(int p)
        {
           return _m + _n + p;
        }
    }
    

    is essentially syntactic sugar for

    typedef struct
    {
       int _m;
       int _n;
       // NO function pointers necessary
    } C;
    
    void C_Meth(C* obj, int p)
    {
       return obj->_m + obj->_n + p;
    }
    

    This is essentially how non-virtual methods are implemented in object-oriented languages. The important thing here is that methods are not part of the instance data.

提交回复
热议问题