>  기사  >  백엔드 개발  >  C# 리터럴

C# 리터럴

WBOY
WBOY원래의
2024-09-03 15:06:55504검색

C#의 리터럴은 코드 실행 중에 수정할 수 없는 미리 정의된 변수에 사용되는 고정 값입니다. 이는 다른 변수와 마찬가지로 상수 값의 편리한 형식이지만 값을 변경할 수는 없습니다. 변수에 사용되는 값은 정수, 소수, 부동 유형 또는 문자열일 수 있습니다. C#에는 다양한 형식의 다양한 유형의 리터럴이 있습니다. C#에는 다양한 유형의 리터럴이 있습니다.

  1. 정수 리터럴
  2. 문자열 리터럴
  3. 문자 리터럴
  4. 부동 소수점 리터럴
  5. 부울 리터럴

C#의 상위 5가지 리터럴 유형

다음은 C#의 다양한 리터럴 유형입니다.

1. 정수 리터럴

정수형의 리터럴은 8진수, 10진수, 16진수가 될 수 있습니다. 접두사는 10진수, 8진수, 16진수인지 지정하는 데 사용됩니다. U와 u는 부호 없는 숫자에 대해 정수형 리터럴의 접미사로도 사용되며, l과 L은 긴 숫자에 사용됩니다. 모든 리터럴은 기본적으로 정수 유형입니다.

  • 십진수 리터럴: 십진수 리터럴에는 0~9자리가 허용됩니다. 10진수 유형의 리터럴에는 접두사가 필요하지 않습니다.

int x = 100;  // 십진수 유형

  • 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. 문자 리터럴

문자 유형 리터럴은 (“)/작은따옴표로 묶습니다. 문자 리터럴을 지정하는 방법에는 세 가지가 있습니다.

  • 작은따옴표: 문자 리터럴은 작은따옴표를 사용하여 단일 문자로 지정할 수 있습니다.
  • 유니코드 표현: 문자 리터럴은 유니코드 표현 'uxxxx'를 사용하여 지정할 수 있습니다. 여기서 xxxx는 16진수입니다.
  • 이스케이프 시퀀스: 일부 이스케이프 문자는 문자 리터럴로 알려져 있습니다.
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
이스케이프 시퀀스 의미 \ 문자 ' 문자 '' 문자 ? 문자 아 경고 ㄴ 백스페이스 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C#의 포인터다음 기사:C#의 포인터