简体   繁体   English

类和数据类型有什么区别?

[英]What is the difference between a class and a datatype?

I have heard the following statement: 我听到以下声明:

We can say class is a datatype or a datatype is one type of class. 我们可以说class是数据类型或数据类型是一种类。

Can anyone explain to me what exactly this means? 任何人都可以向我解释这究竟是什么意思?

C# is a strongly typed language; C#是一种强类型语言; 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 内置数据类型,例如int或char,或

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 值类型(C#参考),用于存储值或

Reference Types (C# Reference), which store references to the actual data. 引用类型(C#引用),用于存储对实际数据的引用。

** Class is a user define data type. **类是用户定义数据类型。 ** **

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 ie data type. 在这个类Person是引用类型, name是值类型即数据类型。

struct Person
{
   string name;
}

In this the struct Person is value type and also name is value type ie both are data type. 在这里,struct Person是值类型, name也是值类型,即两者都是数据类型。

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 有关类和结构的 MSDN文章,以及有关引用类型和值类型的MSDN文章

class is a object oriented programming concept, and datatype is generic to all programming paradigm. class是面向对象的编程概念,而数据类型对于所有编程范例都是通用的。 Class is a datatype but datatype are not class Class是数据类型,但数据类型不是类

This article from the MSDN might be of some use in this regard. 来自MSDN的这篇文章在这方面可能有一些用处。 It tries to explain what Objects, Classes and Structs are in terms of C#. 它试图用C#来解释对象,类和结构是什么。

A class is just one of the data types available in C#: 类只是C#中可用的数据类型之一:

Data Types: 数据类型:

  • Class
  • Struct 结构
  • Enum 枚举
  • Delegate 代表
  • Interface 接口

NOTE: I may be missing a few in above list 注意:我可能会遗漏上面列表中的一些内容

A class is a type of a data type. 类是一种数据类型。 It allows you to declare a variable along with its datatype. 它允许您声明变量及其数据类型。 Eg: 例如:

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

Here, int is a data type and big is the class name. 这里, int是数据类型, big是类名。

Hope you got little idea about it! 希望你对它一无所知!

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. 然后你说“具体来说,数据类型是List <..>”,依此类推。

A class is a kind of data type. 类是一种数据类型。 Other kinds of data types include pointer types and interfaces. 其他类型的数据类型包括指针类型和接口。

如果用户创建类,则类是数据类型,它被称为用户定义的数据类型。

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. C#和其他语言允许我们定义自己的“自定义”数据类型。 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. 这就是类的用武之地.OO语言中的自定义数据类型称为类,当我们定义这些类时,它是类型定义或类型。

Take a look at this Wikipedia page for more information 请查看此Wikipedia页面以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataType(DataType.PhoneNumber)和PhoneAttribute有什么区别 - What's the difference between DataType(DataType.PhoneNumber) & PhoneAttribute 初始化类和继承类有什么区别 - What is difference between initiate a class and inherit a class “使用课程”和“呼叫课程”有什么区别 - What is the difference between “use a class” and “call a class” 密封班和私人班有什么区别? - What is the difference between a sealed class and a private class? 静态类和普通类有什么区别? - What is the difference between a static class and a normal class? ConfigurationManager类和Settings类之间有什么区别? - What is the difference between ConfigurationManager class and Settings class? 命名空间,类,对象和实例之间有什么区别? - What is the difference between a namespace, a class, an object and an instance? MAUI class 库和普通的 class 库有什么区别? - What's the difference between a MAUI class library and a normal class library? &#39;DES&#39;类和&#39;DESCryptoServiceProvider&#39;类有什么区别? - What's the difference between the 'DES' class and The 'DESCryptoServiceProvider' class? 简单基类和抽象类有什么区别? - What is the difference between a simple base class and abstract class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM