Rumah  >  Artikel  >  pembangunan bahagian belakang  >  C# Literals

C# Literals

WBOY
WBOYasal
2024-09-03 15:06:55505semak imbas

Literal dalam C# ialah nilai tetap yang digunakan oleh pembolehubah pratakrif yang tidak boleh diubah suai semasa pelaksanaan kod. Ini adalah bentuk mudah bagi nilai malar seperti pembolehubah lain, tetapi nilainya tidak boleh diubah. Nilai yang digunakan oleh pembolehubah boleh menjadi integer, perpuluhan, jenis terapung atau rentetan. Terdapat pelbagai jenis literal dalam C# dengan bentuk yang berbeza. Terdapat pelbagai jenis literal dalam C#.

  1. Huruf Integer
  2. String Literal
  3. Sastera Watak
  4. Literal Titik Terapung
  5. Huruf Boolean

5 Jenis Literal Teratas dalam C#

Berikut ialah pelbagai jenis literal dalam C#.

1. Huruf Integer

Harta bagi jenis integer boleh menjadi perlapanan, perpuluhan atau perenambelasan. Awalan digunakan untuk menentukan sama ada perpuluhan, perlapanan atau perenambelasan. U dan u juga digunakan sebagai akhiran dengan literal jenis integer untuk nombor tidak bertanda, dan l dan L digunakan untuk nombor panjang. Setiap literal adalah daripada jenis integer secara lalai.

  • Huruf Perpuluhan: 0-9 digit dibenarkan dalam jenis huruf perpuluhan. Tiada awalan diperlukan untuk jenis perpuluhan literal.

int x = 100;  // jenis perpuluhan

  • Oktal Literal: 0-7 digit dibenarkan dalam jenis oktal literal. 0 digunakan sebagai awalan untuk menentukan bentuk literal jenis oktal.

int x = 072;  // jenis oktal

  • Heksadesimal literal: Dalam jenis heksadesimal literal, digit dari 0- 9 dan aksara dari A-f dibenarkan. Huruf besar dan huruf kecil kedua-dua jenis aksara dibenarkan dalam kes ini. 0X atau 0x digunakan sebagai awalan untuk menentukan bentuk jenis heksadesimal literal.

int x = 0x123f;   // heksadesimal  jenis

2. Huruf Rentetan

Tersurat jenis rentetan disertakan dalam (“”)/ petikan berganda dan juga boleh dimulakan dengan @””. Garis panjang boleh dipecahkan kepada berbilang baris dengan literal rentetan dan dipisahkan menggunakan ruang kosong.

string s= "Hi";   // string literals

3. Huruf Huruf

Anda sertakan huruf jenis aksara dalam (“)/petikan tunggal. Terdapat tiga cara untuk menentukan literal aksara.

  • Petikan Tunggal: Huruf aksara boleh ditentukan sebagai aksara tunggal menggunakan petikan tunggal.
  • Perwakilan Unikod: Tersurat aksara boleh ditentukan menggunakan Perwakilan Unikod’ uxxxx,’ dengan xxxx ialah nombor perenambelasan.
  • Jujukan Melarikan Diri: Kami tahu beberapa aksara melarikan diri sebagai huruf aksara.
char c = '\n';

Berikut ialah beberapa literal urutan pelarian yang dijelaskan dengan maksudnya.

Escape Sequence Meaning
\ Character
Character
’’ Character
? Character
a Alert
b Backspace
n Newline
f Form feed
v Vertical tab
xhh Hexadecimal number
Jujukan Melarikan Diri
Maksud
\ Watak
Watak
’’ Watak
? Watak
a Makluman
b Ruang belakang
n Barisan baharu
f Suapan borang
v Tab menegak
xhh Nombor heksadesimal

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# Literals

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# Literals

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# Literals

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# Literals

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# Literals

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.

Atas ialah kandungan terperinci C# Literals. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Penunjuk dalam C#Artikel seterusnya:Penunjuk dalam C#