在C#是术语;原始"和"文本"互换?术语、原始、文本、QUOT

2023-09-03 11:03:37 作者:喜糖

一个讨论今天早些时候使我怀疑我的primtives和文字的理解是否正确。

A discussion earlier today led me to question whether or not my understanding of primtives and literals is correct.

我的理解是,文字类型​​是具体是哪个可以使用符号分配的值人力和编译器可以理解没有特定的类型声明类型:

My understanding is that a literal type is specifically a type which can have a value assigned using a notation that both human and compiler can understand without specific type declarations:

var firstName = "John"; // "John" is literal

var firstName = (string)"John"; // *if* the compiler didn't understand that "John"
                                // was a literal representation of a string then I
                                // would have to direct it as such

我的原语的理解是,他们在本质上是元素数据类型,编译器能够理解,如int:

My understanding of primitives is that they are essentially the elemental datatypes which the compiler can understand, such as int:

int age = 25;

...文字可以是非原始的,如VB9对XML文本的支持。非现实世界的例子是,如果Sy​​stem.Drawing.Point可以用文字来分配的:

...a literal could be non-primitive, such as VB9's support for XML literals. A non-real world example would be if System.Drawing.Point could be assigned with literals:

Point somePoint = 2,2; // both X and Y are primitive values, however Point is a
                       // composite value comprised of two primitive values

最后(这是问题,这反过来导致我问上面的问题): 我的理解是,一种类型是简单的还是文字没有直接关系,无论是数值或引用类型。

在Word中,有关表格的叙述,以下说法正确的是 A. 文本和表可以互相转化 B. 可以将文本转化为表,但表不能转成文本 C. 文本和表不能互相转化 D. 可以将表转化为文本

Finally (and this is the question that in turn led me to ask the above questions): My understanding is that whether a type is primitive or literal there is no direct relation to whether it is a Value or Reference type.

例如System.String是支持文字引用类型。自定义的结构是不支持文字复合值类型。

For example System.String is a reference type which supports literals. Custom-defined structures are composite value types which do not support literals.

我的理解(如果不是我的解释),正确的大部分?的

Is my understanding (if not my explanation) correct for the most part?

更新:的感谢伟大的信息和对话!任何人发现这种情况,请务必阅读评论,以及回答,有一些伟大的澄清US $ p $垫周围还有一些有趣的副作用说明。

Update: Thanks for the great info and conversations! To anyone finding this, make sure to read the comments as well as answers, there's some great clarifications spread around as well as a few interesting side-notes.

顺便说一句:这是一个胜负难料之间回答真的是值得让绿色的大检查。我给它的不幸downvoted答案,不仅包含一个像样的答案,但大量的澄清和信息的评论线程。为了公平起见没有在这里,人们最好的答案,有至少有三个:)

btw: it's a toss-up between which answer really is deserving to get the big green check. I'm giving it to the unfortunately downvoted answer which contains not only a decent answer but lots of clarification and info in the comments thread. To be fair there isn't one best answer here, there's at least three :)

推荐答案

我想有一件事你没有提的是空间和分配。基元是值类型和被分配在栈上(只要它们不与对象相关联)除了字符串类型,你提到的(String类分配的堆空间)。

I guess one thing you did not mention is space and allocation. Primitives are value types and are allocated on the stack (as long as they are not associated with an object) except for the string type as you mentioned (the string class allocates its space on the heap).

虽然对象本身包含元有存储所在的实际的对象被分配时,这是对堆

Although objects themselves contain primitives there storage resides where the actual object is allocated, which is on the heap.

其他然后,你的发言是pretty的写得很好。你有,我错过了一个具体问题:?)

Other then that your statement is pretty well written. Do you have a specific question that I missed :)?