C# 中的文字是預先定義變數使用的固定值,在程式碼執行期間無法修改。與其他變數一樣,這些是常數值的便捷形式,但它們的值不能更改。變數使用的值可以是整數、小數、浮點類型或字串。 C# 中有不同類型、不同形式的文字。 C# 中有多種類型的文字。
以下是 C# 中不同類型的文字。
整數類型的字面量可以是八進位、十進位或十六進位。前綴用於指定它是十進制、八進制還是十六進制。 U 和 u 也用作無符號數字的整數類型文字的後綴,l 和 L 則用於長數字。預設情況下,每個文字都是整數類型。
int x = 100; // 小數型別
int x = 072; // 八進位型別
int x = 0x123f; // 十六進位型別
字串類型文字用(“”)/雙引號括起來,也可以以@””開頭。長行可以用字串文字分成多行,並使用空格分隔。
string s= "Hi"; // string literals
將字元類型文字括在 (“)/單引號中。指定字元文字的方法有三種。
char c = '\n';
以下是一些轉義序列文字及其意義的解釋。
Escape Sequence | Meaning |
\ | Character |
’ | Character |
’’ | Character |
? | Character |
a | Alert |
b | Backspace |
n | Newline |
f | Form feed |
v | Vertical tab |
xhh | Hexadecimal number |
In the floating type of literal, there is an integer part, a fractional part, a decimal part, and an exponent part. The floating type literal is of double type. You can use F or f as a suffix to specify the value because you cannot assign it directly to the float variable.
In the Boolean type of literals, true and false will be the only two values.
Below are the examples that show how we can implement all the above literals in C#
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Literals { class Program { static void Main(string[] args) { int x = 212; // decimal literal int y = 0145; // octal literal int z = 0x4b; // hexadecimal literal Console.WriteLine(x); Console.WriteLine(y); Console.WriteLine(z); Console.ReadLine(); } } }
Output:
Explanation: In the above example, there are various forms of integer-type literals. You use no prefix for the decimal form, use 0 to specify the octal form, and use 0x to specify the hexadecimal number. Using prefixes, we can define the form of integer type literal. In this code, first, there is a literal of decimal type with no prefix, a second type is an octal form with 0 as a prefix, and last, we have a hexadecimal type with 0x as a prefix.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Literals { class Program { static void Main(string[] args) { double x = 187.231; double y = 0141.361; double z = 374159E-4F; Console.WriteLine(x); Console.WriteLine(y); Console.WriteLine(z); Console.ReadLine(); } } }
Output:
Explanation: The above example implements floating-point literals. It can be a decimal number, fractional, or any exponent. So we can represent it either in decimal or in exponential form. The floating type literal is of double type. You can use F or f as a suffix to specify the value because you cannot assign it directly to the float variable.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Literals { class Program { static void Main(string[] args) { char c = 'b'; char ch = '\u0071'; Console.WriteLine(c); Console.WriteLine(ch); Console.WriteLine("\nHello World\t!"); Console.ReadLine(); } } }
Output:
Explanation: The above example implements character-type literals. The above code shows all three forms of character type. We can specify the character using a single quote, Unicode representation, and escape sequence. We have multiple types of escape characters with their meanings. In this code, the first single quote character is specified where the second one has Unicode representation, and then, at last, we have escape form type of character literals.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Literals { class Program { static void Main(string[] args) { String s1 = "This is C# programming"; String s2 = @"This is C# programming"; Console.WriteLine(s1); Console.WriteLine(s2); Console.ReadLine(); } } }
Output:
Explanation: The above example implements string literals. There are two ways to specify string literals, as shown in the code. To implement the string, use double quotes first, then follow with the @ symbol.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Literals { class Program { static void Main(string[] args) { bool x = true; bool y = false; Console.WriteLine(x); Console.WriteLine(y); Console.ReadLine(); } } }
Output:
Explanation: In the example provided, the implementation of Boolean type literals, which consist of two true or false values, can be seen.
So literals are the fixed values. In C#, there are different types of literals with specific form types. It can be of integer, Boolean, string, or a character literal.
以上是C# 文字的詳細內容。更多資訊請關注PHP中文網其他相關文章!