What is the difference between a class and a datat

2019-04-05 08:17发布

问题:

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?

回答1:

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. **



回答2:

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



回答3:

class is a object oriented programming concept, and datatype is generic to all programming paradigm. Class is a datatype but datatype are not class



回答4:

This article from the MSDN might be of some use in this regard. It tries to explain what Objects, Classes and Structs are in terms of C#.



回答5:

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



回答6:

A class is a type of a data type. It allows you to declare a variable along with its datatype. E.g.:

class big  
{  
    public int a;  
    .......  
    ......    
}    

Here, int is a data type and big is the class name.

Hope you got little idea about it!



回答7:

A class is a data type. A datatype is a concept, it's not a class. That is to say, you look at something and say to yourself "Hmm yes, that is so-and-so type of data", or "That is this datatype". Then you say "Specifically, the datatype is List<..>", and so on.



回答8:

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



回答9:

a class is a data type if a user creates a class, it is known as user defined data-type.



回答10:

All (or almost) all programming languages have a notion of a datatype. A datatype can be things like: Integers, doubles, booleans etc.

C# and other languages allow us to define our own "custom" datatypes. That's where classes come in. Custom datatypes in OO languages are called classes and when we define such classes it is the type definition or type.

Take a look at this Wikipedia page for more information