What is the difference between a class and a datat

2019-04-05 07:35发布

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 what exactly this means?

10条回答
时光不老,我们不散
2楼-- · 2019-04-05 08:30

A class is just one of the data types available in C#:

Data Types:

  • Class
  • Struct
  • Enum
  • Delegate
  • Interface

NOTE: I may be missing a few in above list

查看更多
叛逆
3楼-- · 2019-04-05 08:32

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

查看更多
爷的心禁止访问
4楼-- · 2019-04-05 08:35

A class is a kind of data type. Other kinds of data types include pointer types and interfaces.

查看更多
虎瘦雄心在
5楼-- · 2019-04-05 08:37

C# is a strongly typed language; therefore every variable and object must have a declared type.

A data type can be described as being either:

A built-in data type, such as an int or char, or

A user-defined data type, such as a class or interface.

Data types can also be defined as being either:

Value Types (C# Reference), which store values, or

Reference Types (C# Reference), which store references to the actual data.

** Class is a user define data type. **

查看更多
登录 后发表回答