📜  C#|字面量

📅  最后修改于: 2021-05-30 01:15:35             🧑  作者: Mango

固定值称为字面量 。字面量是变量使用的值。值可以是整数,浮点数或字符串等。

// Here 100 is a constant/literal.
int x = 100; 


字面量可以是以下类型:

  • 整数字面量
  • 浮点字面量
  • 字符字面量
  • 字符串字面量
  • 空字面量
  • 布尔字面量

整数字面量:整数类型的字面量是知道的整数字面量。它可以是八进制,十进制,二进制或十六进制常量。十进制数字不需要前缀。后缀也可以与整数字面量一起使用,例如Uu用于无符号数字,而lL用于长数字。默认情况下,每个字面量都是int类型。对于整数数据类型(字节,短整数,整数,长整数),我们可以通过以下方式指定字面量:

  • 十进制字面量(以10为基数):采用这种形式,允许的数字为0-9。
int x = 101;


  • 八进制字面量(以8为基数):采用这种形式,允许的数字为0-7。
// The octal number should be prefix with 0.
int x = 0146; 


  • 十六进制十进制字面量(以16为基数):以这种形式,允许的数字为0-9,字符为af。我们可以同时使用大写和小写字符。我们知道c#是区分大小写的编程语言,但在这里c#并不区分大小写。
// The hexa-decimal number should be prefix
// with 0X or 0x.
int x = 0X123Face; 


  • 二进制字面量(以2为基数):采用这种形式,允许的数字仅为10
// The binary number should be prefix with 0b.
int x = 0b101


例子:

07778    // invalid: 8 is not an octal digit 
045uu    // invalid: suffix (u) is repeated
0b105    // invalid: 5 is not a binary digit
0b101    // valid binary literal
456      // valid decimal literal
02453    // valid octal literal 
0x65d    // valid hexadecimal literal 
12356    // valid int literal 
304U     // valid unsigned int literal 
3078L    // valid long literal 
965UL    // valid unsigned long literal 


程序:

C#
// C# program to illustrate the use of Integer Literals
using System;
 
class Geeks{
 
    // Main method
    public static void Main(String[] args)
    {
       
        // decimal-form literal
        int a = 101;
 
        // octal-form literal
        int b = 0145;
 
        // Hexa-decimal form literal
        int c = 0xFace;
       
        // binary-form literal
        int x = 0b101;
         
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
        Console.WriteLine(x);
    }
}


C#
// C# program to illustrate the use of
// floating-point literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
        // decimal-form literal
        double a = 101.230;
 
        // It also acts as decimal literal
        double b = 0123.222;
 
        Console.WriteLine(a);
        Console.WriteLine(b);
    }
}


C#
// C# program to illustrate the use of char literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // character literal within single quote
        char ch = 'a';
 
        // Unicode representation
        char c = '\u0061';
 
        Console.WriteLine(ch);
        Console.WriteLine(c);
 
        // Escape character literal
        Console.WriteLine("Hello\n\nGeeks\t!");
    }
}


C#
// C#  program to illustrate the use of String literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
 
        String s = "Hello Geeks!";
        String s2 = @"Hello Geeks!";
 
        // If we assign without "" then it
        // treats as a variable
        // and causes compiler error
        // String s1 = Geeks;
 
        Console.WriteLine(s);
        Console.WriteLine(s2);
    }
}


C#
// C# program to illustrate the use
// of boolean literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
        bool b = true;
        bool c = false;
 
        // these will give compile time error
        // bool d = 0;
        // bool e = 1;
        // Console.WriteLine(d);
        // Console.WriteLine(e);
 
        Console.WriteLine(b);
        Console.WriteLine(c);
    }
}


101
145
64206
5




浮点字面量:其具有一个整数部分,小数点,小数部分的字面量,和一个指数部分是被称为浮点字面量。这些可以十进制或指数形式表示。

例子:

Double d = 3.14145       // Valid
Double d = 312569E-5      // Valid
Double d = 125E             // invalid: Incomplete exponent 
Double d = 784f            // valid
Double d = .e45           // invalid: missing integer or fraction


程序:

C#

// C# program to illustrate the use of
// floating-point literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
        // decimal-form literal
        double a = 101.230;
 
        // It also acts as decimal literal
        double b = 0123.222;
 
        Console.WriteLine(a);
        Console.WriteLine(b);
    }
}

输出:

101.23
123.222


注意:默认情况下,每个浮点字面量都是double类型,因此我们不能直接将其分配给float变量。但是我们可以将后缀f或F指定为浮点型字面量作为浮点类型。我们可以将后缀d或D显式指定为浮点型字面量作为double类型,当然,此约定不是必需的。

字符字面量:对于字符数据类型,我们可以通过3种方式指定字面量:

  • 单引号:我们可以将字面量数据类型指定为char数据类型,并将其作为单引号内的单字符。
char ch = 'a';


  • Unicode表示形式:我们可以在Unicode表示形式’\ uxxxx’中指定char字面量。 xxxx代表4个十六进制数。
char ch = '\u0061';// Here /u0061 represent a.


  • 转义序列:每个转义字符都可以指定为char字面量。
char ch = '\n';


Escape Sequence Meaning
\\ \ character
\’ ‘ character
\? ? character
\” ” character
\b Backspace
\a Alert or Bell
\n New Line
\f Form Feed
\r Carriage Return
\v Vertical Tab
\xhh… Hexadecimal number of one or more digits

例子 :

C#

// C# program to illustrate the use of char literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // character literal within single quote
        char ch = 'a';
 
        // Unicode representation
        char c = '\u0061';
 
        Console.WriteLine(ch);
        Console.WriteLine(c);
 
        // Escape character literal
        Console.WriteLine("Hello\n\nGeeks\t!");
    }
}
a
a
Hello

Geeks    !


字符串字面量:其包括在双引号(“”)字面量或开始于@”,”都被称为字符串字面量。

例子:

String s1 = "Hello Geeks!";

String s2 = @"Hello Geeks!";


程序:

C#

// C#  program to illustrate the use of String literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
 
        String s = "Hello Geeks!";
        String s2 = @"Hello Geeks!";
 
        // If we assign without "" then it
        // treats as a variable
        // and causes compiler error
        // String s1 = Geeks;
 
        Console.WriteLine(s);
        Console.WriteLine(s2);
    }
}

输出:

Hello Geeks!
Hello Geeks!


布尔字面量:布尔字面量只允许使用两个值,即true和false。

例子:

bool b = true;
bool c = false;


程序:

C#

// C# program to illustrate the use
// of boolean literals
using System;
 
class Geeks {
 
    // Main Method
    public static void Main(String[] args)
    {
        bool b = true;
        bool c = false;
 
        // these will give compile time error
        // bool d = 0;
        // bool e = 1;
        // Console.WriteLine(d);
        // Console.WriteLine(e);
 
        Console.WriteLine(b);
        Console.WriteLine(c);
    }
}

输出:

True
False