- Mastering Visual Studio 2017
- Kunal Chowdhury
- 248字
- 2021-07-15 17:26:37
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); } }
Once you run the preceding code snippet, you will see the following output in console window:

- DevOps:軟件架構師行動指南
- Drupal 8 Blueprints
- Internet of Things with Intel Galileo
- Python機器學習:手把手教你掌握150個精彩案例(微課視頻版)
- ASP.NET程序設計教程
- Java EE 7 Performance Tuning and Optimization
- 前端HTML+CSS修煉之道(視頻同步+直播)
- 用戶體驗可視化指南
- PhoneGap 4 Mobile Application Development Cookbook
- Go Systems Programming
- JSP應用與開發技術(第3版)
- 小學生C++趣味編程從入門到精通
- C語言從入門到精通(微視頻精編版)
- PHP從入門到精通(第7版)
- iOS應用逆向工程:分析與實戰