C# “字面”这个词是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/485119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the word "literal" mean?
提问by prodev42
What does the word "literal" mean when used in context such as literal strings and literal values?
“文字”一词在文字字符串和文字值等上下文中使用时是什么意思?
What is the difference between a literal value and a value?
文字值和值有什么区别?
采纳答案by mbeckish
A literal is "any notation for representinga value within source code" (wikipedia)
文字是“在源代码中表示值的任何符号”(维基百科)
(Contrast this with identifiers, which referto a value in memory.)
(将此与identifiers形成对比,它指的是内存中的值。)
Examples:
例子:
"hey"
(a string)false
(a boolean)3.14
(a real number)[1,2,3]
(a list of numbers)(x) => x*x
(a function)/^1?$|^(11+?)\1+$/
(a regexp)
"hey"
(一个字符串)false
(一个布尔值)3.14
(实数)[1,2,3]
(数字列表)(x) => x*x
(一个功能)/^1?$|^(11+?)\1+$/
(一个正则表达式)
Some things that are not literals:
一些不是文字的东西:
std::cout
(an identifier)foo = 0;
(a statement)1+2
(an expression)
std::cout
(标识符)foo = 0;
(一份声明)1+2
(一种表达)
回答by Ed S.
A literal is an assignment to an explicit value, such as
文字是对显式值的赋值,例如
int i = 4; // i is assigned the literal value of '4'
int j = i // j is assigned the value of i. Since i is a variable,
//it can change and is not a 'literal'
EDIT: As pointed out, assignment itself has nothing to do with the definition of a literal, I was using assignment in my example, but a literal can also be passed into a method, etc.
编辑:正如所指出的,赋值本身与文字的定义无关,我在我的例子中使用了赋值,但文字也可以传递给方法等。
回答by Anthony D
A literal is when you put it in the code. So a string literal is
文字是当您将其放入代码中时。所以字符串文字是
string s = "SomeText";
This is as opposed to building the string, or taking it in as a parameter.
这与构建字符串或将其作为参数相反。
回答by Joel Coehoorn
A literal is a value that has been hard-coded directly into your source.
文字是直接硬编码到源中的值。
For example:
例如:
string x = "This is a literal";
int y = 2; // so is 2, but not y
int z = y + 4; // y and z are not literals, but 4 is
int a = 1 + 2; // 1 + 2 is not a literal (it is an expression), but 1 and 2 considered separately are literals
Some literals can have a special syntax, so you know what type the literal is:
一些文字可以有特殊的语法,所以你知道文字是什么类型:
//The 'M' in 10000000M means this is a decimal value, rather than int or double.
var accountBalance = 10000000M;
What sets them apart from variables or resources is that the compiler can treat them as constants or perform certain optimizations with code where they are used, because it's certain they won't change.
它们与变量或资源的区别在于,编译器可以将它们视为常量或对使用它们的代码执行某些优化,因为它们肯定不会改变。
回答by mbeckish
Quick example:
快速示例:
int my_int_var = 723;
int my_int_var = 723;
723 - This set of characters refers to a literalinteger value.
723 - 这组字符指的是文字整数值。
my_int_var - This set of characters refers to a variableinteger value.
my_int_var - 这组字符指的是一个可变整数值。
回答by Bill the Lizard
A literal value is avalue, but a value could also be stored in a variable. In the statement
文字值是一个值,但值也可以存储在变量中。在声明中
string str = "string literal";
there's a string variable (str) and a string literal. After the statement is executed they both have the same value.
有一个字符串变量 (str) 和一个字符串文字。语句执行后,它们都具有相同的值。
Be aware that in many languages the variable and the literal value don't necessarily even have to be the same type. For example:
请注意,在许多语言中,变量和文字值甚至不必是相同的类型。例如:
int a = 1.0;
The literal value above is a floating point type. The value will be coerced by the compiler to fit into the int variable.
上面的文字值是一个浮点类型。该值将由编译器强制以适合 int 变量。
For another example, in the first line of C++ code above the type of a string literal isn't actually the library type string
at all. To maintain backwards compatibility with C, string literals in C++ are char arrays.
再举一个例子,在上面的 C++ 代码的第一行中,字符串文字的类型实际上根本不是库类型string
。为了保持与 C 的向后兼容性,C++ 中的字符串文字是 char 数组。
回答by Timothy Carter
I have heard string literals used casually to refer to what the C# specificationactually refers to as verbatim string literals. A regular string literal allows for the escaping of certain characters (prefixed by a ), like \t for a tab. A verbatim string literal is prepended by @ and processed verbatim, \ has no special meaning.
我听说过随意使用字符串文字来指代C# 规范实际所指的逐字字符串文字。常规字符串文字允许转义某些字符(以 a 为前缀),例如用于制表符的 \t。逐字字符串文字以@ 开头并逐字处理,\ 没有特殊含义。
//regular
string regular = "\thello world";
//verbatim
string verbatim = @"C:\";
//the regular equivalent of this would be "C:\"
回答by Matthew Crumley
A literal is when you include the value in the source code (as opposed to referencing a variable or a constant). For example:
文字是指在源代码中包含值(而不是引用变量或常量)。例如:
int result = a + 5; // a is a variable with a value, 5 is a literal
string name = "Jeff Atwood"; // name is a variable initialized
// with the string literal, "Jeff Atwood"
int[] array = new int[] {1, 2, 3}; // C# has array literals (this is actually three
// int literals within an array literal)
If the literal represents some quantity, like a physical constant, it's better to give it a name instead of writing the same literal everywhere you need it. That way, when you are reading the source code, you know what the number means, which is usually more important than its value (which could change anyways).
如果文字代表某个数量,比如物理常数,最好给它一个名字,而不是在任何需要它的地方写相同的文字。这样,当您阅读源代码时,您就会知道数字的含义,这通常比它的值(无论如何都可能改变)更重要。
const int maxUsers = 100;
const double gravitationalAcceleration = 9.8;
Generally, the only numeric literals I use (besides to initialize constants like above) are 0 or 1, and sometimes 2 if I'm skipping every other item in a loop. If the meaningof the number is more important than its actual value(it usually is), its better to name it.
通常,我使用的唯一数字文字(除了像上面那样初始化常量)是 0 或 1,如果我跳过循环中的所有其他项目,有时是 2。如果数字的含义比它的实际值更重要(通常是),最好给它命名。
回答by HasaniH
Generally when someone uses the word literal they mean the value is decipherable from the code (text) as shown in many of the examples in other posts.
通常,当有人使用字面量这个词时,他们的意思是可以从代码(文本)中破译该值,如其他帖子中的许多示例所示。
Another common usage is for values that are converted to immediate values in assembly. These are values that are inserted directly into the machine instruction rather than requiring register loads.
另一种常见用法是用于在汇编中转换为立即数的值。这些是直接插入机器指令而不需要寄存器加载的值。
回答by Koray Tugay
A literal is "source code representation of data".
文字是“数据的源代码表示”。