What is the difference between a class and a datatype?

前端 未结 10 1059
广开言路
广开言路 2021-02-13 15:30

I have heard the following statement:

We can say class is a datatype or a datatype is one type of class.

Can anyone explain to me wh

10条回答
  •  有刺的猬
    2021-02-13 15:50

    Classes are Reference Types.

    A Data Type is a value type if it holds the data within its own memory allocation.

    Reference types are allocated on the heap, and memory management is handled by the garbage collector. Value types are allocated on the stack or inline and are deallocated when they go out of scope. In general, value types are cheaper to allocate and deallocate.

    Like for example

    class Person
    {
       string name;
    }
    

    In this the class Person is reference type and name is value type i.e data type.

    struct Person
    {
       string name;
    }
    

    In this the struct Person is value type and also name is value type i.e both are data type.

    A data type is a value type if it holds the data within its own memory allocation. A reference type contains a pointer to another memory location that holds the data.

    In reference to MSDN article on Classes and Structures and also MSDN article on Reference Type and Value Type

提交回复
热议问题