C#-Literale

WBOY
Freigeben: 2024-09-03 15:06:55
Original
694 Leute haben es durchsucht

Literale in C# sind der feste Wert, der von einer vordefinierten Variablen verwendet wird und während der Ausführung des Codes nicht geändert werden kann. Dabei handelt es sich wie bei anderen Variablen um die praktische Form konstanter Werte, deren Werte jedoch nicht geändert werden können. Der von einer Variablen verwendete Wert kann eine Ganzzahl, eine Dezimalzahl, ein Gleitkommawert oder eine Zeichenfolge sein. In C# gibt es verschiedene Arten von Literalen mit unterschiedlichen Formen. In C# gibt es verschiedene Arten von Literalen.

  1. Integer-Literale
  2. String-Literale
  3. Zeichenliterale
  4. Gleitkomma-Literale
  5. Boolesche Literale

Top 5 Arten von Literalen in C#

Im Folgenden sind die verschiedenen Arten von Literalen in C# aufgeführt.

1. Ganzzahlige Literale

Das Literal vom Typ Integer kann oktal, dezimal oder hexadezimal sein. Mit dem Präfix wird angegeben, ob es sich um eine Dezimal-, Oktal- oder Hexadezimalzahl handelt. U und u werden auch als Suffix mit ganzzahligen Literalen für vorzeichenlose Zahlen verwendet, und l und L werden für lange Zahlen verwendet. Jedes Literal ist standardmäßig vom Typ Integer.

  • Dezimalliterale: Im Dezimaltyp von Literalen sind 0-9 Ziffern zulässig. Für den Dezimaltyp von Literalen ist kein Präfix erforderlich.

int x = 100;  // Dezimaltyp

  • Oktale Literale: 0-7 Ziffern sind im oktalen Literaltyp zulässig. 0 wird als Präfix verwendet, um die Form von Literalen vom Oktaltyp anzugeben.

int x = 072;  // Oktaltyp

  • Hexadezimale Literale: Im hexadezimalen Literaltyp sind Ziffern von 0 bis 9 und Zeichen von A bis f zulässig. In diesem Fall sind sowohl Groß- als auch Kleinbuchstaben zulässig. 0X oder 0x wird als Präfix verwendet, um die Form des hexadezimalen Literaltyps anzugeben.

int x = 0x123f;   // Hexadezimaltyp

2. String-Literale

Die String-Typ-Literale sind in („“)/doppelte Anführungszeichen eingeschlossen und können auch mit @“ begonnen werden. Lange Zeilen können mit String-Literalen in mehrere Zeilen unterteilt und durch Leerzeichen getrennt werden.

string s= "Hi";   // string literals
Nach dem Login kopieren

3. Zeichenliterale

Sie schließen die Zeichentypliterale in (“)/einfache Anführungszeichen ein. Es gibt drei Möglichkeiten, Zeichenliterale anzugeben.

  • Einfaches Anführungszeichen: Die Literale von Zeichen können als einzelnes Zeichen mit einem einfachen Anführungszeichen angegeben werden.
  • Unicode-Darstellung: Zeichenliterale können mithilfe der Unicode-Darstellung „uxxxx“ angegeben werden, wobei xxxx die Hexadezimalzahlen sind.
  • Escape-Sequenz: Wir kennen einige Escape-Zeichen als Char-Literale.
char c = '\n';
Nach dem Login kopieren

Im Folgenden werden einige Escape-Sequenz-Literale mit ihrer Bedeutung erklärt.

Escape Sequence Meaning
\ Character
Character
’’ Character
? Character
a Alert
b Backspace
n Newline
f Form feed
v Vertical tab
xhh Hexadecimal number
Escape-Sequenz
Bedeutung
\ Charakter
Charakter
’’ Charakter
? Charakter
a Alarm
b Rücktaste
n Neue Zeile
f Formularvorschub
v Vertikale Registerkarte
xhh Hexadezimale Zahl

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();
}
}
}
Nach dem Login kopieren

Output:

C#-Literale

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();
}
}
}
Nach dem Login kopieren

Output:

C#-Literale

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();
}
}
}
Nach dem Login kopieren

Output:

C#-Literale

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();
}
}
}
Nach dem Login kopieren

Output:

C#-Literale

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();
}
}
}
Nach dem Login kopieren

Output:

C#-Literale

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.

Das obige ist der detaillierte Inhalt vonC#-Literale. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!