官术网_书友最值得收藏!

Literal improvements in C# 7.0

There were two types of literals supported in C# prior to C# 7.0. They are decimal literals and hexadecimal literals. For example, 490 is a decimal literal, whereas 0x50A or 0X50A is a hexadecimal literal, equivalent to the decimal value 490. Please note that the prefixes 0x and 0X define the same thing.

Here's an example for you to easily understand how a hexadecimal literal is used in C#:

 
    class Program 
    { 
      static void Main(string[] args) 
      { 
        double height = 490; 
        double width = 1290; 
 
        double heightInHex = 0x1EA; // equivalent to decimal 490 
        double widthInHex = 0x50A; // equivalent to decimal 1290 
 
        Console.WriteLine("Height: " + heightInHex + " Width: " + widthInHex); 
      } 
    } 

Along with C# 7.0, Microsoft added more support to literals, and they have now introduced binary literals in C# to handle the binary value. The binary literals prefixed with 0b or 0B have the same meaning.

In the following example, the decimal value of the binary literal 0b1110110110 is 950:

    class Program 
    { 
      static void Main(string[] args) 
      { 
        double height = 950; 
        double width = 5046; 
 
        double heightInBinary = 0b1110110110; // decimal 950 
        double widthInBinary = 0b1001110110110; // decimal 5046 
 
        Console.WriteLine("Height: " + heightInBinary + " Width: " + widthInBinary); 
      } 
    } 
Remember that, whether your representation is decimal, hexadecimal, or binary in code, the output is always in decimal form only.

Once you run the preceding code snippet, you will see the following output in console window:

主站蜘蛛池模板: 封丘县| 札达县| 马鞍山市| 慈利县| 合水县| 安龙县| 油尖旺区| 麻阳| 鲜城| 建始县| 萍乡市| 维西| 弋阳县| 柳林县| 华池县| 旌德县| 长沙市| 察哈| 南靖县| 石泉县| 明溪县| 柳江县| 香河县| 上林县| 石嘴山市| 马鞍山市| 绍兴市| 门头沟区| 临安市| 潍坊市| 香格里拉县| 濉溪县| 屏南县| 成武县| 舟山市| 安义县| 朝阳县| 霍林郭勒市| 锡林郭勒盟| 青岛市| 呼和浩特市|