首页  >  文章  >  后端开发  >  C# 文字

C# 文字

WBOY
WBOY原创
2024-09-03 15:06:55730浏览

C# 中的文字是预定义变量使用的固定值,在代码执行期间无法修改。与其他变量一样,这些是常量值的便捷形式,但它们的值不能更改。变量使用的值可以是整数、小数、浮点类型或字符串。 C# 中有不同类型、不同形式的文字。 C# 中有多种类型的文字。

  1. 整数文字
  2. 字符串文字
  3. 字符文字
  4. 浮点文字
  5. 布尔文字

C# 中最常见的 5 种文字

以下是 C# 中不同类型的文字。

1.整数文字

整数类型的字面量可以是八进制、十进制或十六进制。前缀用于指定它是十进制、八进制还是十六进制。 U 和 u 也用作无符号数字的整数类型文字的后缀,l 和 L 则用于长数字。默认情况下,每个文字都是整数类型。

  • 十进制文字:十进制类型的文字允许包含 0-9 位数字。小数类型的文字不需要前缀。

int x = 100;  // 小数类型

  • 八进制文字: 八进制类型文字中允许使用 0-7 位数字。 0 用作前缀来指定八进制类型文字的形式。

int x = 072;  // 八进制类型

  • 十六进制文字:在十六进制类型的文字中,允许使用 0-9 的数字和 A-f 的字符。在这种情况下,允许使用大写和小写两种类型的字符。 0X 或 0x 用作前缀,指定十六进制类型文字的形式。

int x = 0x123f;   // 十六进制类型

2.字符串文字

字符串类型文字用(“”)/双引号括起来,也可以以@””开头。长行可以用字符串文字分成多行,并使用空格分隔。

string s= "Hi";   // string literals

3.字符文字

将字符类型文字括在 (“)/单引号中。指定字符文字的方法有三种。

  • 单引号: 可以使用单引号将字符的文字指定为单个字符。
  • Unicode 表示形式:可以使用 Unicode 表示形式“uxxxx”指定字符文字,其中 xxxx 是十六进制数字。
  • 转义序列:我们知道一些转义字符为 char 文字。
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
转义序列 含义 \ 字符 ’ 字符 '' 字符 ? 字符 a 提醒 b 退格键 n 换行符 f 换页 v 垂直标签 xhh 十六进制数 表>

4. Floating Point Literals

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.

5. Boolean Literals

In the Boolean type of literals, true and false will be the only two values.

Examples of C# Literals

Below are the examples that show how we can implement all the above literals in C#

Example #1 – Integer Literal

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:

C# 文字

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.

Example #2 – Floating Point Literal

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:

C# 文字

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.

Example #3 – 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)
{
char c = 'b';
char ch = '\u0071';
Console.WriteLine(c);
Console.WriteLine(ch);
Console.WriteLine("\nHello World\t!");
Console.ReadLine();
}
}
}

Output:

C# 文字

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.

Example #4 – String Literal

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:

C# 文字

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.

Example #5 – Boolean Type Literal

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:

C# 文字

Explanation: In the example provided, the implementation of Boolean type literals, which consist of two true or false values, can be seen.

Conclusion

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Pointers in C#下一篇:C# Commands