ホームページ  >  記事  >  バックエンド開発  >  C# リテラル

C# リテラル

WBOY
WBOYオリジナル
2024-09-03 15:06:55505ブラウズ

C# のリテラルは、コードの実行中に変更できない事前定義された変数によって使用される固定値です。これらは他の変数と同様に定数値の便利な形式ですが、その値は変更できません。変数で使用される値は、整数、10 進数、浮動小数点型、または文字列です。 C# には、さまざまな形式のさまざまな種類のリテラルがあります。 C# にはさまざまな種類のリテラルがあります。

  1. 整数リテラル
  2. 文字列リテラル
  3. 文字リテラル
  4. 浮動小数点リテラル
  5. ブールリテラル

C# のリテラルの上位 5 種類

次に、C# のさまざまな種類のリテラルを示します。

1.整数リテラル

整数型のリテラルは、8 進数、10 進数、または 16 進数のいずれかになります。プレフィックスは、10 進数、8 進数、または 16 進数のいずれであるかを指定するために使用されます。 U と u は、符号なし数値の整数型リテラルの接尾辞としても使用され、l と L は長い数値に使用されます。すべてのリテラルはデフォルトで整数型です。

  • 10 進数リテラル: 10 進数タイプのリテラルでは 0 ~ 9 の数字が使用できます。 10 進数型のリテラルには接頭辞は必要ありません。

int x = 100;  // 10進数型

  • 8 進リテラル: 8 進タイプのリテラルでは 0 ~ 7 桁が許可されます。 0 は、8 進数リテラルの形式を指定するための接頭辞として使用されます。

int x = 072;  // 8進数型

  • 16 進数リテラル: 16 進数タイプのリテラルでは、0 ~ 9 の数字と A ~ f の文字が許可されます。この場合、大文字と小文字の両方の種類の文字が許可されます。 0X または 0x は、16 進数タイプのリテラルの形式を指定するための接頭辞として使用されます。

int x = 0x123f;   // 16 進数の型

2.文字列リテラル

文字列型リテラルは ("")/ 二重引用符で囲み、@"" で始めることもできます。長い行は文字列リテラルを使用して複数の行に分割し、空白スペースを使用して区切ることができます。

string s= "Hi";   // string literals

3.文字リテラル

文字型リテラルを (")/一重引用符で囲みます。文字リテラルを指定するには 3 つの方法があります。

  • 一重引用符: 文字のリテラルは、一重引用符を使用して単一の文字として指定できます。
  • Unicode 表現: 文字リテラルは、Unicode 表現「uxxxx」を使用して指定できます。xxxx は 16 進数です。
  • エスケープ シーケンス: いくつかのエスケープ文字は 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 16 進数 テーブル>

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:C# のポインター次の記事:C# のポインター