{"id":270,"date":"2023-10-15T17:51:39","date_gmt":"2023-10-15T09:51:39","guid":{"rendered":"https:\/\/miie.net\/?p=270"},"modified":"2023-10-15T17:51:39","modified_gmt":"2023-10-15T09:51:39","slug":"pro-c10-chapter-1-introducing-c-and-net-6","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=270","title":{"rendered":"Pro C#10 CHAPTER 1 Introducing C# and .NET 6"},"content":{"rendered":"<h1>Pro C# 10 with .NET 6<\/h1>\n<p>PART I<\/p>\n<p>Introducing C# and .NET 6<br \/>\n\u4ecb\u7ecd C# \u548c .NET 6<\/p>\n<h1>CHAPTER 1<\/h1>\n<p>\u7b2c\u4e00\u7ae0 <\/p>\n<p>Introducing C# and .NET 6<br \/>\n\u4ecb\u7ecd C# \u548c .NET 6<\/p>\n<p>Microsoft\u2019s .NET platform and the C# programming language were formally introduced circa 2002 and have quickly become a mainstay of modern-day software development. The .NET platform enables a large number of programming languages (including C#, VB.NET, and F#) to interact with each other. A program written in C# can be referenced by another program written in VB.NET. More on this interoperability later in this chapter.<br \/>\nMicrosoft\u7684.NET\u5e73\u53f0\u548cC#\u7f16\u7a0b\u8bed\u8a00\u5927\u7ea6\u57282002\u5e74\u6b63\u5f0f\u63a8\u51fa\uff0c\u5e76\u8fc5\u901f\u6210\u4e3a\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u7684\u652f\u67f1\u3002.NET \u5e73\u53f0\u4f7f\u5927\u91cf\u7f16\u7a0b\u8bed\u8a00\uff08\u5305\u62ec C#\u3001VB.NET \u548c F#\uff09\u80fd\u591f\u76f8\u4e92\u4ea4\u4e92\u3002\u4e00\u4e2a\u7a0b\u5e8f\u7528 C# \u7f16\u5199\u7684\u7a0b\u5e8f\u53ef\u4ee5\u88ab\u53e6\u4e00\u4e2a\u7528 VB.NET \u7f16\u5199\u7684\u7a0b\u5e8f\u5f15\u7528\u3002\u672c\u7ae0\u540e\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6b64\u4e92\u64cd\u4f5c\u6027\u3002<\/p>\n<p>In 2016, Microsoft officially launched .NET Core. Like .NET, .NET Core allows languages to interop with each other (although a limited number of languages are supported). More importantly, this new framework is no longer limited to running on the Windows operating system but can also run on iOS, and Linux and be developed on MacOS and Linux. This platform independence opened up .NET and C# to a much larger pool of developers. While cross-platform use of C# was supported prior to .NET Core, that was through various other frameworks such as the Mono project.<br \/>\n2016\u5e74\uff0cMicrosoft\u6b63\u5f0f\u63a8\u51fa.NET Core\u3002\u4e0e .NET \u4e00\u6837\uff0c.NET Core \u5141\u8bb8\u8bed\u8a00\u76f8\u4e92\u4e92\u64cd\u4f5c\uff08\u5c3d\u7ba1\u652f\u6301\u7684\u8bed\u8a00\u6570\u91cf\u6709\u9650\uff09\u3002\u66f4\u91cd\u8981\u7684\u662f\uff0c\u8fd9\u4e2a\u65b0\u6846\u67b6\u4e0d\u518d\u5c40\u9650\u4e8e\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u8fd8\u53ef\u4ee5\u5728iOS\u548cLinux\u4e0a\u8fd0\u884c\uff0c\u5e76\u5728MacOS\u548cLinux\u4e0a\u5f00\u53d1\u3002\u8fd9\u79cd\u5e73\u53f0\u72ec\u7acb\u6027\u5411\u66f4\u591a\u7684\u5f00\u53d1\u4eba\u5458\u5f00\u653e\u4e86 .NET \u548c C#\u3002\u867d\u7136\u5728.NET Core\u4e4b\u524d\u652f\u6301\u8de8\u5e73\u53f0\u4f7f\u7528C#\uff0c\u4f46\u8fd9\u662f\u901a\u8fc7\u5404\u79cd\u5176\u4ed6\u6846\u67b6\uff08\u5982Mono\u9879\u76ee\uff09\u652f\u6301\u7684\u3002<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nWith the release of .NET 5, the \u201cCore\u201d part of the name was dropped. Throughout this book, the term .NET refers to .NET Core (up to 3.1) and .NET 5\/6<br \/>\n\u6ce8\u610f \u968f\u7740 .NET 5 \u7684\u53d1\u5e03\uff0c\u540d\u79f0\u7684\u201c\u6838\u5fc3\u201d\u90e8\u5206\u88ab\u5220\u9664\u3002\u5728\u6574\u672c\u4e66\u4e2d\uff0c\u672f\u8bed.NET \u662f\u6307 .NET Core\uff08\u6700\u9ad8 3.1\uff09\u548c .NET 5\/6\u3002\n<\/p><\/blockquote>\n<p>Microsoft launched C# 10 and .NET 6 on November 8, 2021. C# 10 is tied to a specific version of the framework and will run only on .NET 6 and above. This relationship between language and .NET versions gives the C# team the freedom to introduce features into C# that couldn\u2019t otherwise be added into the language due to framework limitations.<br \/>\nMicrosoft\u4e8e 10 \u5e74 6 \u6708 8 \u65e5\u63a8\u51fa\u4e86 C# 10 \u548c .NET6\u3002C# 10 \u7ed1\u5b9a\u5230\u7279\u5b9a\u7248\u672c\u7684\u6846\u67b6\uff0c\u5e76\u4e14\u53ea\u80fd\u5728 .NET6\u53ca\u66f4\u9ad8\u7248\u672c\u4e0a\u8fd0\u884c\u3002\u8bed\u8a00\u548c .NET \u7248\u672c\u4e4b\u95f4\u7684\u8fd9\u79cd\u5173\u7cfb\u4f7f C# \u56e2\u961f\u53ef\u4ee5\u81ea\u7531\u5730\u5c06\u7531\u4e8e\u6846\u67b6\u9650\u5236\u800c\u65e0\u6cd5\u6dfb\u52a0\u5230\u8bed\u8a00\u4e2d\u7684\u529f\u80fd\u5f15\u5165 C#\u3002<\/p>\n<p>As mentioned in the book\u2019s introduction, the goal of this text is twofold. The first order of business is to provide you with a deep and detailed examination of the syntax and semantics of C#. The second (equally important) order of business is to illustrate the use of numerous .NET development frameworks. These include database access with ADO.NET and Entity Framework (EF) Core, user interfaces with Windows Presentation Foundation (WPF), and finally RESTful services and web applications with ASP.NET Core. As it is said, the journey of a thousand miles begins with a single step; and with this, I welcome you to Chapter 1.<br \/>\n\u6b63\u5982\u672c\u4e66\u7684\u5f15\u8a00\u4e2d\u63d0\u5230\u7684\uff0c\u672c\u6587\u7684\u76ee\u6807\u662f\u53cc\u91cd\u7684\u3002\u9996\u8981\u4efb\u52a1\u662f\u4e3a\u60a8\u63d0\u4f9b\u5bf9 C# \u8bed\u6cd5\u548c\u8bed\u4e49\u7684\u6df1\u5165\u800c\u8be6\u7ec6\u7684\u68c0\u67e5\u3002\u7b2c\u4e8c\u4e2a\uff08\u540c\u6837\u91cd\u8981\uff09\u7684\u4e1a\u52a1\u987a\u5e8f\u662f\u8bf4\u660e\u8bb8\u591a .NET \u5f00\u53d1\u6846\u67b6\u7684\u4f7f\u7528\u3002\u5176\u4e2d\u5305\u62ec\u4f7f\u7528 ADO.NET \u548c\u5b9e\u4f53\u6846\u67b6 \uff08EF\uff09 \u6838\u5fc3\u8bbf\u95ee\u6570\u636e\u5e93\u3001\u4f7f\u7528 Windows Presentation Foundation \uff08WPF\uff09 \u7684\u7528\u6237\u754c\u9762\uff0c\u4ee5\u53ca\u6700\u540e\u4f7f\u7528 ASP.NET Core \u7684 RESTful \u670d\u52a1\u548c Web \u5e94\u7528\u7a0b\u5e8f\u3002\u4fd7\u8bdd\u8bf4\uff0c\u5343\u91cc\u4e4b\u884c\uff0c\u59cb\u4e8e\u8db3\u4e0b;\u6709\u4e86\u8fd9\u4e2a\uff0c\u6211\u6b22\u8fce\u4f60\u6765\u5230\u7b2c\u4e00\u7ae0\u3002<\/p>\n<p>This first chapter lays the conceptual groundwork for the remainder of the book. Here, you will find a high-level discussion of a number of .NET-related topics such as assemblies, the Common Intermediate Language (CIL), and just-in-time (JIT) compilation. In addition to previewing some keywords of the C# programming language, you will also come to understand the relationship between the .NET Runtime, the Common Type System (CTS) and the Common Language Specification (CLS).<br \/>\n\u7b2c\u4e00\u7ae0\u4e3a\u672c\u4e66\u7684\u5176\u4f59\u90e8\u5206\u5960\u5b9a\u4e86\u6982\u5ff5\u57fa\u7840\u3002\u5728\u8fd9\u91cc\uff0c\u60a8\u5c06\u627e\u5230\u4e00\u4e9b .\u4e0e NET \u76f8\u5173\u7684\u4e3b\u9898\uff0c\u4f8b\u5982\u7a0b\u5e8f\u96c6\u3001\u516c\u5171\u4e2d\u95f4\u8bed\u8a00 \uff08CIL\uff09 \u548c\u5b9e\u65f6 \uff08JIT\uff09 \u7f16\u8bd1\u3002\u9664\u4e86\u9884\u89c8 C# \u7f16\u7a0b\u8bed\u8a00\u7684\u4e00\u4e9b\u5173\u952e\u5b57\u5916\uff0c\u60a8\u8fd8\u5c06\u4e86\u89e3 .NET \u8fd0\u884c\u65f6\u3001\u901a\u7528\u7c7b\u578b\u7cfb\u7edf \uff08CTS\uff09 \u548c\u516c\u5171\u8bed\u8a00\u89c4\u8303 \uff08CLS\uff09 \u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<p>This chapter also provides you with a survey of the functionality supplied by the .NET base class libraries, sometimes abbreviated as BCLs. Here, you will get an overview of the language-agnostic and platform-independent nature of the .NET platform. As you would expect, these topics are explored in further detail throughout the remainder of this text.<br \/>\n\u672c\u7ae0\u8fd8\u6982\u8ff0\u4e86 .NET \u57fa\u7c7b\u5e93\uff08\u6709\u65f6\u7f29\u5199\u4e3a BCL\uff09\u63d0\u4f9b\u7684\u529f\u80fd\u3002\u5728\u8fd9\u91cc\uff0c\u4f60\u5c06\u5927\u81f4\u4e86\u89e3 .NET \u5e73\u53f0\u4e0e\u8bed\u8a00\u65e0\u5173\u4e14\u72ec\u7acb\u4e8e\u5e73\u53f0\u7684\u6027\u8d28\u3002\u5982\u60a8\u6240\u6599\uff0c\u672c\u6587\u7684\u5176\u4f59\u90e8\u5206\u5c06\u66f4\u8be6\u7ec6\u5730\u63a2\u8ba8\u8fd9\u4e9b\u4e3b\u9898\u3002<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nMany of the features highlighted in this chapter (and throughout the book) also apply to the original.NET Framework.<br \/>\n\u6ce8\u610f \u672c\u7ae0\uff08\u4ee5\u53ca\u6574\u672c\u4e66\uff09\u4e2d\u5f3a\u8c03\u7684\u8bb8\u591a\u529f\u80fd\u4e5f\u9002\u7528\u4e8e\u539f\u6587.NET Framework.\n<\/p><\/blockquote>\n<h2>Exploring Some Key Benefits of the .NET Platform<\/h2>\n<p>\u63a2\u7d22 .NET \u5e73\u53f0\u7684\u4e00\u4e9b\u4e3b\u8981\u4f18\u52bf<\/p>\n<p>The .NET framework is a software platform for building web applications and services for the Windows, iOS, and Linux operating systems, as well as WinForms and WPF applications on Windows operating systems. To set the stage, here is a quick rundown of some core features provided, courtesy of .NET:<br \/>\nNET \u6846\u67b6\u662f\u4e00\u4e2a\u8f6f\u4ef6\u5e73\u53f0\uff0c\u7528\u4e8e\u4e3a Windows\u3001iOS \u548c Linux \u64cd\u4f5c\u7cfb\u7edf\u4ee5\u53ca Windows \u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684 WinForms \u548c WPF \u5e94\u7528\u7a0b\u5e8f\u6784\u5efa Web \u5e94\u7528\u7a0b\u5e8f\u548c\u670d\u52a1\u3002\u4e3a\u4e86\u5960\u5b9a\u57fa\u7840\uff0c\u4e0b\u9762\u662f\u7531 .NET \u63d0\u4f9b\u7684\u4e00\u4e9b\u6838\u5fc3\u529f\u80fd\u7684\u5feb\u901f\u6982\u8ff0\uff1a<\/p>\n<ul>\n<li>Support for numerous programming languages: .NET applications can be created using C#, F#, and VB.NET programming languages (with C# and F# being the primary languages for ASP.NET Core).<br \/>\n\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff1a\u53ef\u4ee5\u4f7f\u7528 C#\u3001F# \u548c VB.NET \u7f16\u7a0b\u8bed\u8a00\uff08C# \u548c F# \u662f ASP.NET Core \u7684\u4e3b\u8981\u8bed\u8a00\uff09\u521b\u5efa .NET \u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<li>A common runtime engine shared by all .NET languages: One aspect of this engine is a well-defined set of types that each .NET language understands.<br \/>\n\u6240\u6709 .NET \u8bed\u8a00\u5171\u4eab\u7684\u901a\u7528\u8fd0\u884c\u65f6\u5f15\u64ce\uff1a\u6b64\u5f15\u64ce\u7684\u4e00\u4e2a\u65b9\u9762\u662f\u4e00\u7ec4\u5b9a\u4e49\u826f\u597d\u7684\u7c7b\u578b\uff0c\u6bcf\u79cd .NET \u8bed\u8a00\u90fd\u80fd\u7406\u89e3\u8fd9\u4e9b\u7c7b\u578b\u3002<\/li>\n<li>Language integration: .NET supports cross-language inheritance, cross-language exception handling, and cross-language debugging of code. For example, you can define a base class in C# and extend this type in Visual Basic.<br \/>\n\u8bed\u8a00\u96c6\u6210\uff1a.NET \u652f\u6301\u8de8\u8bed\u8a00\u7ee7\u627f\u3001\u8de8\u8bed\u8a00\u5f02\u5e38\u5904\u7406\u548c\u8de8\u8bed\u8a00\u4ee3\u7801\u8c03\u8bd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728 C# \u4e2d\u5b9a\u4e49\u57fa\u7c7b\uff0c\u5e76\u5728 Visual Basic \u4e2d\u6269\u5c55\u6b64\u7c7b\u578b\u3002<\/li>\n<li>A comprehensive base class library: This library provides thousands of predefined types that allow you to build code libraries, simple terminal applications, graphical desktop applications, and enterprise-level websites.<br \/>\n\u5168\u9762\u7684\u57fa\u7c7b\u5e93\uff1a\u6b64\u5e93\u63d0\u4f9b\u6570\u5343\u79cd\u9884\u5b9a\u4e49\u7c7b\u578b\uff0c\u5141\u8bb8\u60a8\u6784\u5efa\u4ee3\u7801\u5e93\u3001\u7b80\u5355\u7ec8\u7aef\u5e94\u7528\u7a0b\u5e8f\u3001\u56fe\u5f62\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u548c\u4f01\u4e1a\u7ea7\u7f51\u7ad9\u3002<\/li>\n<li>A simplified deployment model: .NET libraries are not registered into the system registry. Furthermore, the .NET platform allows multiple versions of the framework as well as applications to exist in harmony on a single machine.<br \/>\n\u7b80\u5316\u7684\u90e8\u7f72\u6a21\u578b\uff1a.NET \u5e93\u672a\u6ce8\u518c\u5230\u7cfb\u7edf\u6ce8\u518c\u8868\u4e2d\u3002\u6b64\u5916\uff0c.NET \u5e73\u53f0\u5141\u8bb8\u6846\u67b6\u7684\u591a\u4e2a\u7248\u672c\u4ee5\u53ca\u5e94\u7528\u7a0b\u5e8f\u5728\u4e00\u53f0\u673a\u5668\u4e0a\u548c\u8c10\u5b58\u5728\u3002<\/li>\n<li>Extensive command-line support: The .NET command-line interface (CLI) is a cross- platform tool chain for developing and packaging .NET applications. Additional tools can be installed (globally or locally) beyond the standard tools that ship with the .NET SDK.<br \/>\n\u5e7f\u6cdb\u7684\u547d\u4ee4\u884c\u652f\u6301\uff1a.NET \u547d\u4ee4\u884c\u754c\u9762 \uff08CLI\uff09 \u662f\u7528\u4e8e\u5f00\u53d1\u548c\u6253\u5305 .NET \u5e94\u7528\u7a0b\u5e8f\u7684\u8de8\u5e73\u53f0\u5de5\u5177\u94fe\u3002\u9664\u4e86 .NET SDK \u9644\u5e26\u7684\u6807\u51c6\u5de5\u5177\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u5b89\u88c5\u5176\u4ed6\u5de5\u5177\uff08\u5168\u5c40\u6216\u672c\u5730\uff09\u3002<\/li>\n<\/ul>\n<p>You will see each of these topics (and many more) examined in the chapters to come. But first, I need to explain the new support lifecycle for .NET.<br \/>\n\u60a8\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\u770b\u5230\u8fd9\u4e9b\u4e3b\u9898\u4e2d\u7684\u6bcf\u4e00\u4e2a\uff08\u4ee5\u53ca\u66f4\u591a\uff09\u3002\u4f46\u9996\u5148\uff0c\u6211\u9700\u8981\u89e3\u91ca .NET \u7684\u65b0\u652f\u6301\u751f\u547d\u5468\u671f\u3002<\/p>\n<h2>Understanding the .NET Support Lifecycle<\/h2>\n<p>\u4e86\u89e3.NET \u652f\u6301\u751f\u547d\u5468\u671f<\/p>\n<p>.NET versions are released much more frequently than prior .NET Framework. With all of these releases available, it can be difficult to keep up, especially in an enterprise development environment. To better define the support lifecycle for the releases, Microsoft has adopted a variation of the Long-Term Support Model,1 commonly used by modern open source frameworks.<br \/>\n.NET \u7248\u672c\u7684\u53d1\u5e03\u9891\u7387\u6bd4\u4ee5\u524d\u7684 .NET Framework \u9ad8\u5f97\u591a\u3002\u7531\u4e8e\u6240\u6709\u8fd9\u4e9b\u7248\u672c\u90fd\u53ef\u7528\uff0c\u56e0\u6b64\u53ef\u80fd\u5f88\u96be\u8ddf\u4e0a\uff0c\u5c24\u5176\u662f\u5728\u4f01\u4e1a\u5f00\u53d1\u73af\u5883\u4e2d\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5b9a\u4e49\u7248\u672c\u7684\u652f\u6301\u751f\u547d\u5468\u671f\uff0cMicrosoft\u91c7\u7528\u4e86\u73b0\u4ee3\u5f00\u6e90\u6846\u67b6\u5e38\u7528\u7684\u957f\u671f\u652f\u6301\u6a21\u578b1\u7684\u53d8\u4f53\u3002<\/p>\n<p>Long-Term Support (LTS) releases are major releases that will be supported for an extended period of time. They will only receive critical and\/or nonbreaking fixes throughout their life span. Prior to end-of-life, LTS versions will be changed to the designation of maintenance. LTS releases with .NET will be supported for the following time frames, whichever is longer:<br \/>\n\u957f\u671f\u652f\u6301 \uff08LTS\uff09 \u7248\u672c\u662f\u5c06\u957f\u671f\u652f\u6301\u7684\u4e3b\u8981\u7248\u672c\u3002\u4ed6\u4eec\u53ea\u4f1a\u5728\u5176\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\u6536\u5230\u5173\u952e\u548c\/\u6216\u4e0d\u95f4\u65ad\u7684\u4fee\u590d\u3002\u5728\u4f7f\u7528\u5bff\u547d\u7ed3\u675f\u4e4b\u524d\uff0cLTS \u7248\u672c\u5c06\u66f4\u6539\u4e3a\u7ef4\u62a4\u6307\u5b9a\u3002\u5728\u4ee5\u4e0b\u65f6\u95f4\u8303\u56f4\u5185\uff0c\u5c06\u652f\u6301\u5e26\u6709 .NET \u7684 LTS \u7248\u672c\uff0c\u4ee5\u8f83\u957f\u8005\u4e3a\u51c6\uff1a<\/p>\n<ul>\n<li>Three years after initial release<br \/>\n\u9996\u6b21\u53d1\u5e03\u4e09\u5e74\u540e<\/li>\n<li>One year of maintenance support after subsequent LTS release<br \/>\n\u540e\u7eed LTS \u53d1\u5e03\u540e\u4e00\u5e74\u7684\u7ef4\u62a4\u652f\u6301<\/li>\n<\/ul>\n<p>Microsoft has decided to name Short-Term Support releases as Current, which are interval releases between the major LTS releases. They are supported for six months after a subsequent Current or LTS release.<br \/>\nMicrosoft\u5df2\u51b3\u5b9a\u5c06\u77ed\u671f\u652f\u6301\u7248\u672c\u547d\u540d\u4e3a\u5f53\u524d\u7248\u672c\uff0c\u8fd9\u662f\u4e3b\u8981 LTS \u7248\u672c\u4e4b\u95f4\u7684\u95f4\u9694\u7248\u672c\u3002\u5b83\u4eec\u5728\u540e\u7eed\u5f53\u524d\u6216 LTS \u7248\u672c\u53d1\u5e03\u540e\u7684\u516d\u4e2a\u6708\u5185\u53d7\u652f\u6301\u3002<\/p>\n<p>As mentioned earlier, .NET 6 was released in November 2021. It was released as a Long Term Support version and will be supported at least until November 2024. .NET 5 will go out of support on May 8, 2022, six months after the .NET 6 release. The only other supported version at the time of this writing is .NET Core 3.1 which is supported until December 3, 2022.<br \/>\n\u5982\u524d\u6240\u8ff0\uff0c.NET 6\u4e8e2021\u5e7411\u6708\u53d1\u5e03\u3002\u5b83\u4f5c\u4e3a\u957f\u671f\u652f\u6301\u7248\u672c\u53d1\u5e03\uff0c\u81f3\u5c11\u57282024\u5e7411\u6708\u4e4b\u524d\u5c06\u53d7\u652f\u6301\u3002.NET 5\u5c06\u4e8e2022\u5e745\u6708\u505c\u6b62\u652f\u6301\uff0c\u5373.NET 6\u53d1\u5e033\u4e2a\u6708\u540e\u3002\u5728\u64b0\u5199\u672c\u6587\u65f6\uff0c\u552f\u4e00\u53d7\u652f\u6301\u7684\u5176\u4ed6\u7248\u672c\u662f .NET Core 3.1\uff0c\u8be5\u7248\u672c\u57282022\u5e7412\u67083\u65e5\u4e4b\u524d\u53d7\u652f\u6301\u3002<\/p>\n<p>It\u2019s important to check the support policy for each new version of .NET that is released. Just having a higher number doesn\u2019t necessarily mean it\u2019s going to be supported long term. The full policy is located here:<br \/>\n\u8bf7\u52a1\u5fc5\u68c0\u67e5\u53d1\u5e03\u7684\u6bcf\u4e2a\u65b0\u7248\u672c .NET \u7684\u652f\u6301\u7b56\u7565\u3002\u4ec5\u4ec5\u62e5\u6709\u66f4\u9ad8\u7684\u6570\u5b57\u5e76\u4e0d\u4e00\u5b9a\u610f\u5473\u7740\u5b83\u5c06\u5f97\u5230\u957f\u671f\u652f\u6301\u3002\u5b8c\u6574\u653f\u7b56\u4f4d\u4e8e\u6b64\u5904\uff1a<br \/>\n<a href=\"https:\/\/dotnet.microsoft.com\/platform\/support-policy\/dotnet-core\"><a href=\"https:\/\/dotnet.microsoft.com\/platform\/support-policy\/dotnet-core\"><a href=\"https:\/\/dotnet.microsoft.com\/platform\/support-policy\/dotnet-core\">https:\/\/dotnet.microsoft.com\/platform\/support-policy\/dotnet-core<\/a><\/a><\/a><\/p>\n<h2>Previewing the Building Blocks of the .NET Platform<\/h2>\n<p>\u9884\u89c8 .NET \u5e73\u53f0\u7684\u6784\u5efa\u57fa\u5757<\/p>\n<p>Now that you know some of the major benefits provided by .NET, let\u2019s preview key (and interrelated) topics that make it all possible: the .NET Runtime, CTS, and the CLS. From a programmer\u2019s point of view, .NET can be understood as a runtime environment and a comprehensive base class library. The runtime layer contains the set of minimal implementations that are tied specifically to a platform (Windows, iOS, Linux) and architecture (x86, x64, ARM), as well as all of the base types for .NET.<br \/>\n\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u4e86\u89e3\u4e86 .NET \u63d0\u4f9b\u7684\u4e00\u4e9b\u4e3b\u8981\u4f18\u52bf\uff0c\u8ba9\u6211\u4eec\u9884\u89c8\u4e00\u4e0b\u4f7f\u8fd9\u4e00\u5207\u6210\u4e3a\u53ef\u80fd\u7684\u5173\u952e\uff08\u548c\u76f8\u4e92\u5173\u8054\uff09\u4e3b\u9898\uff1a.NET \u8fd0\u884c\u65f6\u3001CTS \u548c CLS\u3002\u4ece\u7a0b\u5e8f\u5458\u7684\u89d2\u5ea6\u6765\u770b\uff0c.NET \u53ef\u4ee5\u7406\u89e3\u4e3a\u8fd0\u884c\u65f6\u73af\u5883\u548c\u5168\u9762\u7684\u57fa\u7c7b\u5e93\u3002\u8fd0\u884c\u65f6\u5c42\u5305\u542b\u4e00\u7ec4\u4e13\u95e8\u7ed1\u5b9a\u5230\u5e73\u53f0\uff08Windows\u3001iOS\u3001Linux\uff09\u548c\u4f53\u7cfb\u7ed3\u6784\uff08x86\u3001x64\u3001ARM\uff09\u7684\u6700\u5c0f\u5b9e\u73b0\uff0c\u4ee5\u53ca .NET \u7684\u6240\u6709\u57fa\u672c\u7c7b\u578b\u3002<\/p>\n<p>Another building block of the .NET platform is the Common Type System, or CTS. The CTS specification fully describes all possible data types and all programming constructs supported by the runtime, specifies how these entities can interact with each other, and details how they are represented in the .NET metadata format (more information on metadata later in this chapter; see Chapter 17 for complete details).<br \/>\n.NET \u5e73\u53f0\u7684\u53e6\u4e00\u4e2a\u6784\u5efa\u57fa\u5757\u662f\u901a\u7528\u7c7b\u578b\u7cfb\u7edf\u6216 CTS\u3002CTS \u89c4\u8303\u5b8c\u6574\u63cf\u8ff0\u4e86\u8fd0\u884c\u65f6\u652f\u6301\u7684\u6240\u6709\u53ef\u80fd\u7684\u6570\u636e\u7c7b\u578b\u548c\u6240\u6709\u7f16\u7a0b\u6784\u9020\uff0c\u6307\u5b9a\u4e86\u8fd9\u4e9b\u5b9e\u4f53\u5982\u4f55\u76f8\u4e92\u4ea4\u4e92\uff0c\u5e76\u8be6\u7ec6\u8bf4\u660e\u4e86\u5b83\u4eec\u5982\u4f55\u4ee5 .NET \u5143\u6570\u636e\u683c\u5f0f\u8868\u793a\uff08\u6709\u5173\u672c\u7ae0\u540e\u9762\u7684\u5143\u6570\u636e\u7684\u8be6\u7ec6\u4fe1\u606f;\u6709\u5173\u5b8c\u6574\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u7b2c 17 \u7ae0\uff09\u3002<\/p>\n<p>Understand that a given .NET language might not support every feature defined by the CTS. The Common Language Specification, or CLS, is a related specification that defines a subset of common types and programming constructs that all .NET programming languages can agree on. Thus, if you build .NET types that expose only CLS-compliant features, you can rest assured that all .NET languages can consume them. Conversely, if you make use of a data type or programming construct that is outside of the bounds of the CLS, you cannot guarantee that every .NET programming language can interact with your .NET code library. Thankfully, as you will see later in this chapter, it is simple to tell your C# compiler to check all of your code for CLS compliance.<br \/>\n\u4e86\u89e3\u7ed9\u5b9a\u7684 .NET \u8bed\u8a00\u53ef\u80fd\u4e0d\u652f\u6301 CTS \u5b9a\u4e49\u7684\u6240\u6709\u529f\u80fd\u3002\u516c\u5171\u8bed\u8a00\u89c4\u8303 \uff08CLS\uff09 \u662f\u4e00\u4e2a\u76f8\u5173\u89c4\u8303\uff0c\u5b83\u5b9a\u4e49\u6240\u6709 .NET \u7f16\u7a0b\u8bed\u8a00\u90fd\u53ef\u4ee5\u540c\u610f\u7684\u5e38\u89c1\u7c7b\u578b\u548c\u7f16\u7a0b\u6784\u9020\u7684\u5b50\u96c6\u3002\u56e0\u6b64\uff0c\u5982\u679c\u751f\u6210\u4ec5\u516c\u5f00\u7b26\u5408 CLS \u7684\u529f\u80fd\u7684 .NET \u7c7b\u578b\uff0c\u5219\u53ef\u4ee5\u653e\u5fc3\uff0c\u6240\u6709 .NET \u8bed\u8a00\u90fd\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u3002\u76f8\u53cd\uff0c\u5982\u679c\u4f7f\u7528 CLS \u8fb9\u754c\u4e4b\u5916\u7684\u6570\u636e\u7c7b\u578b\u6216\u7f16\u7a0b\u6784\u9020\uff0c\u5219\u65e0\u6cd5\u4fdd\u8bc1\u6bcf\u79cd .NET \u7f16\u7a0b\u8bed\u8a00\u90fd\u53ef\u4ee5\u4e0e .NET \u4ee3\u7801\u5e93\u4ea4\u4e92\u3002\u503c\u5f97\u5e86\u5e78\u7684\u662f\uff0c\u6b63\u5982\u60a8\u5c06\u5728\u672c\u7ae0\u540e\u9762\u770b\u5230\u7684\u90a3\u6837\uff0c\u544a\u8bc9 C# \u7f16\u8bd1\u5668\u68c0\u67e5\u6240\u6709\u4ee3\u7801\u662f\u5426\u7b26\u5408 CLS \u975e\u5e38\u7b80\u5355\u3002<\/p>\n<h3>The Role of the Base Class Libraries<\/h3>\n<p>\u57fa\u7c7b\u5e93\u7684\u4f5c\u7528<\/p>\n<p>The .NET platform also provides a set of base class libraries (BCLs) that are available to all .NET programming languages. Not only does this base class library encapsulate various primitives such as threads, file input\/output (I\/O), graphical rendering systems, and interaction with various external hardware devices, but it also provides support for a number of services required by most real-world applications.<br \/>\n.NET \u5e73\u53f0\u8fd8\u63d0\u4f9b\u4e86\u4e00\u7ec4\u53ef\u7528\u4e8e\u6240\u6709 .NET \u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7c7b\u5e93 \uff08BCL\uff09\u3002\u6b64\u57fa\u7c7b\u5e93\u4e0d\u4ec5\u5c01\u88c5\u4e86\u5404\u79cd\u57fa\u5143\uff0c\u4f8b\u5982\u7ebf\u7a0b\u3001\u6587\u4ef6\u8f93\u5165\/\u8f93\u51fa \uff08I\/O\uff09\u3001\u56fe\u5f62\u5448\u73b0\u7cfb\u7edf\u4ee5\u53ca\u4e0e\u5404\u79cd\u5916\u90e8\u786c\u4ef6\u8bbe\u5907\u7684\u4ea4\u4e92\uff0c\u800c\u4e14\u8fd8\u4e3a\u5927\u591a\u6570\u5b9e\u9645\u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u7684\u8bb8\u591a\u670d\u52a1\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n<p>The base class libraries define types that can be used to build any type of software application and for components of the application to interact with each other.<br \/>\n\u57fa\u7c7b\u5e93\u5b9a\u4e49\u53ef\u7528\u4e8e\u751f\u6210\u4efb\u4f55\u7c7b\u578b\u7684\u8f6f\u4ef6\u5e94\u7528\u7a0b\u5e8f\u4ee5\u53ca\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u76f8\u4e92\u4ea4\u4e92\u7684\u7c7b\u578b\u3002<\/p>\n<h2>The Role of .NET Standard<\/h2>\n<p>.NET \u6807\u51c6\u7684\u89d2\u8272<\/p>\n<p>The number of base class libraries in the .NET Framework far exceeds those in .NET. This is understandable, as the .NET Framework had a 14-year head start on .NET. This disparity created issues when attempting to use .NET Framework code with .NET code. The solution (and requirement) for .NET Framework\/.NET Core3.1 interop is .NET Standard.<br \/>\n.NET Framework \u4e2d\u7684\u57fa\u7c7b\u5e93\u6570\u91cf\u8fdc\u8fdc\u8d85\u8fc7 .NET \u4e2d\u7684\u57fa\u7c7b\u5e93\u3002\u8fd9\u662f\u53ef\u4ee5\u7406\u89e3\u7684\uff0c\u56e0\u4e3a.NET Framework\u5728.NET\u4e0a\u5df2\u7ecf\u9886\u5148\u4e8614\u5e74\u3002\u8fd9\u79cd\u5dee\u5f02\u5728\u5c1d\u8bd5\u5c06.NET Framework\u4ee3\u7801\u4e0e.NET\u4ee3\u7801\u4e00\u8d77\u4f7f\u7528\u65f6\u4f1a\u4ea7\u751f\u95ee\u9898\u3002.NET Framework\/.NET Core \u7684\u89e3\u51b3\u65b9\u6848\uff08\u548c\u8981\u6c42\uff09\u4e92\u64cd\u4f5c\u662f .NET Standard\u3002<\/p>\n<p>.NET Standard is a specification that defines the availability of .NET APIs and base class libraries that must be available in each implementation. The standard enables the following scenarios:<br \/>\n.NET \u6807\u51c6\u662f\u4e00\u4e2a\u89c4\u8303\uff0c\u7528\u4e8e\u5b9a\u4e49\u6bcf\u4e2a\u5b9e\u73b0\u4e2d\u5fc5\u987b\u53ef\u7528\u7684.NET API \u548c\u57fa\u7c7b\u5e93\u7684\u53ef\u7528\u6027\u3002\u8be5\u6807\u51c6\u652f\u6301\u4ee5\u4e0b\u65b9\u6848\uff1a<\/p>\n<ul>\n<li>Defines a uniform set of BCL APIs for all .NET implementations to implement, independent of workload<br \/>\n\u4e3a\u6240\u6709\u8981\u5b9e\u73b0\u7684 .NET \u5b9e\u73b0\u5b9a\u4e49\u4e00\u7ec4\u7edf\u4e00\u7684 BCL API\uff0c\u4e0e\u5de5\u4f5c\u8d1f\u8f7d\u65e0\u5173<\/li>\n<li>Enables developers to produce portable libraries that are usable across .NET implementations, using this same set of APIs<br \/>\n\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u4f7f\u7528\u540c\u4e00\u7ec4 API \u751f\u6210\u53ef\u8de8 .NET \u5b9e\u73b0\u4f7f\u7528\u7684\u53ef\u79fb\u690d\u5e93<\/li>\n<li>Reduces or even eliminates conditional compilation of shared source due to .NET APIs, only for OS APIs<br \/>\n\u51cf\u5c11\u751a\u81f3\u6d88\u9664\u7531\u4e8e .NET API \u800c\u5bf9\u5171\u4eab\u6e90\u4ee3\u7801\u7684\u6761\u4ef6\u7f16\u8bd1\uff0c\u4ec5\u9002\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf API<\/li>\n<\/ul>\n<p>The chart located in the Microsoft documentation (<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard<\/a><\/a><\/a>) shows the various compatibility between .NET Framework and .NET. This is useful for prior versions of C#. However, C# 9+ will only run on .NET 5+ or .NET Standard 2.1, and .NET Standard 2.1 is not available to the .NET Framework.<br \/>\n\u4f4d\u4e8eMicrosoft\u6587\u6863\uff08<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\"><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/net-standard<\/a><\/a><\/a> \uff09\u4e2d\u7684\u56fe\u8868\u663e\u793a\u4e86.NET Framework\u548c.NET\u4e4b\u95f4\u7684\u5404\u79cd\u517c\u5bb9\u6027\u3002\u8fd9\u5bf9\u4e8e\u4ee5\u524d\u7248\u672c\u7684 C# \u5f88\u6709\u7528\u3002\u4f46\u662f\uff0cC# 9+ \u53ea\u80fd\u5728 .NET 5+ \u6216 .NET Standard 2.1\u3002.NET Standard \u5bf9 .NET Framework \u4e0d\u53ef\u7528\u3002<\/p>\n<h2>What C# Brings to the Table<\/h2>\n<p>C# \u5e26\u6765\u4e86\u4ec0\u4e48<\/p>\n<p>C# is a programming language whose core syntax looks very similar to the syntax of Java. However, calling C# a Java clone is inaccurate. In reality, both C# and Java are members of the C family of programming languages (e.g., C, Objective-C, C++) and, therefore, share a similar syntax.<br \/>\nC#\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u6838\u5fc3\u8bed\u6cd5\u770b\u8d77\u6765\u4e0eJava\u7684\u8bed\u6cd5\u975e\u5e38\u76f8\u4f3c\u3002\u4f46\u662f\uff0c\u5c06 C# \u79f0\u4e3a Java \u514b\u9686\u662f\u4e0d\u51c6\u786e\u7684\u3002\u5b9e\u9645\u4e0a\uff0cC#\u548cJava\u90fd\u662fC\u7cfb\u5217\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982C\uff0cObjective-C\uff0cC++\uff09\u7684\u6210\u5458\uff0c\u56e0\u6b64\u5171\u4eab\u76f8\u4f3c\u7684\u8bed\u6cd5\u3002<\/p>\n<p>The truth of the matter is that many of C#\u2019s syntactic constructs are modeled after various aspects of Visual Basic (VB) and C++. For example, like VB, C# supports the notion of class properties (as opposed to traditional getter and setter methods) and optional parameters. Like C++, C# allows you to overload operators, as well as create structures, enumerations, and callback functions (via delegates).<br \/>\n\u4e8b\u60c5\u7684\u771f\u76f8\u662f\uff0cC#\u7684\u8bb8\u591a\u8bed\u6cd5\u7ed3\u6784\u90fd\u662f\u6839\u636eVisual Basic\uff08VB\uff09\u548cC++\u7684\u5404\u4e2a\u65b9\u9762\u5efa\u6a21\u7684\u3002\u4f8b\u5982\uff0c\u4e0e VB \u4e00\u6837\uff0cC# \u652f\u6301\u7c7b\u5c5e\u6027\uff08\u4e0e\u4f20\u7edf\u7684 getter \u548c setter \u65b9\u6cd5\u76f8\u53cd\uff09\u548c\u53ef\u9009\u53c2\u6570\u7684\u6982\u5ff5\u3002\u4e0eC++\u4e00\u6837\uff0cC# \u5141\u8bb8\u60a8\u91cd\u8f7d\u8fd0\u7b97\u7b26\uff0c\u4ee5\u53ca\u521b\u5efa\u7ed3\u6784\u3001\u679a\u4e3e\u548c\u56de\u8c03\u51fd\u6570\uff08\u901a\u8fc7\u59d4\u6258\uff09\u3002<\/p>\n<p>Moreover, as you work through this text, you will quickly see that C# supports a number of features, such as lambda expressions and anonymous types, traditionally found in various functional languages (e.g., LISP or Haskell). Furthermore, with the advent of Language Integrated Query (LINQ), C# supports a number of constructs that make it quite unique in the programming landscape. Nevertheless, the bulk of C# is indeed influenced by C-based languages.<br \/>\n\u6b64\u5916\uff0c\u5f53\u60a8\u9605\u8bfb\u672c\u6587\u65f6\uff0c\u60a8\u5c06\u5f88\u5feb\u770b\u5230 C# \u652f\u6301\u8bb8\u591a\u529f\u80fd\uff0c\u4f8b\u5982 lambda \u8868\u8fbe\u5f0f\u548c\u533f\u540d\u7c7b\u578b\uff0c\u8fd9\u4e9b\u529f\u80fd\u4f20\u7edf\u4e0a\u5b58\u5728\u4e8e\u5404\u79cd\u51fd\u6570\u5f0f\u8bed\u8a00\uff08\u4f8b\u5982 LISP \u6216 Haskell\uff09\u4e2d\u3002\u6b64\u5916\uff0c\u968f\u7740\u8bed\u8a00\u96c6\u6210\u67e5\u8be2 \uff08LINQ\uff09 \u7684\u51fa\u73b0\uff0cC# \u652f\u6301\u4f7f\u5176\u5728\u7f16\u7a0b\u73af\u5883\u4e2d\u975e\u5e38\u72ec\u7279\u7684\u6784\u9020\u6570\u91cf\u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u5927\u90e8\u5206 C# \u786e\u5b9e\u53d7\u5230\u57fa\u4e8e C \u7684\u8bed\u8a00\u7684\u5f71\u54cd\u3002<\/p>\n<p>Because C# is a hybrid of numerous languages, the result is a product that is as syntactically clean as (if not cleaner than) Java provides just about as much power and flexibility as C++. Here is a partial list of core C# features that are found in all versions of the language:<br \/>\n\u7531\u4e8e C# \u662f\u591a\u79cd\u8bed\u8a00\u7684\u6df7\u5408\u4f53\uff0c\u56e0\u6b64\u5176\u7ed3\u679c\u662f\u4ea7\u54c1\u5728\u8bed\u6cd5\u4e0a\u4e0e Java \u4e00\u6837\u5e72\u51c0\uff08\u5982\u679c\u4e0d\u662f\u6bd4 Java \u66f4\u5e72\u51c0\u7684\u8bdd\uff09\uff0c\u5b83\u63d0\u4f9b\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u4e0e C++ \u4e00\u6837\u591a\u3002\u4e0b\u9762\u662f\u8be5\u8bed\u8a00\u7684\u6240\u6709\u7248\u672c\u4e2d\u7684\u6838\u5fc3 C# \u529f\u80fd\u7684\u90e8\u5206\u5217\u8868\uff1a<\/p>\n<ul>\n<li>No pointers required! C# programs typically have no need for direct pointer manipulation (although you are free to drop down to that level if absolutely necessary, as shown in Chapter 11).<br \/>\n\u65e0\u9700\u6307\u9488\uff01C# \u7a0b\u5e8f\u901a\u5e38\u4e0d\u9700\u8981\u76f4\u63a5\u6307\u9488\u64cd\u4f5c\uff08\u5c3d\u7ba1\u5982\u679c\u7edd\u5bf9\u5fc5\u8981\uff0c\u60a8\u53ef\u4ee5\u81ea\u7531\u4e0b\u964d\u5230\u8be5\u7ea7\u522b\uff0c\u5982\u7b2c 11 \u7ae0\u6240\u793a\uff09\u3002<\/li>\n<li>Automatic memory management through garbage collection. Given this, C# does not support a delete keyword.<br \/>\n\u901a\u8fc7\u5783\u573e\u56de\u6536\u8fdb\u884c\u81ea\u52a8\u5185\u5b58\u7ba1\u7406\u3002\u9274\u4e8e\u6b64\uff0cC# \u4e0d\u652f\u6301 delete \u5173\u952e\u5b57\u3002<\/li>\n<li>Formal syntactic constructs for classes, interfaces, structures, enumerations, and delegates.<br \/>\n\u7c7b\u3001\u63a5\u53e3\u3001\u7ed3\u6784\u3001\u679a\u4e3e\u548c\u59d4\u6258\u7684\u6b63\u5f0f\u8bed\u6cd5\u6784\u9020\u3002<\/li>\n<li>The C++-like ability to overload operators for a custom type, without the complexity.<br \/>\n\u7c7b\u4f3cC++\u7684\u529f\u80fd\uff0c\u53ef\u91cd\u8f7d\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\uff0c\u800c\u6ca1\u6709\u590d\u6742\u6027\u3002<\/li>\n<li>Support for attribute-based programming. This brand of development allows you to annotate types and their members to further qualify their behavior. For example, if you mark a method with the [Obsolete] attribute, programmers will see your custom warning message print out if they attempt to make use of the decorated member.<br \/>\n\u652f\u6301\u57fa\u4e8e\u5c5e\u6027\u7684\u7f16\u7a0b\u3002\u6b64\u5f00\u53d1\u54c1\u724c\u5141\u8bb8\u60a8\u6279\u6ce8\u7c7b\u578b\u53ca\u5176\u6210\u5458\uff0c\u4ee5\u8fdb\u4e00\u6b65\u9650\u5b9a\u5176\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528 [Obsolete] \u5c5e\u6027\u6807\u8bb0\u65b9\u6cd5\uff0c\u5219\u7a0b\u5e8f\u5458\u5728\u5c1d\u8bd5\u4f7f\u7528\u4fee\u9970\u6210\u5458\u65f6\u5c06\u770b\u5230\u6253\u5370\u51fa\u7684\u81ea\u5b9a\u4e49\u8b66\u544a\u6d88\u606f\u3002<\/li>\n<\/ul>\n<p>C# 10 is an already powerful language and, combined with .NET, enables building a wide range of application types.<br \/>\nC# 10 \u662f\u4e00\u79cd\u5df2\u7ecf\u5f88\u5f3a\u5927\u7684\u8bed\u8a00\uff0c\u4e0e .NET \u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u5e94\u7528\u7a0b\u5e8f\u7c7b\u578b\u3002<\/p>\n<h2>Major Features in Prior Releases<\/h2>\n<p>\u5148\u524d\u7248\u672c\u4e2d\u7684\u4e3b\u8981\u529f\u80fd<\/p>\n<p>Starting with C# 7, I started adding into the section headers the version when features were added (e.g. \u201c(New 7.x)\u201d) or updated (e.g. \u201c(Updated 7.x)\u201d).<br \/>\n\u4ece C# 7 \u5f00\u59cb\uff0c\u6211\u5f00\u59cb\u5728\u8282\u6807\u9898\u4e2d\u6dfb\u52a0\u6dfb\u52a0\u529f\u80fd\uff08\u4f8b\u5982\u201c\uff08\u65b0 7.x\uff09\u201d\uff09\u6216\u66f4\u65b0\u529f\u80fd\uff08\u4f8b\u5982\u201c\uff08\u66f4\u65b0\u7684 7.x\uff09\u201d\uff09\u65f6\u7684\u7248\u672c\u3002<\/p>\n<h2>New Features in C# 10<\/h2>\n<p>C# 10 \u4e2d\u7684\u65b0\u529f\u80fd<\/p>\n<p>C# 10, released on November 8, 2021, with .NET 6, adds the following features:<br \/>\n2021\u5e7411\u67088\u65e5\u53d1\u5e03\u7684 C# 6 \u4e0e .NET6\u4e00\u8d77\u53d1\u5e03\uff0c\u6dfb\u52a0\u4e86\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>Record structs<br \/>\n\u8bb0\u5f55\u7ed3\u6784<\/li>\n<li>Improvements to structure types<br \/>\n\u5bf9\u7ed3\u6784\u7c7b\u578b\u7684\u6539\u8fdb<\/li>\n<li>Global using directives and global implicit using directives<br \/>\n\u5168\u5c40 using \u6307\u4ee4\u548c\u5168\u5c40\u9690\u5f0f using \u6307\u4ee4<\/li>\n<li>File scoped namespaces<br \/>\n\u6587\u4ef6\u8303\u56f4\u7684\u547d\u540d\u7a7a\u95f4<\/li>\n<li>Property pattern matching enhancements<br \/>\n\u5c5e\u6027\u6a21\u5f0f\u5339\u914d\u589e\u5f3a\u529f\u80fd<\/li>\n<li>Constant Interpolated strings<br \/>\n\u5e38\u91cf\u5185\u63d2\u5b57\u7b26\u4e32<\/li>\n<li>Improvements to lambda expressions<br \/>\n\u5bf9lambda\u8868\u8fbe\u5f0f\u7684\u6539\u8fdb<\/li>\n<li>Record type enhancements<br \/>\n\u8bb0\u5f55\u7c7b\u578b\u589e\u5f3a\u529f\u80fd<\/li>\n<li>Assignment and declaration in deconstruction<br \/>\n\u89e3\u6784\u4e2d\u7684\u8f6c\u8ba9\u548c\u58f0\u660e<\/li>\n<li>Removal of false warnings on definite assignment<br \/>\n\u6d88\u9664\u660e\u786e\u5206\u914d\u7684\u9519\u8bef\u8b66\u544a<\/li>\n<\/ul>\n<p>In addition to this, there are also updates to the .NET framework that impact development. I have updated the section headers that are new (or updated) in C#10 \/NET 6 with \u201c(New 10)\u201d or \u201c(Updated 10\u201d).<br \/>\n\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u5f71\u54cd\u5f00\u53d1\u7684 .NET \u6846\u67b6\u66f4\u65b0\u3002\u6211\u5df2\u7ecf\u5c06 C#10 \/NET 6 \u4e2d\u65b0\u589e\uff08\u6216\u66f4\u65b0\uff09\u7684\u90e8\u5206\u6807\u9898\u66f4\u65b0\u4e3a\u201c\uff08\u65b0 10\uff09\u201d\u6216\u201c\uff08\u66f4\u65b0 10\u201d\uff09\u3002<\/p>\n<h2>Managed vs. Unmanaged Code<\/h2>\n<p>\u6258\u7ba1\u4ee3\u7801\u4e0e\u975e\u6258\u7ba1\u4ee3\u7801<\/p>\n<p>It is important to note that the C# language can be used only to build software that is hosted under the .NET runtime (you could never use C# to build a native COM server or an unmanaged C\/C++-style application). Officially speaking, the term used to describe the code targeting the .NET runtime is managed code. The binary unit that contains the managed code is termed an assembly (more details on assemblies in just a bit). Conversely, code that cannot be directly hosted by the .NET runtime is termed unmanaged code.<br \/>\n\u8bf7\u52a1\u5fc5\u6ce8\u610f\uff0cC# \u8bed\u8a00\u53ea\u80fd\u7528\u4e8e\u751f\u6210\u5728 .NET \u8fd0\u884c\u65f6\u4e0b\u627f\u8f7d\u7684\u8f6f\u4ef6\uff08\u6c38\u8fdc\u4e0d\u80fd\u4f7f\u7528 C# \u751f\u6210\u672c\u673a COM \u670d\u52a1\u5668\u6216\u975e\u6258\u7ba1 C\/C++ \u6837\u5f0f\u7684\u5e94\u7528\u7a0b\u5e8f\uff09\u3002\u6b63\u5f0f\u5730\u8bf4\uff0c\u7528\u4e8e\u63cf\u8ff0\u9762\u5411 .NET \u8fd0\u884c\u65f6\u7684\u4ee3\u7801\u7684\u672f\u8bed\u662f\u6258\u7ba1\u4ee3\u7801\u3002\u5305\u542b\u6258\u7ba1\u4ee3\u7801\u7684\u4e8c\u8fdb\u5236\u5355\u5143\u79f0\u4e3a\u7a0b\u5e8f\u96c6\uff08\u7a0d\u540e\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u7a0b\u5e8f\u96c6\uff09\u3002\u76f8\u53cd\uff0c\u4e0d\u80fd\u7531 .NET \u8fd0\u884c\u65f6\u76f4\u63a5\u627f\u8f7d\u7684\u4ee3\u7801\u79f0\u4e3a\u975e\u6258\u7ba1\u4ee3\u7801\u3002<\/p>\n<p>As mentioned previously, the .NET platform can run on a variety of operating systems. Thus, it is quite possible to build a C# application on a Windows machine and run the program on an iOS machine using the .NET runtime. As well, you can build a C# application on Linux using Visual Studio Code and run the program on Windows. With Visual Studio for Mac, you can also build .NET applications on a Mac to be run on Windows, macOS, or Linux.<br \/>\n\u5982\u524d\u6240\u8ff0\uff0c.NET \u5e73\u53f0\u53ef\u4ee5\u5728\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002\u56e0\u6b64\uff0c\u5f88\u6709\u53ef\u80fd\u5728 Windows \u8ba1\u7b97\u673a\u4e0a\u751f\u6210 C# \u5e94\u7528\u7a0b\u5e8f\u5e76\u4f7f\u7528 .NET \u8fd0\u884c\u65f6\u5728 iOS \u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u8be5\u7a0b\u5e8f\u3002\u540c\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Visual Studio Code\u5728Linux\u4e0a\u6784\u5efaC#\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5728Windows\u4e0a\u8fd0\u884c\u8be5\u7a0b\u5e8f\u3002\u4f7f\u7528 Visual Studio for Mac\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5728 Mac \u4e0a\u6784\u5efa .NET \u5e94\u7528\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u5728 Windows\u3001macOS \u6216 Linux \u4e0a\u8fd0\u884c\u3002<\/p>\n<p>Unmanaged code can still be accessed from a C# program, but it then locks you into a specific development and deployment target.<br \/>\n\u4ecd\u7136\u53ef\u4ee5\u4ece C# \u7a0b\u5e8f\u8bbf\u95ee\u975e\u6258\u7ba1\u4ee3\u7801\uff0c\u4f46\u968f\u540e\u5b83\u4f1a\u5c06\u60a8\u9501\u5b9a\u5230\u7279\u5b9a\u7684\u5f00\u53d1\u548c\u90e8\u7f72\u76ee\u6807\u3002<\/p>\n<h2>Using Additional .NET\u2013Aware Programming Languages<\/h2>\n<p>\u4f7f\u7528\u5176\u4ed6 .NET \u611f\u77e5\u7f16\u7a0b\u8bed\u8a00<\/p>\n<p>Understand that C# is not the only language that can be used to build .NET applications. .NET applications can generally be built with C#, Visual Basic, and F#, which are the three languages supported directly by Microsoft.<br \/>\n\u4e86\u89e3 C# \u5e76\u4e0d\u662f\u53ef\u7528\u4e8e\u751f\u6210 .NET \u5e94\u7528\u7a0b\u5e8f\u7684\u552f\u4e00\u8bed\u8a00\u3002.NET \u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528 C#\u3001Visual Basic \u548c F# \u6784\u5efa\uff0c\u8fd9\u662f Microsoft\u76f4\u63a5\u652f\u6301\u7684\u4e09\u79cd\u8bed\u8a00\u3002<\/p>\n<h2>Getting an Overview of .NET Assemblies<\/h2>\n<p>\u83b7\u53d6 .NET \u7a0b\u5e8f\u96c6\u7684\u6982\u8ff0<\/p>\n<p>Regardless of which .NET language you choose to program with, understand that despite .NET binaries taking the same file extension as unmanaged Windows binaries (<em>.dll), they have absolutely no internal similarities. Specifically, .NET binaries do not contain platform-specific instructions but rather platform- agnostic Intermediate Language (IL) and type metadata.<br \/>\n\u65e0\u8bba\u9009\u62e9\u4f7f\u7528\u54ea\u79cd .NET \u8bed\u8a00\u8fdb\u884c\u7f16\u7a0b\uff0c\u8bf7\u4e86\u89e3\uff0c\u5c3d\u7ba1 .NET \u4e8c\u8fdb\u5236\u6587\u4ef6\u91c7\u7528\u4e0e\u975e\u6258\u7ba1 Windows \u4e8c\u8fdb\u5236\u6587\u4ef6\u76f8\u540c\u7684\u6587\u4ef6\u6269\u5c55\u540d \uff08<\/em>.dll\uff09\uff0c\u4f46\u5b83\u4eec\u7edd\u5bf9\u6ca1\u6709\u5185\u90e8\u76f8\u4f3c\u4e4b\u5904\u3002\u5177\u4f53\u800c\u8a00\uff0c.NET \u4e8c\u8fdb\u5236\u6587\u4ef6\u4e0d\u5305\u542b\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4\uff0c\u800c\u662f\u5305\u542b\u4e0e\u5e73\u53f0\u65e0\u5173\u7684\u4e2d\u95f4\u8bed\u8a00 \uff08IL\uff09 \u548c\u7c7b\u578b\u5143\u6570\u636e\u3002<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nIL is also known as Microsoft Intermediate Language (MSIL) or alternatively as the Common Intermediate Language (CIL). Thus, as you read the .NET literature, understand that IL, MSIL, and CIL are all describing essentially the same concept. In this book, I will use the abbreviation CIL to refer to this low-level instruction set.<br \/>\n\u6ce8\u610f IL \u4e5f\u79f0\u4e3aMicrosoft\u4e2d\u95f4\u8bed\u8a00 \uff08MSIL\uff09 \u6216\u516c\u5171\u4e2d\u95f4\u8bed\u8a00 \uff08CIL\uff09\u3002\u56e0\u6b64\uff0c\u5728\u9605\u8bfb .NET \u6587\u732e\u65f6\uff0c\u8bf7\u4e86\u89e3 IL\u3001MSIL \u548c CIL \u672c\u8d28\u4e0a\u90fd\u63cf\u8ff0\u4e86\u76f8\u540c\u7684\u6982\u5ff5\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u6211\u5c06\u4f7f\u7528\u7f29\u5199CIL\u6765\u6307\u4ee3\u8fd9\u4e2a\u4f4e\u7ea7\u6307\u4ee4\u96c6\u3002\n<\/p><\/blockquote>\n<p>When a <em>.dll has been created using a .NET compiler, the binary blob is termed an assembly. You will examine numerous details of .NET assemblies in Chapter 16. However, to facilitate the current discussion, you do need to understand four basic properties of this new file format.<br \/>\n\u4f7f\u7528 .NET \u7f16\u8bd1\u5668\u521b\u5efa <\/em>.dll \u65f6\uff0c\u4e8c\u8fdb\u5236 blob \u79f0\u4e3a\u7a0b\u5e8f\u96c6\u3002\u60a8\u5c06\u5728\u7b2c 16 \u7ae0\u4e2d\u7814\u7a76 .NET \u7a0b\u5e8f\u96c6\u7684\u5927\u91cf\u7ec6\u8282\u3002\u4f46\u662f\uff0c\u4e3a\u4e86\u4fbf\u4e8e\u5f53\u524d\u7684\u8ba8\u8bba\uff0c\u60a8\u786e\u5b9e\u9700\u8981\u4e86\u89e3\u8fd9\u79cd\u65b0\u6587\u4ef6\u683c\u5f0f\u7684\u56db\u4e2a\u57fa\u672c\u5c5e\u6027\u3002<\/p>\n<p>First, unlike .NET Framework assemblies that can be either a <em>.dll or <\/em>.exe, .NET projects are always compiled to a file with a .dll extension, even if the project is an executable. Executable .NET assemblies are executed with the command dotnet <assembly name>.dll. New in .NET Core 3.0 (and later), the dotnet.exe command is copied to the build directory and renamed to <assembly name>.exe. Running this command automatically calls the dotnet <assembly name>.dll file, executing the equivalent of dotnet <assembly name>.dll. The <em>.exe with your project name isn\u2019t actually your project\u2019s code; it is a convenient shortcut to running your application.<br \/>\n\u9996\u5148\uff0c\u4e0e\u53ef\u4ee5\u662f <\/em>.dll \u6216 <em>.exe \u7684 .NET Framework \u7a0b\u5e8f\u96c6\u4e0d\u540c\uff0c.NET \u9879\u76ee\u59cb\u7ec8\u7f16\u8bd1\u4e3a\u6269\u5c55\u540d\u4e3a .dll \u7684\u6587\u4ef6\uff0c\u5373\u4f7f\u8be5\u9879\u76ee\u662f\u53ef\u6267\u884c\u6587\u4ef6\u4e5f\u662f\u5982\u6b64\u3002\u53ef\u6267\u884c\u7684 .NET \u7a0b\u5e8f\u96c6\u4f7f\u7528\u547d\u4ee4 dotnet&lt;\u7a0b\u5e8f\u96c6\u540d\u79f0&gt;.dll\u6267\u884c\u3002dotnet.exe \u547d\u4ee4\u662f .NET Core 3.0\uff08\u53ca\u66f4\u9ad8\u7248\u672c\uff09\u4e2d\u7684\u65b0\u589e\u529f\u80fd\uff0c\u5b83\u88ab\u590d\u5236\u5230\u751f\u6210\u76ee\u5f55\u5e76\u91cd\u547d\u540d\u4e3a &lt;\u7a0b\u5e8f\u96c6\u540d\u79f0&gt;.exe\u3002\u8fd0\u884c\u6b64\u547d\u4ee4\u4f1a\u81ea\u52a8\u8c03\u7528 dotnet&lt;\u7a0b\u5e8f\u96c6\u540d\u79f0&gt;.dll\u6587\u4ef6\uff0c\u6267\u884c\u7b49\u6548\u7684 dotnet&lt;\u7a0b\u5e8f\u96c6\u540d\u79f0&gt;.dll\u3002\u5e26\u6709\u9879\u76ee\u540d\u79f0\u7684 <\/em>.exe \u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u9879\u76ee\u4ee3\u7801;\u5b83\u662f\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u7684\u4fbf\u6377\u5feb\u6377\u65b9\u5f0f\u3002<\/p>\n<p>Updated in .NET 6, your application can be reduced to a single file that is executed directly. Even though this single file looks and acts like a C++-style native executable, the single file is a packaging convenience. It contains all the files needed to run your application, potentially even the .NET runtime itself! But know that your code is still running in a managed container just as if it were published as multiple files.<br \/>\n\u5728 .NET 6 \u4e2d\u66f4\u65b0\uff0c\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u7f29\u51cf\u4e3a\u76f4\u63a5\u6267\u884c\u7684\u5355\u4e2a\u6587\u4ef6\u3002\u5c3d\u7ba1\u6b64\u5355\u4e2a\u6587\u4ef6\u7684\u5916\u89c2\u548c\u884c\u4e3a\u7c7b\u4f3c\u4e8e C++ \u6837\u5f0f\u7684\u672c\u673a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f46\u5355\u4e2a\u6587\u4ef6\u662f\u4e00\u79cd\u6253\u5305\u4fbf\u5229\u3002\u5b83\u5305\u542b\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u7684\u6240\u6709\u6587\u4ef6\uff0c\u751a\u81f3\u53ef\u80fd\u5305\u542b .NET \u8fd0\u884c\u65f6\u672c\u8eab\uff01\u4f46\u8981\u77e5\u9053\uff0c\u60a8\u7684\u4ee3\u7801\u4ecd\u5728\u6258\u7ba1\u5bb9\u5668\u4e2d\u8fd0\u884c\uff0c\u5c31\u50cf\u5b83\u4f5c\u4e3a\u591a\u4e2a\u6587\u4ef6\u53d1\u5e03\u4e00\u6837\u3002<\/p>\n<p>Second, an assembly contains CIL code, which is conceptually similar to Java bytecode, in that it is not compiled to platform-specific instructions until absolutely necessary. Typically, \u201cabsolutely necessary\u201d is the point at which a block of CIL instructions (such as a method implementation) is referenced for use by the.NET runtime.<br \/>\n\u5176\u6b21\uff0c\u7a0b\u5e8f\u96c6\u5305\u542b CIL \u4ee3\u7801\uff0c\u8fd9\u5728\u6982\u5ff5\u4e0a\u7c7b\u4f3c\u4e8e Java \u5b57\u8282\u7801\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u7f16\u8bd1\u4e3a\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4\uff0c\u9664\u975e\u7edd\u5bf9\u5fc5\u8981\u3002\u901a\u5e38\uff0c\u201c\u7edd\u5bf9\u5fc5\u8981\u201d\u662f\u5f15\u7528 CIL \u6307\u4ee4\u5757\uff08\u4f8b\u5982\u65b9\u6cd5\u5b9e\u73b0\uff09\u4ee5\u4f9b.NET \u8fd0\u884c\u65f6\u3002<\/p>\n<p>Third, assemblies also contain metadata that describes in vivid detail the characteristics of every \u201ctype\u201d within the binary. For example, if you have a class named SportsCar, the type metadata describes details such as SportsCar\u2019s base class, specifies which interfaces are implemented by SportsCar (if any), and gives full descriptions of each member supported by the SportsCar type. .NET metadata is always present within an assembly and is automatically generated by the language compiler.<br \/>\n\u7b2c\u4e09\uff0c\u7a0b\u5e8f\u96c6\u8fd8\u5305\u542b\u5143\u6570\u636e\uff0c\u8fd9\u4e9b\u5143\u6570\u636e\u751f\u52a8\u8be6\u7ec6\u5730\u63cf\u8ff0\u4e86\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u6bcf\u4e2a\u201c\u7c7b\u578b\u201d\u7684\u7279\u5f81\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a SportsCar \u7684\u7c7b\uff0c\u5219\u7c7b\u578b\u5143\u6570\u636e\u63cf\u8ff0 SportsCar \u57fa\u7c7b\u7b49\u8be6\u7ec6\u4fe1\u606f\uff0c\u6307\u5b9a SportsCar \u5b9e\u73b0\u7684\u63a5\u53e3\uff08\u5982\u679c\u6709\uff09\uff0c\u5e76\u63d0\u4f9b SportsCar \u7c7b\u578b\u652f\u6301\u7684\u6bcf\u4e2a\u6210\u5458\u7684\u5b8c\u6574\u8bf4\u660e\u3002 .NET \u5143\u6570\u636e\u59cb\u7ec8\u5b58\u5728\u4e8e\u7a0b\u5e8f\u96c6\u4e2d\uff0c\u5e76\u7531\u8bed\u8a00\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u3002<\/p>\n<p>Finally, in addition to CIL and type metadata, assemblies themselves are also described using metadata, which is officially termed a manifest. The manifest contains information about the current version of the assembly, culture information (used for localizing string and image resources), and a list of all externally referenced assemblies that are required for proper execution. You\u2019ll examine various tools that can be used to examine an assembly\u2019s types, metadata, and manifest information over the course of the next few chapters.<br \/>\n\u6700\u540e\uff0c\u9664\u4e86 CIL \u548c\u7c7b\u578b\u5143\u6570\u636e\u4e4b\u5916\uff0c\u7a0b\u5e8f\u96c6\u672c\u8eab\u4e5f\u4f7f\u7528\u5143\u6570\u636e\u8fdb\u884c\u63cf\u8ff0\uff0c\u6b63\u5f0f\u79f0\u4e3a\u6e05\u5355\u3002\u6e05\u5355\u5305\u542b\u6709\u5173\u7a0b\u5e8f\u96c6\u5f53\u524d\u7248\u672c\u7684\u4fe1\u606f\u3001\u533a\u57df\u6027\u4fe1\u606f\uff08\u7528\u4e8e\u672c\u5730\u5316\u5b57\u7b26\u4e32\u548c\u56fe\u50cf\u8d44\u6e90\uff09\u4ee5\u53ca\u6b63\u786e\u6267\u884c\u6240\u9700\u7684\u6240\u6709\u5916\u90e8\u5f15\u7528\u7a0b\u5e8f\u96c6\u7684\u5217\u8868\u3002\u60a8\u5c06\u68c0\u67e5\u5404\u79cd\u5de5\u5177\u7528\u4e8e\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u7ae0\u4e2d\u68c0\u67e5\u7a0b\u5e8f\u96c6\u7684\u7c7b\u578b\u3001\u5143\u6570\u636e\u548c\u6e05\u5355\u4fe1\u606f\u3002<\/p>\n<h2>The Role of the Common Intermediate Language<\/h2>\n<p>\u516c\u5171\u4e2d\u95f4\u8bed\u8a00\u7684\u4f5c\u7528<\/p>\n<p>Let\u2019s examine CIL code, type metadata, and the assembly manifest in a bit more detail. CIL is a language that sits above any particular platform-specific instruction set. For example, the following C# code models a trivial calculator. Don\u2019t concern yourself with the exact syntax for now, but do notice the format of the Add() method in the Calc class.<br \/>\n\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u68c0\u67e5 CIL \u4ee3\u7801\u3001\u7c7b\u578b\u5143\u6570\u636e\u548c\u7a0b\u5e8f\u96c6\u6e05\u5355\u3002CIL \u662f\u4e00\u79cd\u4f4d\u4e8e\u4efb\u4f55\u7279\u5b9a\u5e73\u53f0\u7279\u5b9a\u6307\u4ee4\u96c6\u4e4b\u4e0a\u7684\u8bed\u8a00\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b C# \u4ee3\u7801\u5bf9\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u8fdb\u884c\u4e86\u5efa\u6a21\u3002\u73b0\u5728\u4e0d\u8981\u5173\u5fc3\u786e\u5207\u7684\u8bed\u6cd5\uff0c\u4f46\u8bf7\u6ce8\u610f Calc \u7c7b\u4e2d Add\uff08\uff09 \u65b9\u6cd5\u7684\u683c\u5f0f\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/Clac.cs or Program.cs\n\/\/\u5728Program.cs\u6587\u4ef6\u4e2d\u7f16\u5199\u4ee3\u7801\u6216\u8005\u5355\u72ec\u7f16\u5199Clac.cs\u6587\u4ef6\n\nCalc c = new Calc();\nint ans = c.Add(10, 84);\nConsole.WriteLine(&quot;10+84 is {0} .&quot;,ans);\n\n\/\/ Wait for user to press the Enter key\n\/\/ \u7b49\u5f85\u7528\u6237\u6309\u4e0bEnter\u952e\nConsole.ReadLine();\n\n\/\/ The C# Calculator .\n\/\/ C# \u4ee3\u7801\u8ba1\u7b97\u5668\nclass Calc\n{\n    public int Add(int addend1, int addend2)\n    {\n        return addend1 + addend2;\n    }\n}\n```\n<\/pre>\n<\/p>\n<p>Compiling this code produces a file <em>.dll assembly that contains a manifest, CIL instructions, and metadata describing each aspect of the Calc and Program classes.<br \/>\n\u7f16\u8bd1\u6b64\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u6587\u4ef6 <\/em>.dll \u7a0b\u5e8f\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u6e05\u5355\u3001CIL \u6307\u4ee4\u548c\u63cf\u8ff0 Calc \u548c Program \u7c7b\u5404\u4e2a\u65b9\u9762\u7684\u5143\u6570\u636e\u3002<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nChapter 2 examines how to use graphical integrated development environments (IdEs), such as Visual Studio Community, to compile your code files.<br \/>\n\u6ce8\u610f \u7b2c 2 \u7ae0\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u56fe\u5f62\u96c6\u6210\u5f00\u53d1\u73af\u5883 \uff08IdE\uff09\uff08\u5982 Visual Studio Community\uff09\u6765\u7f16\u8bd1\u4ee3\u7801\u6587\u4ef6\u3002\n<\/p><\/blockquote>\n<p>For example, if you were to output the IL from this assembly using ildasm.exe (examined later in this chapter), you would find that the Add() method is represented using CIL such as the following:<br \/>\n\u4f8b\u5982\uff0c\u5982\u679c\u8981\u4f7f\u7528 ildasm.exe\uff08\u5728\u672c\u7ae0\u540e\u9762\u8fdb\u884c\u68c0\u67e5\uff09\u4ece\u6b64\u7a0b\u5e8f\u96c6\u8f93\u51fa IL\uff0c\u60a8\u4f1a\u53d1\u73b0 Add\uff08\uff09 \u65b9\u6cd5\u662f\u4f7f\u7528 CIL \u8868\u793a\u7684\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n.method public hidebysig instance int32 Add(int32 addend1,\n              int32 addend2) cil managed\n{\n\/\/ Method begins at RVA 0x2090\n\/\/ Code size        9 (0x9)\n.maxstack 2\n.locals \/*11000002*\/ init (int32 V_0)\nIL_0000: \/* 00 |        *\/ nop\nIL_0001: \/* 03 |        *\/ ldarg.1\nIL_0002: \/* 04 |        *\/ ldarg.2\nIL_0003: \/* 58 |        *\/ add\nIL_0004: \/* 0A |        *\/ stloc.0\nIL_0005: \/* 2B | 00     *\/ br.s IL_0007\nIL_0007: \/* 06 |        *\/ ldloc.0\nIL_0008: \/* 2A |        *\/ ret\n} \/\/ end of method Calc::Add\n<\/pre>\n<\/p>\n<p>Don\u2019t worry if you are unable to make heads or tails of the resulting CIL for this method because Chapter 18 will describe the basics of the CIL programming language. The point to concentrate on is that the C# compiler emits CIL, not platform-specific instructions.<br \/>\n\u5982\u679c\u60a8\u65e0\u6cd5\u4e3a\u6b64\u65b9\u6cd5\u751f\u6210 CIL \u7684\u6b63\u9762\u6216\u53cd\u9762\uff0c\u8bf7\u4e0d\u8981\u62c5\u5fc3\uff0c\u56e0\u4e3a\u7b2c 18 \u7ae0\u5c06\u63cf\u8ff0 CIL \u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u9700\u8981\u5173\u6ce8\u7684\u4e00\u70b9\u662f C# \u7f16\u8bd1\u5668\u53d1\u51fa CIL\uff0c\u800c\u4e0d\u662f\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4\u3002<\/p>\n<p>Now, recall that this is true of all .NET compilers. To illustrate, assume you created this same application using Visual Basic, rather than C#.<br \/>\n\u73b0\u5728\uff0c\u56de\u60f3\u4e00\u4e0b\uff0c\u6240\u6709 .NET \u7f16\u8bd1\u5668\u90fd\u662f\u5982\u6b64\u3002\u4e3a\u4e86\u8bf4\u660e\u8fd9\u4e00\u70b9\uff0c\u5047\u8bbe\u60a8\u4f7f\u7528 Visual Basic \u800c\u4e0d\u662f C# \u521b\u5efa\u4e86\u76f8\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n```\nREM: Calc.vb\nModule Program\n    REM: This class contains the app&amp;#039;s entry point.\n    Sub Main(args As String())\n        Dim c As New Calc\n        Dim ans As Integer = c.Add(10, 84)\n        Console.WriteLine(&quot;10 + 84 is {0}&quot;, ans)\n        REM: Wait for user to press the Enter key before shutting down\n        Console.ReadLine()\n    End Sub\nEnd Module\nREM: The VB.NET calculator.\nClass Calc\n    Public Function Add(ByVal addend1 As Integer, ByVal addend2 As Integer) As Integer\n        Return addend1 + addend2\n    End Function\nEnd Class\n```\n<\/pre>\n<\/p>\n<p>If you examine the CIL for the Add() method, you find similar instructions (slightly tweaked by the Visual Basic compiler).<br \/>\n\u5982\u679c\u60a8\u68c0\u67e5 Add()\u65b9\u6cd5\u7684 CIL\uff0c\u60a8\u4f1a\u53d1\u73b0\u7c7b\u4f3c\u7684\u6307\u4ee4\uff08Visual Basic \u7f16\u8bd1\u5668\u7565\u6709\u8c03\u6574\uff09\u3002<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n.method public instance int32 Add(int32 addend1,\n                    int32 addend2) cil managed\n{\n\/\/ Code size     9 (0x9)\n.maxstack 2\n.locals init (int32 V_0)\nIL_0000: nop\nIL_0001: ldarg.1\nIL_0002: ldarg.2\nIL_0003: add.ovf\nIL_0004: stloc.0\nIL_0005: br.s    IL_0007\nIL_0007: ldloc.0\nIL_0008: ret\n} \/\/ end of method Calc::Add\n<\/pre>\n<\/p>\n<p>As a final example, the same simple Calc program developed in F# (another .NET language) is shown here:<br \/>\n\u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u793a\u4f8b\uff0c\u4e0b\u9762\u663e\u793a\u4e86\u7528 F#\uff08\u53e6\u4e00\u79cd .NET \u8bed\u8a00\uff09\u5f00\u53d1\u7684\u76f8\u540c\u7b80\u5355 Calc \u7a0b\u5e8f\uff1a<\/p>\n<p><pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\n\/\/ Learn more about F# at http:\/\/fsharp.org\n\/\/ Calc.fs\nopen System\n\nmodule Calc =\n    let add addend1 addend2 =\n        addend1 + addend2\n\n&#x5B;&lt;EntryPoint&gt;]\nlet main argv =\n    let ans = Calc.add 10 84\n    printfn &quot;10 + 84 is %d&quot; ans\n    Console.ReadLine()\n    0\n<\/pre>\n<\/p>\n<p>If you examine the CIL for the Add() method, once again you find similar instructions (slightly tweaked by the F# compiler).<br \/>\n\u5982\u679c\u68c0\u67e5 Add()\u65b9\u6cd5\u7684 CIL\uff0c\u5219\u4f1a\u518d\u6b21\u53d1\u73b0\u7c7b\u4f3c\u7684\u6307\u4ee4\uff08F# \u7f16\u8bd1\u5668\u7565\u6709\u8c03\u6574\uff09\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n.method public instance int32 Add(int32 addend1,\n                    int32 addend2) cil managed\n{\n\/\/ Code size 9 (0x9)\n.maxstack 2\n.locals init (int32 V_0)\nIL_0000: nop\nIL_0001: ldarg.1\nIL_0002: ldarg.2\nIL_0003: add.ovf\nIL_0004: stloc.0\nIL_0005: br.s        IL_0007\n\nIL_0007: ldloc.0\nIL_0008: ret\n} \/\/ end of method Calc::Add\n\n<\/pre>\n<\/p>\n<h2>Benefits of CIL<\/h2>\n<p>CIL\u7684\u597d\u5904<\/p>\n<p>At this point, you might be wondering exactly what is gained by compiling source code into CIL rather than directly to a specific instruction set. One benefit is language integration. As you have already seen, each .NET compiler produces nearly identical CIL instructions. Therefore, all languages are able to interact within a well-defined binary arena.<br \/>\n\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u60a8\u53ef\u80fd\u60f3\u77e5\u9053\u901a\u8fc7\u5c06\u6e90\u4ee3\u7801\u7f16\u8bd1\u5230 CIL \u800c\u4e0d\u662f\u76f4\u63a5\u7f16\u8bd1\u5230\u7279\u5b9a\u6307\u4ee4\u96c6\u4f1a\u5f97\u5230\u4ec0\u4e48\u3002\u4e00\u4e2a\u597d\u5904\u662f\u8bed\u8a00\u96c6\u6210\u3002\u5982\u60a8\u6240\u89c1\uff0c\u6bcf\u4e2a .NET \u7f16\u8bd1\u5668\u90fd\u4f1a\u751f\u6210\u51e0\u4e4e\u76f8\u540c\u7684 CIL \u6307\u4ee4\u3002\u56e0\u6b64\uff0c\u6240\u6709\u8bed\u8a00\u90fd\u80fd\u591f\u5728\u660e\u786e\u5b9a\u4e49\u7684\u4e8c\u8fdb\u5236\u9886\u57df\u5185\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p>Furthermore, given that CIL is platform-agnostic, the .NET Framework itself is platform-agnostic, providing the same benefits Java developers have grown accustomed to (e.g., a single code base running on numerous operating systems). In fact, there is an international standard for the C# language. Prior to .NET Core, there were numerous implementations of the .NET framework for non-Windows platforms, such as Mono. These still exist, although the release of .NET 6 greatly reduced the need for those other platforms.<br \/>\n\u6b64\u5916\uff0c\u9274\u4e8eCIL\u4e0e\u5e73\u53f0\u65e0\u5173\uff0c.NET Framework\u672c\u8eab\u4e0e\u5e73\u53f0\u65e0\u5173\uff0c\u63d0\u4f9b\u4e86Java\u5f00\u53d1\u4eba\u5458\u5df2\u7ecf\u4e60\u60ef\u7684\u76f8\u540c\u597d\u5904\uff08\u4f8b\u5982\uff0c\u5728\u4f17\u591a\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u7684\u5355\u4e2a\u4ee3\u7801\u5e93\uff09\u3002\u4e8b\u5b9e\u4e0a\uff0cC# \u8bed\u8a00\u6709\u4e00\u4e2a\u56fd\u9645\u6807\u51c6\u3002\u5728.NET Core\u4e4b\u524d\uff0c\u6709\u8bb8\u591a\u7528\u4e8e\u975eWindows\u5e73\u53f0\uff08\u5982Mono\uff09\u7684.NET\u6846\u67b6\u5b9e\u73b0\u3002\u8fd9\u4e9b\u4ecd\u7136\u5b58\u5728\uff0c\u5c3d\u7ba1 .NET 6 \u7684\u53d1\u5e03\u5927\u5927\u51cf\u5c11\u4e86\u5bf9\u8fd9\u4e9b\u5176\u4ed6\u5e73\u53f0\u7684\u9700\u6c42\u3002<\/p>\n<h2>Compiling CIL to Platform-Specific Instructions<\/h2>\n<p>\u5c06 CIL \u7f16\u8bd1\u4e3a\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4<\/p>\n<p>Because assemblies contain CIL instructions rather than platform-specific instructions, CIL code must be compiled on the fly before use. The entity that compiles CIL code into meaningful CPU instructions is a JIT compiler, which sometimes goes by the friendly name of jitter. The .NET runtime environment leverages a JIT compiler for each CPU targeting the runtime, each optimized for the underlying platform.<br \/>\n\u7531\u4e8e\u7a0b\u5e8f\u96c6\u5305\u542b CIL \u6307\u4ee4\u800c\u4e0d\u662f\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4\uff0c\u56e0\u6b64\u5fc5\u987b\u5728\u4f7f\u7528\u524d\u52a8\u6001\u7f16\u8bd1 CIL \u4ee3\u7801\u3002\u5c06 CIL \u4ee3\u7801\u7f16\u8bd1\u4e3a\u6709\u610f\u4e49\u7684 CPU \u6307\u4ee4\u7684\u5b9e\u4f53\u662f JIT \u7f16\u8bd1\u5668\uff0c\u5b83\u6709\u65f6\u4f7f\u7528\u53cb\u597d\u540d\u79f0 jitter\u3002.NET \u8fd0\u884c\u65f6\u73af\u5883\u4e3a\u9762\u5411\u8fd0\u884c\u65f6\u7684\u6bcf\u4e2a CPU \u5229\u7528\u4e00\u4e2a JIT \u7f16\u8bd1\u5668\uff0c\u6bcf\u4e2a CPU \u90fd\u9488\u5bf9\u57fa\u7840\u5e73\u53f0\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<p>For example, if you are building a .NET application to be deployed to a handheld device (such as an iOS or Android phone), the corresponding jitter is well equipped to run within a low-memory environment. On the other hand, if you are deploying your assembly to a back-end company server (where memory is seldom an issue), the jitter will be optimized to function in a high-memory environment. In this way, developers can write a single body of code that can be efficiently JIT compiled and executed on machines with different architectures.<br \/>\n\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6b63\u5728\u6784\u5efa\u8981\u90e8\u7f72\u5230\u624b\u6301\u8bbe\u5907\uff08\u5982 iOS \u6216 Android \u624b\u673a\uff09\u7684 .NET \u5e94\u7528\u7a0b\u5e8f\uff0c\u5219\u76f8\u5e94\u7684\u6296\u52a8\u975e\u5e38\u9002\u5408\u5728\u4f4e\u5185\u5b58\u73af\u5883\u4e2d\u8fd0\u884c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u8981\u5c06\u7a0b\u5e8f\u96c6\u90e8\u7f72\u5230\u540e\u7aef\u516c\u53f8\u670d\u52a1\u5668\uff08\u5185\u5b58\u5f88\u5c11\u51fa\u73b0\u95ee\u9898\uff09\uff0c\u5219\u6296\u52a8\u5c06\u7ecf\u8fc7\u4f18\u5316\uff0c\u4ee5\u4fbf\u5728\u9ad8\u5185\u5b58\u73af\u5883\u4e2d\u8fd0\u884c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u7f16\u5199\u5355\u4e2a\u4ee3\u7801\u4f53\uff0c\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5728\u5177\u6709\u4e0d\u540c\u4f53\u7cfb\u7ed3\u6784\u7684\u8ba1\u7b97\u673a\u4e0a\u6709\u6548\u5730\u8fdb\u884c JIT \u7f16\u8bd1\u548c\u6267\u884c\u3002<br \/>\nFurthermore, as a given jitter compiles CIL instructions into corresponding machine code, it will cache the results in memory in a manner suited to the target operating system. In this way, if a call is made to a method named PrintDocument(), the CIL instructions are compiled into platform-specific instructions on the first invocation and retained in memory for later use. Therefore, the next time PrintDocument() is called, there is no need to recompile the CIL.<br \/>\n\u6b64\u5916\uff0c\u5f53\u7ed9\u5b9a\u7684\u6296\u52a8\u5c06CIL\u6307\u4ee4\u7f16\u8bd1\u6210\u76f8\u5e94\u7684\u673a\u5668\u4ee3\u7801\u65f6\uff0c\u5b83\u5c06\u4ee5\u9002\u5408\u76ee\u6807\u64cd\u4f5c\u7cfb\u7edf\u7684\u65b9\u5f0f\u5c06\u7ed3\u679c\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\u3002\u8fd9\u6837\uff0c\u5982\u679c\u8c03\u7528\u540d\u4e3a PrintDocument\uff08\uff09 \u7684\u65b9\u6cd5\uff0cCIL \u6307\u4ee4\u5c06\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u7f16\u8bd1\u4e3a\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4\uff0c\u5e76\u4fdd\u7559\u5728\u5185\u5b58\u4e2d\u4f9b\u4ee5\u540e\u4f7f\u7528\u3002\u56e0\u6b64\uff0c\u4e0b\u6b21\u8c03\u7528 PrintDocument\uff08\uff09 \u65f6\uff0c\u65e0\u9700\u91cd\u65b0\u7f16\u8bd1 CIL\u3002<\/p>\n<h2>Precompiling CIL to Platform-Specific Instructions<\/h2>\n<p>\u5c06 CIL \u9884\u7f16\u8bd1\u4e3a\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4<\/p>\n<p>There is a utility in .NET called crossgen.exe, which can be used to pre-JIT your code. Fortunately, in .NET Core 6, the ability to produce \u201cready-to-run\u201d assemblies is built into the framework. More on this later in this book.<br \/>\n.NET \u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a crossgen.exe \u7684\u5b9e\u7528\u7a0b\u5e8f\uff0c\u53ef\u7528\u4e8e\u9884 JIT \u4ee3\u7801\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5728 .NET Core 6 \u4e2d\uff0c\u751f\u6210\u201c\u5373\u7528\u578b\u201d\u7a0b\u5e8f\u96c6\u7684\u529f\u80fd\u5185\u7f6e\u4e8e\u6846\u67b6\u4e2d\u3002\u672c\u4e66\u540e\u9762\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u70b9\u3002<\/p>\n<h2>The Role of .NET Type Metadata.NET<\/h2>\n<p>\u7c7b\u578b\u5143\u6570\u636e\u7684\u4f5c\u7528<\/p>\n<p>In addition to CIL instructions, a .NET assembly contains full, complete, and accurate metadata, which describes every type (e.g., class, structure, enumeration) defined in the binary, as well as the members of each type (e.g., properties, methods, events). Thankfully, it is always the job of the compiler (not the programmer) to emit the latest and greatest type metadata. Because .NET metadata is so wickedly meticulous, assemblies are completely self-describing entities.<br \/>\n\u9664\u4e86 CIL \u6307\u4ee4\u4e4b\u5916\uff0c.NET \u7a0b\u5e8f\u96c6\u8fd8\u5305\u542b\u5b8c\u6574\u3001\u5b8c\u6574\u4e14\u51c6\u786e\u7684\u5143\u6570\u636e\uff0c\u8fd9\u4e9b\u5143\u6570\u636e\u63cf\u8ff0\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u6bcf\u79cd\u7c7b\u578b\uff08\u4f8b\u5982\uff0c\u7c7b\u3001\u7ed3\u6784\u3001\u679a\u4e3e\uff09\u4ee5\u53ca\u6bcf\u79cd\u7c7b\u578b\u7684\u6210\u5458\uff08\u4f8b\u5982\uff0c\u5c5e\u6027\u3001\u65b9\u6cd5\u3001\u4e8b\u4ef6\uff09\u3002\u503c\u5f97\u5e86\u5e78\u7684\u662f\uff0c\u8fd9\u59cb\u7ec8\u662f\u7f16\u8bd1\u5668\u7684\u5de5\u4f5c\uff08\u4e0d\u662f\u7a0b\u5e8f\u5458\uff09\u53d1\u51fa\u6700\u65b0\u548c\u6700\u597d\u7684\u7c7b\u578b\u5143\u6570\u636e\u3002\u7531\u4e8e .NET \u5143\u6570\u636e\u975e\u5e38\u7ec6\u81f4\uff0c\u56e0\u6b64\u7a0b\u5e8f\u96c6\u662f\u5b8c\u5168\u81ea\u63cf\u8ff0\u7684\u5b9e\u4f53\u3002<\/p>\n<p>To illustrate the format of .NET type metadata, let\u2019s take a look at the metadata that has been generated for the Add() method of the C# Calc class you examined previously (the metadata generated for the Visual Basic version of the Add() method is similar, so we will examine the C# version only).<br \/>\n\u4e3a\u4e86\u8bf4\u660e .NET \u7c7b\u578b\u5143\u6570\u636e\u7684\u683c\u5f0f\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u4e3a\u524d\u9762\u68c0\u67e5\u7684 C# Calc \u7c7b\u7684 Add\uff08\uff09 \u65b9\u6cd5\u751f\u6210\u7684\u5143\u6570\u636e\uff08\u4e3a Add\uff08\uff09 \u65b9\u6cd5\u7684 Visual Basic \u7248\u672c\u751f\u6210\u7684\u5143\u6570\u636e\u7c7b\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u4ec5\u68c0\u67e5 C# \u7248\u672c\uff09\u3002<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ TypeDef #2 (02000003)\n\/\/ -------------------------------------------------------\n\/\/ TypDefName: Calc (02000003)\n\/\/ Flags : &#x5B;NotPublic] &#x5B;AutoLayout] &#x5B;Class] &#x5B;AnsiClass] &#x5B;BeforeFieldInit] (00100000)\n\/\/ Extends : 0100000D &#x5B;TypeRef] System.Object\n\/\/ Method #1 (06000003)\n\/\/ -------------------------------------------------------\n\/\/ MethodName: Add (06000003)\n\/\/ Flags : &#x5B;Public] &#x5B;HideBySig] &#x5B;ReuseSlot] (00000086)\n\/\/ RVA : 0x00002090\n\/\/ ImplFlags : &#x5B;IL] &#x5B;Managed] (00000000)\n\/\/ CallCnvntn: &#x5B;DEFAULT]\n\/\/ hasThis\n\/\/ ReturnType: I4\n\/\/ 2 Arguments\n\/\/ Argument #1: I4\n\/\/ Argument #2: I4\n\/\/ 2 Parameters\n\/\/ (1) ParamToken : (08000002) Name : addend1 flags: &#x5B;none] (00000000)\n\/\/ (2) ParamToken : (08000003) Name : addend2 flags: &#x5B;none] (00000000)\n<\/pre>\n<\/p>\n<p>Metadata is used by numerous aspects of the .NET runtime environment, as well as by various development tools. For example, the IntelliSense feature provided by tools such as Visual Studio is made possible by reading an assembly\u2019s metadata at design time. Metadata is also used by various object-browsing utilities, debugging tools, and the C# compiler itself. To be sure, metadata is the backbone of numerous .NET technologies including reflection, late binding, and object serialization. Chapter 17 will formalize the role of .NET metadata.<br \/>\n\u5143\u6570\u636e\u7531 .NET \u8fd0\u884c\u65f6\u73af\u5883\u7684\u8bb8\u591a\u65b9\u9762\u4ee5\u53ca\u5404\u79cd\u5f00\u53d1\u5de5\u5177\u4f7f\u7528\u3002\u4f8b\u5982\uff0cVisual Studio \u7b49\u5de5\u5177\u63d0\u4f9b\u7684\u667a\u80fd\u611f\u77e5\u529f\u80fd\u662f\u901a\u8fc7\u5728\u8bbe\u8ba1\u65f6\u8bfb\u53d6\u7a0b\u5e8f\u96c6\u7684\u5143\u6570\u636e\u6765\u5b9e\u73b0\u7684\u3002\u5143\u6570\u636e\u8fd8\u7531\u5404\u79cd\u5bf9\u8c61\u6d4f\u89c8\u5b9e\u7528\u7a0b\u5e8f\u3001\u8c03\u8bd5\u5de5\u5177\u548c C# \u7f16\u8bd1\u5668\u672c\u8eab\u4f7f\u7528\u3002\u53ef\u4ee5\u80af\u5b9a\u7684\u662f\uff0c\u5143\u6570\u636e\u662f\u8bb8\u591a .NET \u6280\u672f\u7684\u652f\u67f1\uff0c\u5305\u62ec\u53cd\u5c04\u3001\u540e\u671f\u7ed1\u5b9a\u548c\u5bf9\u8c61\u5e8f\u5217\u5316\u3002\u7b2c17\u7ae0\u5c06\u6b63\u5f0f\u786e\u5b9a.NET \u5143\u6570\u636e\u3002<\/p>\n<h2>The Role of the Assembly Manifest<\/h2>\n<p>\u7a0b\u5e8f\u96c6\u6e05\u5355\u7684\u4f5c\u7528<\/p>\n<p>Last but not least, remember that a .NET assembly also contains metadata that describes the assembly itself (technically termed a manifest). Among other details, the manifest documents all external assemblies required by the current assembly to function correctly, the assembly\u2019s version number, copyright information, and so forth. Like type metadata, it is always the job of the compiler to generate the assembly\u2019s manifest. Here are some relevant details of the manifest generated when compiling the Calc.cs code file shown earlier in this chapter (some lines omitted for brevity):<br \/>\n\u6700\u540e\u4f46\u5e76\u975e\u6700\u4e0d\u91cd\u8981\u7684\u4e00\u70b9\u662f\uff0c\u8bf7\u8bb0\u4f4f\uff0c.NET \u7a0b\u5e8f\u96c6\u8fd8\u5305\u542b\u63cf\u8ff0\u7a0b\u5e8f\u96c6\u672c\u8eab\u7684\u5143\u6570\u636e\uff08\u6280\u672f\u4e0a\u79f0\u4e3a\u6e05\u5355\uff09\u3002\u9664\u5176\u4ed6\u8be6\u7ec6\u4fe1\u606f\u5916\uff0c\u6e05\u5355\u8fd8\u8bb0\u5f55\u4e86\u5f53\u524d\u7a0b\u5e8f\u96c6\u6b63\u5e38\u8fd0\u884c\u6240\u9700\u7684\u6240\u6709\u5916\u90e8\u7a0b\u5e8f\u96c6\u3001\u7a0b\u5e8f\u96c6\u7684\u7248\u672c\u53f7\u3001\u7248\u6743\u4fe1\u606f\u7b49\u3002\u4e0e\u7c7b\u578b\u5143\u6570\u636e\u4e00\u6837\uff0c\u7f16\u8bd1\u5668\u7684\u5de5\u4f5c\u59cb\u7ec8\u662f\u751f\u6210\u7a0b\u5e8f\u96c6\u7684\u6e05\u5355\u3002\u4ee5\u4e0b\u662f\u7f16\u8bd1\u672c\u7ae0\u524d\u9762\u6240\u793a\u7684 Calc.cs \u4ee3\u7801\u6587\u4ef6\u65f6\u751f\u6210\u7684\u6e05\u5355\u7684\u4e00\u4e9b\u76f8\u5173\u8be6\u7ec6\u4fe1\u606f\uff08\u4e3a\u7b80\u6d01\u8d77\u89c1\uff0c\u7701\u7565\u4e86\u4e00\u4e9b\u884c\uff09\uff1a<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n.assembly extern \/*23000001*\/ System.Runtime\n{\n.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) \/\/ .?_....:\n.ver 6:0:0:0\n}\n.assembly extern \/*23000002*\/ System.Console\n{\n.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) \/\/ .?_....:\n.ver 6:0:0:0\n}\n.assembly \/*20000001*\/ Calc.Cs\n{\n.hash algorithm 0x00008004\n.ver 1:0:0:0\n}\n.module Calc.Cs.dll\n.imagebase 0x00400000\n.file alignment 0x00000200\n.stackreserve 0x00100000\n.subsystem 0x0003 \/\/ WINDOWS_CUI\n.corflags 0x00000001 \/\/ ILONLY\n<\/pre>\n<\/p>\n<p>In a nutshell, the manifest documents the set of external assemblies required by Calc.dll (via the .assembly extern directive) as well as various characteristics of the assembly itself (e.g., version number, module name). Chapter 16 will examine the usefulness of manifest data in much more detail.<br \/>\n\u7b80\u800c\u8a00\u4e4b\uff0c\u6e05\u5355\u8bb0\u5f55\u4e86 Calc \u6240\u9700\u7684\u5916\u90e8\u7a0b\u5e8f\u96c6\u96c6.dll\uff08\u901a\u8fc7.assembly extern\u6307\u4ee4\uff09\u4ee5\u53ca\u7a0b\u5e8f\u96c6\u672c\u8eab\u7684\u5404\u79cd\u7279\u5f81\uff08\u4f8b\u5982\uff0c\u7248\u672c\u53f7\uff0c\u6a21\u5757\u540d\u79f0\uff09\u3002\u7b2c16\u7ae0\u5c06\u66f4\u8be6\u7ec6\u5730\u7814\u7a76\u6e05\u5355\u6570\u636e\u7684\u6709\u7528\u6027\u3002<\/p>\n<h2>Understanding the Common Type System<\/h2>\n<p>\u4e86\u89e3\u901a\u7528\u7c7b\u578b\u7cfb\u7edf<\/p>\n<p>A given assembly may contain any number of distinct types. In the world of .NET, type is simply a general term used to refer to a member from the set {class, interface, structure, enumeration, delegate}. When you build solutions using a .NET language, you will most likely interact with many of these types. For example, your assembly might define a single class that implements some number of interfaces. Perhaps one of the interface methods takes an enumeration type as an input parameter and returns a structure to the caller.<br \/>\n\u7ed9\u5b9a\u7a0b\u5e8f\u96c6\u53ef\u80fd\u5305\u542b\u4efb\u610f\u6570\u91cf\u7684\u4e0d\u540c\u7c7b\u578b\u3002\u5728 .NET \u4e16\u754c\u4e2d\uff0c\u7c7b\u578b\u53ea\u662f\u4e00\u4e2a\u901a\u7528\u672f\u8bed\uff0c\u7528\u4e8e\u5f15\u7528\u96c6\u5408 {\u7c7b\u3001\u63a5\u53e3\u3001\u7ed3\u6784\u3001\u679a\u4e3e\u3001\u59d4\u6258} \u4e2d\u7684\u6210\u5458\u3002\u4f7f\u7528 .NET \u8bed\u8a00\u751f\u6210\u89e3\u51b3\u65b9\u6848\u65f6\uff0c\u5f88\u53ef\u80fd\u4f1a\u4e0e\u5176\u4e2d\u8bb8\u591a\u7c7b\u578b\u8fdb\u884c\u4ea4\u4e92\u3002\u4f8b\u5982\uff0c\u7a0b\u5e8f\u96c6\u53ef\u80fd\u5b9a\u4e49\u5b9e\u73b0\u4e00\u5b9a\u6570\u91cf\u63a5\u53e3\u7684\u5355\u4e2a\u7c7b\u3002\u4e5f\u8bb8\u5176\u4e2d\u4e00\u4e2a\u63a5\u53e3\u65b9\u6cd5\u5c06\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\uff0c\u5e76\u5c06\u7ed3\u6784\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\u3002<\/p>\n<p>Recall that the CTS is a formal specification that documents how types must be defined in order to be hosted by the .NET Runtime. Typically, the only individuals who are deeply concerned with the inner workings of the CTS are those building tools and\/or compilers that target the .NET platform. It is important, however, for all .NET programmers to learn about how to work with the five types defined by the CTS in their language of choice. The following is a brief overview.<br \/>\n\u56de\u60f3\u4e00\u4e0b\uff0cCTS \u662f\u4e00\u4e2a\u6b63\u5f0f\u89c4\u8303\uff0c\u5b83\u8bb0\u5f55\u4e86\u5fc5\u987b\u5982\u4f55\u5b9a\u4e49\u7c7b\u578b\u624d\u80fd\u7531 .NET \u8fd0\u884c\u65f6\u6258\u7ba1\u3002\u901a\u5e38\uff0c\u552f\u4e00\u6df1\u5207\u5173\u6ce8\u5185\u5728\u7684\u4ebaCTS \u7684\u5de5\u4f5c\u539f\u7406\u662f\u90a3\u4e9b\u9762\u5411 .NET \u5e73\u53f0\u7684\u6784\u5efa\u5de5\u5177\u548c\/\u6216\u7f16\u8bd1\u5668\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u6240\u6709 .NET \u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u4ed6\u4eec\u9009\u62e9\u7684\u8bed\u8a00\u4e2d CTS \u5b9a\u4e49\u7684\u4e94\u79cd\u7c7b\u578b\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u7b80\u8981\u6982\u8ff0\u3002<\/p>\n<h2>CTS Class TypesCTS<\/h2>\n<p>\u7c7b\u7c7b\u578b<br \/>\nEvery .NET language supports, at the least, the notion of a class type, which is the cornerstone of object- oriented programming (OOP). A class may be composed of any number of members (such as constructors, properties, methods, and events) and data points (fields). In C#, classes are declared using the class keyword, like so:<br \/>\n\u6bcf\u79cd .NET \u8bed\u8a00\u81f3\u5c11\u90fd\u652f\u6301\u7c7b\u7c7b\u578b\u7684\u6982\u5ff5\uff0c\u8fd9\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b \uff08OOP\uff09 \u7684\u57fa\u77f3\u3002\u7c7b\u53ef\u4ee5\u7531\u4efb\u610f\u6570\u91cf\u7684\u6210\u5458\uff08\u5982\u6784\u9020\u51fd\u6570\u3001\u5c5e\u6027\u3001\u65b9\u6cd5\u548c\u4e8b\u4ef6\uff09\u548c\u6570\u636e\u70b9\uff08\u5b57\u6bb5\uff09\u7ec4\u6210\u3002\u5728 C# \u4e2d\uff0c\u7c7b\u662f\u4f7f\u7528 class \u5173\u952e\u5b57\u58f0\u660e\u7684\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/ A C# class type with 1 method. \n\/\/ \u5177\u6709 1 \u4e2a\u65b9\u6cd5\u7684 C# \u7c7b\u7c7b\u578b\u3002\nclass Calc\n{\n    public int Add(int addend1, int addend2)\n    {\n        return addend1 + addend2;\n    }\n}\n```\n<\/pre>\n<\/p>\n<p>Chapter 5 will begin your formal examination of building class types with C#; however, Table 1-1 documents a number of characteristics pertaining to class types.<br \/>\n\u7b2c 5 \u7ae0\u5c06\u5f00\u59cb\u5bf9\u4f7f\u7528 C# \u6784\u5efa\u7c7b\u7c7b\u578b\u8fdb\u884c\u6b63\u5f0f\u68c0\u67e5;\u4f46\u662f\uff0c\u8868 1-1 \u8bb0\u5f55\u4e86\u8bb8\u591a\u4e0e\u7c7b\u7c7b\u578b\u76f8\u5173\u7684\u7279\u5f81\u3002<\/p>\n<p>Table 1-1. CTS Class Characteristics<br \/>\n\u8868 1-1. CTS \u7c7b\u7279\u5f81<\/p>\n<table >\n<tr>\n<th>\nClass Characteristic<br \/>\n\u7c7b\u7279\u5f81<\/th>\n<th>\nMeaning in Life<br \/>\n\u610f\u4e49<\/th>\n<\/tr>\n<tr>\n<td>\nIs the class sealed?<br \/>\n\u7c7b\u662f\u5bc6\u5c01\u7684\u5417\uff1f\n<\/td>\n<td>\nSealed classes cannot function as a base class to other classes.<br \/>\n\u5bc6\u5c01\u7c7b\u4e0d\u80fd\u5145\u5f53\u5176\u4ed6\u7c7b\u7684\u57fa\u7c7b\u3002<\/td>\n<\/tr>\n<tr>\n<td>\nDoes the class implement any interfaces?<br \/>\n\u8be5\u7c7b\u662f\u5426\u5b9e\u73b0\u4efb\u4f55\u63a5\u53e3\uff1f<\/td>\n<td>\nAn interface is a collection of abstract members that provides a contract between the object and object user. The CTS allows a class to implement any number of interfaces.<br \/>\n\u63a5\u53e3\u662f\u62bd\u8c61\u6210\u5458\u7684\u96c6\u5408\uff0c\u5b83\u63d0\u4f9b\u5bf9\u8c61\u548c\u5bf9\u8c61\u7528\u6237\u4e4b\u95f4\u7684\u534f\u5b9a\u3002CTS \u5141\u8bb8\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4efb\u610f\u6570\u91cf\u7684\u63a5\u53e3\u3002\n<\/td>\n<\/tr>\n<tr>\n<td>\nIs the class abstract or concrete?<br \/>\n\u7c7b\u662f\u62bd\u8c61\u7684\u8fd8\u662f\u5177\u4f53\u7684\uff1f\n<\/td>\n<td>\nAbstract classes cannot be directly instantiated but are intended to define common behaviors for derived types. Concrete classes can be instantiated directly.<br \/>\n\u62bd\u8c61\u7c7b\u4e0d\u80fd\u76f4\u63a5\u5b9e\u4f8b\u5316\uff0c\u4f46\u65e8\u5728\u5b9a\u4e49\u6d3e\u751f\u7c7b\u578b\u7684\u5e38\u89c1\u884c\u4e3a\u3002\u5177\u4f53\u7c7b\u53ef\u4ee5\u76f4\u63a5\u5b9e\u4f8b\u5316\u3002\n <\/td>\n<\/tr>\n<tr>\n<td>\nWhat is the visibility of this class?<br \/>\n\u8fd9\u4e2a\u7c7b\u7684\u53ef\u89c1\u6027\u662f\u591a\u5c11\uff1f\n<\/td>\n<td>\nEach class must be configured with a visibility keyword such as public or internal. Basically, this controls whether the class may be used by external assemblies or only from within the defining assembly.<br \/>\n \u6bcf\u4e2a\u7c7b\u90fd\u5fc5\u987b\u914d\u7f6e\u4e00\u4e2a\u53ef\u89c1\u6027\u5173\u952e\u5b57\uff0c\u4f8b\u5982\u516c\u5171\u6216\u5185\u90e8\u3002\u57fa\u672c\u4e0a\uff0c\u8fd9\u63a7\u5236\u7c7b\u662f\u53ef\u4ee5\u7531\u5916\u90e8\u7a0b\u5e8f\u96c6\u4f7f\u7528\u8fd8\u662f\u53ea\u80fd\u4ece\u5b9a\u4e49\u7a0b\u5e8f\u96c6\u5185\u90e8\u4f7f\u7528\u3002\n<\/td>\n<\/tr>\n<\/table>\n<h2>CTS Interface Types<\/h2>\n<p>CTS \u63a5\u53e3\u7c7b\u578b<\/p>\n<p>Interfaces are nothing more than a named collection of abstract member definitions and\/or (introduced in C# 8) default implementations, which are implemented (optionally in the case of default implementations) by a given class or structure. In C#, interface types are defined using the interface keyword. By convention, all .NET interfaces begin with a capital letter I, as in the following example:<br \/>\n\u63a5\u53e3\u53ea\u4e0d\u8fc7\u662f\u62bd\u8c61\u6210\u5458\u5b9a\u4e49\u548c\/\u6216\uff08\u5728 C# 8 \u4e2d\u5f15\u5165\uff09\u9ed8\u8ba4\u5b9e\u73b0\u7684\u547d\u540d\u96c6\u5408\uff0c\u8fd9\u4e9b\u5b9e\u73b0\u7531\u7ed9\u5b9a\u7684\u7c7b\u6216\u7ed3\u6784\u5b9e\u73b0\uff08\u5728\u9ed8\u8ba4\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\u662f\u53ef\u9009\u7684\uff09\u3002\u5728 C# \u4e2d\uff0c\u63a5\u53e3\u7c7b\u578b\u662f\u4f7f\u7528\u63a5\u53e3\u5173\u952e\u5b57\u5b9a\u4e49\u7684\u3002\u6309\u7167\u7ea6\u5b9a\uff0c\u6240\u6709 .NET \u63a5\u53e3\u90fd\u4ee5\u5927\u5199\u5b57\u6bcd I \u5f00\u5934\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u6240\u793a\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/ A C# interface type is usually\n\/\/ declared as public, to allow types in other\n\/\/ assemblies to implement their behavior.\n\/\/ C# \u63a5\u53e3\u7c7b\u578b\u901a\u5e38\u662f\n\/\/ \u58f0\u660e\u4e3a\u516c\u5171\uff0c\u4ee5\u5141\u8bb8\u5176\u4ed6\u7c7b\u578b\n\/\/ \u7528\u4e8e\u5b9e\u73b0\u5176\u884c\u4e3a\u7684\u7a0b\u5e8f\u96c6\u3002\npublic interface IDraw\n{\n    void Draw();\n}\n```\n<\/pre>\n<\/p>\n<p>On their own, interfaces are of little use. However, when a class or structure implements a given interface in its unique way, you are able to request access to the supplied functionality using an interface reference in a polymorphic manner. Interface-based programming will be fully explored in Chapter 8.<br \/>\n\u5c31\u5176\u672c\u8eab\u800c\u8a00\uff0c\u63a5\u53e3\u51e0\u4e4e\u6ca1\u6709\u7528\u5904\u3002\u4f46\u662f\uff0c\u5f53\u7c7b\u6216\u7ed3\u6784\u4ee5\u5176\u552f\u4e00\u65b9\u5f0f\u5b9e\u73b0\u7ed9\u5b9a\u63a5\u53e3\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u63a5\u53e3\u5f15\u7528\u4ee5\u591a\u6001\u65b9\u5f0f\u8bf7\u6c42\u8bbf\u95ee\u6240\u63d0\u4f9b\u7684\u529f\u80fd\u3002\u7b2c8\u7ae0\u5c06\u5168\u9762\u63a2\u8ba8\u57fa\u4e8e\u63a5\u53e3\u7684\u7f16\u7a0b\u3002<\/p>\n<h2>CTS Structure Types<\/h2>\n<p>CTS \u7ed3\u6784\u7c7b\u578b<\/p>\n<p>The concept of a structure is also formalized under the CTS. If you have a C background, you should be pleased to know that these user-defined types (UDTs) have survived in the world of .NET (although they behave a bit differently under the hood). Simply put, a structure can be thought of as a lightweight class type having value-based semantics. For more details on the subtleties of structures, see Chapter 4. Typically, structures are best suited for modeling geometric and mathematical data and are created in C# using the struct keyword, as follows:<br \/>\n\u7ed3\u6784\u7684\u6982\u5ff5\u4e5f\u5728CTS\u4e0b\u6b63\u5f0f\u786e\u5b9a\u3002\u5982\u679c\u4f60\u6709 C \u80cc\u666f\uff0c\u4f60\u5e94\u8be5\u5f88\u9ad8\u5174\u77e5\u9053\u8fd9\u4e9b\u7528\u6237\u5b9a\u4e49\u7c7b\u578b \uff08UDT\uff09 \u5728 .NET \u4e16\u754c\u4e2d\u5e78\u5b58\u4e0b\u6765\uff08\u5c3d\u7ba1\u5b83\u4eec\u5728\u540e\u53f0\u7684\u884c\u4e3a\u7565\u6709\u4e0d\u540c\uff09\u3002\u7b80\u5355\u5730\u8bf4\uff0c\u53ef\u4ee5\u5c06\u7ed3\u6784\u89c6\u4e3a\u5177\u6709\u57fa\u4e8e\u503c\u7684\u8bed\u4e49\u7684\u8f7b\u91cf\u7ea7\u7c7b\u7c7b\u578b\u3002\u6709\u5173\u7ed3\u6784\u5fae\u5999\u4e4b\u5904\u7684\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u7b2c4\u7ae0\u3002\u901a\u5e38\uff0c\u7ed3\u6784\u6700\u9002\u5408\u5bf9\u51e0\u4f55\u548c\u6570\u5b66\u6570\u636e\u8fdb\u884c\u5efa\u6a21\uff0c\u5e76\u4f7f\u7528 struct \u5173\u952e\u5b57\u5728 C# \u4e2d\u521b\u5efa\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/ A C# structure type.\n\/\/ C# \u7ed3\u6784\u7c7b\u578b\u3002\nstruct Point\n{\n    \/\/ Structures can contain fields.\n    \/\/ \u7ed3\u6784\u53ef\u4ee5\u5305\u542b\u5b57\u6bb5\u3002\n    public int xPos, yPos;\n\n    \/\/ Structures can contain parameterized constructors.\n    \/\/ \u7ed3\u6784\u53ef\u4ee5\u5305\u542b\u53c2\u6570\u5316\u6784\u9020\u51fd\u6570\u3002\n    public Point(int x, int y)\n    { xPos = x; yPos = y; }\n\n    \/\/ Structures may define methods.\n    \/\/ \u7ed3\u6784\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\u3002\n    public void PrintPosition()\n    {\n        Console.WriteLine(&quot;({0}, {1})&quot;, xPos, yPos);\n    }\n}\n```\n<\/pre>\n<\/p>\n<h2>CTS Enumeration Types<\/h2>\n<p>CTS\u679a\u4e3e\u7c7b\u578b<\/p>\n<p>Enumerations are a handy programming construct that allow you to group name-value pairs. For example, assume you are creating a video game application that allows the player to select from three character categories (Wizard, Fighter, or Thief). Rather than keeping track of simple numerical values to represent each possibility, you could build a strongly typed enumeration using the enum keyword.<br \/>\n\u679a\u4e3e\u662f\u4e00\u79cd\u65b9\u4fbf\u7684\u7f16\u7a0b\u6784\u9020\uff0c\u53ef\u7528\u4e8e\u5bf9\u540d\u79f0-\u503c\u5bf9\u8fdb\u884c\u5206\u7ec4\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u6b63\u5728\u521b\u5efa\u4e00\u4e2a\u89c6\u9891\u6e38\u620f\u5e94\u7528\u7a0b\u5e8f\uff0c\u8be5\u5e94\u7528\u7a0b\u5e8f\u5141\u8bb8\u73a9\u5bb6\u4ece\u4e09\u4e2a\u89d2\u8272\u7c7b\u522b\uff08\u5deb\u5e08\u3001\u6218\u58eb\u6216\u5c0f\u5077\uff09\u4e2d\u8fdb\u884c\u9009\u62e9\u3002\u4e0e\u5176\u8ddf\u8e2a\u7b80\u5355\u7684\u6570\u503c\u6765\u8868\u793a\u6bcf\u79cd\u53ef\u80fd\u6027\uff0c\u4e0d\u5982\u4f7f\u7528 enum \u5173\u952e\u5b57\u751f\u6210\u5f3a\u7c7b\u578b\u679a\u4e3e\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/ A C# enumeration type.\n\/\/ C# \u679a\u4e3e\u7c7b\u578b\u3002\nenum CharacterTypeEnum\n{\n    Wizard = 100,\n    Fighter = 200,\n    Thief = 300\n}\n```\n<\/pre>\n<\/p>\n<p>By default, the storage used to hold each item is a 32-bit integer; however, it is possible to alter this storage slot if need be (e.g., when programming for a low-memory device such as a mobile device). Also, the CTS demands that enumerated types derive from a common base class, System.Enum. As you will see in Chapter 4, this base class defines a number of interesting members that allow you to extract, manipulate, and transform the underlying name-value pairs programmatically.<br \/>\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7528\u4e8e\u4fdd\u5b58\u6bcf\u4e2a\u9879\u76ee\u7684\u5b58\u50a8\u4e3a 32 \u4f4d\u6574\u6570;\u4f46\u662f\uff0c\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u66f4\u6539\u6b64\u5b58\u50a8\u63d2\u69fd\uff08\u4f8b\u5982\uff0c\u5728\u4e3a\u8bf8\u5982\u79fb\u52a8\u8bbe\u5907\u7684\u4f4e\u5185\u5b58\u8bbe\u5907\u7f16\u7a0b\u65f6\uff09\u3002\u6b64\u5916\uff0cCTS \u8981\u6c42\u679a\u4e3e\u7c7b\u578b\u6d3e\u751f\u81ea\u516c\u5171\u57fa\u7c7b System.Enum\u3002\u6b63\u5982\u60a8\u5c06\u5728\u7b2c 4 \u7ae0\u4e2d\u770b\u5230\u7684\uff0c\u6b64\u57fa\u7c7b\u5b9a\u4e49\u4e86\u8bb8\u591a\u6709\u8da3\u7684\u6210\u5458\uff0c\u8fd9\u4e9b\u6210\u5458\u5141\u8bb8\u60a8\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u63d0\u53d6\u3001\u64cd\u4f5c\u548c\u8f6c\u6362\u57fa\u7840\u540d\u79f0-\u503c\u5bf9\u3002<\/p>\n<h2>CTS Delegate Types<\/h2>\n<p>CTS \u59d4\u6258\u7c7b\u578b<\/p>\n<p>Delegates are the .NET equivalent of a type-safe, C-style function pointer. The key difference is that a .NET delegate is a class that derives from System.MulticastDelegate, rather than a simple pointer to a raw memory address. In C#, delegates are declared using the delegate keyword. \u59d4\u6258\u7b49\u6548\u4e8e\u7c7b\u578b\u5b89\u5168\u7684 C \u6837\u5f0f\u51fd\u6570\u6307\u9488\u3002\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff0c.NET \u59d4\u6258\u662f\u6d3e\u751f\u81ea System.MulticastDelegate \u7684\u7c7b\uff0c\u800c\u4e0d\u662f\u6307\u5411\u539f\u59cb\u5185\u5b58\u5730\u5740\u7684\u7b80\u5355\u6307\u9488\u3002\u5728 C# \u4e2d\uff0c\u59d4\u6258\u662f\u4f7f\u7528 delegate \u5173\u952e\u5b57\u58f0\u660e\u7684\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ This C# delegate type can &quot;point to&quot; any method\n\/\/ \u6b64 C# \u59d4\u6258\u7c7b\u578b\u53ef\u4ee5\u201c\u6307\u5411\u201d\u4efb\u4f55\u65b9\u6cd5\n\/\/ returning an int and taking two ints as input.\n\/\/ \u8fd4\u56de\u4e00\u4e2a int \u5e76\u5c06\u4e24\u4e2a\u6574\u6570\u4f5c\u4e3a\u8f93\u5165\u3002\ndelegate int BinaryOp(int x, int y);\n<\/pre>\n<\/p>\n<p>Delegates are critical when you want to provide a way for one object to forward a call to another object and provide the foundation for the .NET event architecture. As you will see in Chapters 12 and 14, delegates have intrinsic support for multicasting (i.e., forwarding a request to multiple recipients) and asynchronous method invocations (i.e., invoking the method on a secondary thread).<br \/>\n\u5982\u679c\u8981\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u63d0\u4f9b\u5c06\u8c03\u7528\u8f6c\u53d1\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u5e76\u4e3a .NET \u4e8b\u4ef6\u4f53\u7cfb\u7ed3\u6784\u63d0\u4f9b\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u5219\u59d4\u6258\u81f3\u5173\u91cd\u8981\u3002\u6b63\u5982\u60a8\u5c06\u5728\u7b2c 12 \u7ae0\u548c\u7b2c 14 \u7ae0\u4e2d\u770b\u5230\u7684\uff0c\u59d4\u6258\u5bf9\u591a\u64ad\uff08\u5373\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u591a\u4e2a\u63a5\u6536\u8005\uff09\u548c\u5f02\u6b65\u65b9\u6cd5\u8c03\u7528\uff08\u5373\u5728\u8f85\u52a9\u7ebf\u7a0b\u4e0a\u8c03\u7528\u8be5\u65b9\u6cd5\uff09\u5177\u6709\u5185\u5728\u652f\u6301\u3002<\/p>\n<h2>CTS Type Members<\/h2>\n<p>CTS \u7c7b\u578b\u6210\u5458<\/p>\n<p>Now that you have previewed each of the types formalized by the CTS, realize that most types take any number of members. Formally speaking, a type member is constrained by the set {constructor, finalizer, static constructor, nested type, operator, method, property, indexer, field, read-only field, constant, event}.<br \/>\n\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u9884\u89c8\u4e86 CTS \u5f62\u5f0f\u5316\u7684\u6bcf\u4e2a\u7c7b\u578b\uff0c\u8bf7\u6ce8\u610f\u5927\u591a\u6570\u7c7b\u578b\u91c7\u7528\u4efb\u610f\u6570\u91cf\u7684\u6210\u5458\u3002\u4ece\u5f62\u5f0f\u4e0a\u8bb2\uff0c\u7c7b\u578b\u6210\u5458\u53d7\u96c6\u5408 {\u6784\u9020\u51fd\u6570\u3001\u7ec8\u7ed3\u5668\u3001\u9759\u6001\u6784\u9020\u51fd\u6570\u3001\u5d4c\u5957\u7c7b\u578b\u3001\u8fd0\u7b97\u7b26\u3001\u65b9\u6cd5\u3001\u5c5e\u6027\u3001\u7d22\u5f15\u5668\u3001\u5b57\u6bb5\u3001\u53ea\u8bfb\u5b57\u6bb5\u3001\u5e38\u91cf\u3001\u4e8b\u4ef6} \u7684\u7ea6\u675f\u3002<\/p>\n<p>The CTS defines various adornments that may be associated with a given member. For example, each member has a given visibility trait (e.g., public, private, protected). Some members may be declared as abstract (to enforce a polymorphic behavior on derived types) as well as virtual (to define a canned, but overridable, implementation). Also, most members may be configured as static (bound at the class level) or instance (bound at the object level). The creation of type members is examined over the course of the next several chapters.<br \/>\nCTS \u5b9a\u4e49\u4e86\u53ef\u80fd\u4e0e\u7ed9\u5b9a\u6210\u5458\u5173\u8054\u7684\u5404\u79cd\u88c5\u9970\u3002\u4f8b\u5982\uff0c\u6bcf\u4e2a\u6210\u5458\u90fd\u6709\u4e00\u4e2a\u7ed9\u5b9a\u7684\u53ef\u89c1\u6027\u7279\u5f81\uff08\u4f8b\u5982\uff0c\u516c\u5171\u3001\u79c1\u6709\u3001\u53d7\u4fdd\u62a4\uff09\u3002\u67d0\u4e9b\u6210\u5458\u53ef\u80fd\u88ab\u58f0\u660e\u4e3a\u62bd\u8c61\uff08\u4ee5\u5bf9\u6d3e\u751f\u7c7b\u578b\u5f3a\u5236\u5b9e\u65bd\u591a\u6001\u884c\u4e3a\uff09\u4ee5\u53ca\u865a\u62df\uff08\u4ee5\u5b9a\u4e49\u56fa\u5b9a\u4f46\u53ef\u91cd\u5199\u7684\u5b9e\u73b0\uff09\u3002\u6b64\u5916\uff0c\u5927\u591a\u6570\u6210\u5458\u53ef\u4ee5\u914d\u7f6e\u4e3a\u9759\u6001\uff08\u5728\u7c7b\u7ea7\u522b\u7ed1\u5b9a\uff09\u6216\u5b9e\u4f8b\uff08\u5728\u5bf9\u8c61\u7ea7\u522b\u7ed1\u5b9a\uff09\u3002\u7c7b\u578b\u6210\u5458\u7684\u521b\u5efa\u5c06\u5728\u63a5\u4e0b\u6765\u7684\u51e0\u7ae0\u4e2d\u8fdb\u884c\u68c0\u67e5\u3002<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nas described in Chapter 10, the C# language also supports the creation of generic types and generic members.<br \/>\n\u6ce8\u610f \u5982\u7b2c 10 \u7ae0\u4e2d\u6240\u8ff0\uff0cC# \u8bed\u8a00\u8fd8\u652f\u6301\u521b\u5efa\u6cdb\u578b\u7c7b\u578b\u548c\u6cdb\u578b\u6210\u5458\u3002\n<\/p><\/blockquote>\n<h2>Intrinsic CTS Data Types<\/h2>\n<p>\u56fa\u6709 CTS \u6570\u636e\u7c7b\u578b<\/p>\n<p>The final aspect of the CTS to be aware of for the time being is that it establishes a well-defined set of fundamental data types. Although a given language typically has a unique keyword used to declare a fundamental data type, all .NET language keywords ultimately resolve to the same CTS type defined in an assembly named mscorlib.dll. Consider Table 1-2, which documents how key CTS data types are expressed in VB.NET and C#.<br \/>\nCTS \u76ee\u524d\u9700\u8981\u6ce8\u610f\u7684\u6700\u540e\u4e00\u4e2a\u65b9\u9762\u662f\u5b83\u5efa\u7acb\u4e86\u4e00\u7ec4\u5b9a\u4e49\u660e\u786e\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3002\u5c3d\u7ba1\u7ed9\u5b9a\u8bed\u8a00\u901a\u5e38\u5177\u6709\u7528\u4e8e\u58f0\u660e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u552f\u4e00\u5173\u952e\u5b57\uff0c\u4f46\u6240\u6709 .NET \u8bed\u8a00\u5173\u952e\u5b57\u6700\u7ec8\u89e3\u6790\u4e3a\u5728\u540d\u4e3a mscorlib.dll \u7684\u7a0b\u5e8f\u96c6\u4e2d\u5b9a\u4e49\u7684\u76f8\u540c CTS \u7c7b\u578b\u3002\u8bf7\u8003\u8651\u8868 1-2\uff0c\u5176\u4e2d\u8bb0\u5f55\u4e86\u5173\u952e CTS \u6570\u636e\u7c7b\u578b\u5982\u4f55\u5728 VB.NET \u548c C# \u4e2d\u8868\u793a\u3002<\/p>\n<p>Table 1-2. The Intrinsic CTS Data Types<br \/>\n\u8868 1-2. \u56fa\u6709 CTS \u6570\u636e\u7c7b\u578b<\/p>\n<table>\n<thead>\n<tr>\n<th>CTS Data Type<\/th>\n<th>VB Keyword<\/th>\n<th>C# Keyword<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>System.Byte<\/td>\n<td>Byte<\/td>\n<td>byte<\/td>\n<\/tr>\n<tr>\n<td>System.SByte<\/td>\n<td>SByte<\/td>\n<td>sbyte<\/td>\n<\/tr>\n<tr>\n<td>System.Int16<\/td>\n<td>Short<\/td>\n<td>short<\/td>\n<\/tr>\n<tr>\n<td>System.Int32<\/td>\n<td>Integer<\/td>\n<td>int<\/td>\n<\/tr>\n<tr>\n<td>System.Int64<\/td>\n<td>Long<\/td>\n<td>long<\/td>\n<\/tr>\n<tr>\n<td>System.UInt16<\/td>\n<td>UShort<\/td>\n<td>ushort<\/td>\n<\/tr>\n<tr>\n<td>System.UInt32<\/td>\n<td>UInteger<\/td>\n<td>uint<\/td>\n<\/tr>\n<tr>\n<td>System.UInt64<\/td>\n<td>ULong<\/td>\n<td>ulong<\/td>\n<\/tr>\n<tr>\n<td>System.Single<\/td>\n<td>Single<\/td>\n<td>float<\/td>\n<\/tr>\n<tr>\n<td>System.Double<\/td>\n<td>Double<\/td>\n<td>double<\/td>\n<\/tr>\n<tr>\n<td>System.Object<\/td>\n<td>Object<\/td>\n<td>object<\/td>\n<\/tr>\n<tr>\n<td>System.Char<\/td>\n<td>Char<\/td>\n<td>char<\/td>\n<\/tr>\n<tr>\n<td>System.String<\/td>\n<td>String<\/td>\n<td>string<\/td>\n<\/tr>\n<tr>\n<td>System.Decimal<\/td>\n<td>Decimal<\/td>\n<td>decimal<\/td>\n<\/tr>\n<tr>\n<td>System.Boolean<\/td>\n<td>Boolean<\/td>\n<td>bool<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Given that the unique keywords of a managed language are simply shorthand notations for a real type in the System namespace, you no longer have to worry about overflow\/underflow conditions for numerical data or how strings and Booleans are internally represented across different languages. Consider the following code snippets, which define 32-bit numerical variables in C# and Visual Basic, using language keywords as well as the formal CTS data type:<br \/>\n\u9274\u4e8e\u6258\u7ba1\u8bed\u8a00\u7684\u552f\u4e00\u5173\u952e\u5b57\u53ea\u662f System \u547d\u540d\u7a7a\u95f4\u4e2d\u5b9e\u9645\u7c7b\u578b\u7684\u7b80\u5199\u8868\u793a\u6cd5\uff0c\u60a8\u4e0d\u518d\u9700\u8981\u62c5\u5fc3\u6570\u503c\u6570\u636e\u7684\u6ea2\u51fa\/\u4e0b\u6ea2\u60c5\u51b5\uff0c\u4e5f\u4e0d\u5fc5\u62c5\u5fc3\u5b57\u7b26\u4e32\u548c\u5e03\u5c14\u503c\u5728\u4e0d\u540c\u8bed\u8a00\u4e2d\u7684\u5185\u90e8\u8868\u793a\u65b9\u5f0f\u3002\u8bf7\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\uff0c\u8fd9\u4e9b\u4ee3\u7801\u7247\u6bb5\u4f7f\u7528\u8bed\u8a00\u5173\u952e\u5b57\u548c\u6b63\u5f0f\u7684 CTS \u6570\u636e\u7c7b\u578b\u5728 C# \u548c Visual Basic \u4e2d\u5b9a\u4e49 32 \u4f4d\u6570\u503c\u53d8\u91cf\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/ Define some &quot;ints&quot; in C#.\n\/\/ \u5728 C# \u4e2d\u5b9a\u4e49\u4e00\u4e9b\u201c\u6574\u6570\u201d\u3002\nint i = 0; \nSystem.Int32 j = 0;\n\nREM: Define some &quot;ints&quot; in VB.\nREM: \u5728 VB \u4e2d\u5b9a\u4e49\u4e00\u4e9b\u201c\u6574\u6570\u201d\u3002\nDim i As Integer = 0\nDim j As System.Int32 = 0\n```\n<\/pre>\n<\/p>\n<h2>Understanding the Common Language Specification<\/h2>\n<p>\u4e86\u89e3\u516c\u5171\u8bed\u8a00\u89c4\u8303<\/p>\n<p>As you are aware, different languages express the same programming constructs in unique, language- specific terms. For example, in C# you denote string concatenation using the plus operator (+), while in VB you typically make use of the ampersand (&amp;). Even when two distinct languages express the same programmatic idiom (e.g., a function with no return value), the chances are good that the syntax will appear quite different on the surface.<br \/>\n\u5982\u60a8\u6240\u77e5\uff0c\u4e0d\u540c\u7684\u8bed\u8a00\u4ee5\u72ec\u7279\u7684\u3001\u7279\u5b9a\u4e8e\u8bed\u8a00\u7684\u672f\u8bed\u8868\u8fbe\u76f8\u540c\u7684\u7f16\u7a0b\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u5728 C# \u4e2d\uff0c\u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u7b26 \uff08+\uff09 \u8868\u793a\u5b57\u7b26\u4e32\u8fde\u63a5\uff0c\u800c\u5728 VB \u4e2d\uff0c\u901a\u5e38\u4f7f\u7528\u4e0e\u53f7 \uff08&amp;\uff09\u3002\u5373\u4f7f\u4e24\u79cd\u4e0d\u540c\u7684\u8bed\u8a00\u8868\u8fbe\u76f8\u540c\u7f16\u7a0b\u4e60\u8bed\uff08\u4f8b\u5982\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff09\uff0c\u8bed\u6cd5\u5f88\u6709\u53ef\u80fd\u5728\u8868\u9762\u4e0a\u770b\u8d77\u6765\u5b8c\u5168\u4e0d\u540c\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/ C# method returning nothing.\n\/\/ \u65b9\u6cd5\u4e0d\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\u3002\npublic void MyMethod()\n{\n    \/\/ Some interesting code...\n    \/\/ \u4e00\u4e9b\u6709\u8da3\u7684\u4ee3\u7801...\n}\n```\n<\/pre>\n<\/p>\n<p><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n```\nREM: VB method returning nothing.\nREM: VB \u65b9\u6cd5\u4e0d\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\u3002\nPublic Sub MyMethod()\n    REM: Some interesting code...\n    REM: \u4e00\u4e9b\u6709\u8da3\u7684\u4ee3\u7801...\nEnd Sub\n```\n<\/pre>\n<\/p>\n<p>As you have already seen, these minor syntactic variations are inconsequential in the eyes of the .NET runtime, given that the respective compilers (csc.exe or vbc.exe, in this case) emit a similar set of CIL instructions. However, languages can also differ with regard to their overall level of functionality. For<br \/>\nexample, a .NET language might or might not have a keyword to represent unsigned data and might or might not support pointer types. Given these possible variations, it would be ideal to have a baseline to which all.NET languages are expected to conform.<br \/>\n\u6b63\u5982\u4f60\u5df2\u7ecf\u770b\u5230\u7684\uff0c\u8fd9\u4e9b\u5fae\u5c0f\u7684\u53e5\u6cd5\u53d8\u5316\u5728.NET \u8fd0\u884c\u65f6\uff0c\u5047\u8bbe\u76f8\u5e94\u7684\u7f16\u8bd1\u5668\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a csc.exe \u6216 vbc.exe\uff09\u53d1\u51fa\u4e00\u7ec4\u7c7b\u4f3c\u7684 CIL \u6307\u4ee4\u3002\u4f46\u662f\uff0c\u8bed\u8a00\u5728\u6574\u4f53\u529f\u80fd\u7ea7\u522b\u65b9\u9762\u4e5f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff0c.NET \u8bed\u8a00\u53ef\u80fd\u6709\u4e5f\u53ef\u80fd\u6ca1\u6709\u7528\u4e8e\u8868\u793a\u65e0\u7b26\u53f7\u6570\u636e\u7684\u5173\u952e\u5b57\uff0c\u5e76\u4e14\u53ef\u80fd\u652f\u6301\u4e5f\u53ef\u80fd\u4e0d\u652f\u6301\u6307\u9488\u7c7b\u578b\u3002\u9274\u4e8e\u8fd9\u4e9b\u53ef\u80fd\u7684\u53d8\u5316\uff0c\u7406\u60f3\u7684\u505a\u6cd5\u662f\u6709\u4e00\u4e2a\u57fa\u7ebf\uff0c\u6240\u6709.NET \u8bed\u8a00\u5e94\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<p>The CLS is a set of rules that describe in vivid detail the minimal and complete set of features a given.NET compiler must support to produce code that can be hosted by the .NET Runtime, while at the same time be accessed in a uniform manner by all languages that target the .NET platform. In many ways, the CLS can be viewed as a subset of the full functionality defined by the CTS.<br \/>\nCLS \u662f\u4e00\u7ec4\u89c4\u5219\uff0c\u7528\u4e8e\u751f\u52a8\u8be6\u7ec6\u5730\u63cf\u8ff0\u7ed9\u5b9a\u7684\u6700\u5c0f\u548c\u5b8c\u6574\u529f\u80fd\u96c6.NET \u7f16\u8bd1\u5668\u5fc5\u987b\u652f\u6301\u751f\u6210\u53ef\u7531 .NET \u8fd0\u884c\u65f6\u627f\u8f7d\u7684\u4ee3\u7801\uff0c\u540c\u65f6\u7531\u9762\u5411 .NET \u5e73\u53f0\u7684\u6240\u6709\u8bed\u8a00\u4ee5\u7edf\u4e00\u65b9\u5f0f\u8bbf\u95ee\u3002\u5728\u8bb8\u591a\u65b9\u9762\uff0cCLS \u53ef\u4ee5\u88ab\u89c6\u4e3a CTS \u5b9a\u4e49\u7684\u5b8c\u6574\u529f\u80fd\u7684\u5b50\u96c6\u3002<\/p>\n<p>The CLS is ultimately a set of rules that compiler builders must conform to if they intend their products to function seamlessly within the .NET universe. Each rule is assigned a simple name (e.g., CLS Rule 6) and describes how this rule affects those who build the compilers as well as those who (in some way) interact with them. The cr\u00e8me de la cr\u00e8me of the CLS is Rule 1. CLS<br \/>\n\u6700\u7ec8\u662f\u4e00\u7ec4\u89c4\u5219\uff0c\u5982\u679c\u7f16\u8bd1\u5668\u6784\u5efa\u8005\u5e0c\u671b\u5176\u4ea7\u54c1\u5728 .NET \u4e16\u754c\u4e2d\u65e0\u7f1d\u8fd0\u884c\uff0c\u5219\u5fc5\u987b\u9075\u5b88\u8fd9\u4e9b\u89c4\u5219\u3002\u6bcf\u4e2a\u89c4\u5219\u90fd\u88ab\u5206\u914d\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u540d\u79f0\uff08\u4f8b\u5982\uff0cCLS \u89c4\u5219 6\uff09\uff0c\u5e76\u63cf\u8ff0\u4e86\u8be5\u89c4\u5219\u5982\u4f55\u5f71\u54cd\u6784\u5efa\u7f16\u8bd1\u5668\u7684\u4eba\u4ee5\u53ca\uff08\u4ee5\u67d0\u79cd\u65b9\u5f0f\uff09\u4e0e\u4ed6\u4eec\u4ea4\u4e92\u7684\u4eba\u3002CLS\u7684\u7cbe\u534e\u662f\u89c4\u52191\u3002<\/p>\n<p><em><em> Rule 1: CLS rules apply only to those parts of a type that are exposed outside the defining assembly.<br \/>\n<\/em><\/em>  \u89c4\u5219 1\uff1aCLS \u89c4\u5219\u4ec5\u9002\u7528\u4e8e\u5728\u5b9a\u4e49\u7a0b\u5e8f\u96c6\u5916\u90e8\u516c\u5f00\u7684\u7c7b\u578b\u96f6\u4ef6\u3002<\/p>\n<p>Given this rule, you can (correctly) infer that the remaining rules of the CLS do not apply to the logic used to build the inner workings of a .NET type. The only aspects of a type that must conform to the CLS are the member definitions themselves (i.e., naming conventions, parameters, and return types). The<\/p>\n<p>implementation logic for a member may use any number of non-CLS techniques, as the outside world won\u2019t know the difference.<br \/>\n\u7ed9\u5b9a\u6b64\u89c4\u5219\uff0c\u60a8\u53ef\u4ee5\uff08\u6b63\u786e\uff09\u63a8\u65ad CLS \u7684\u5176\u4f59\u89c4\u5219\u4e0d\u9002\u7528\u4e8e\u7528\u4e8e\u751f\u6210 .NET \u7c7b\u578b\u5185\u90e8\u5de5\u4f5c\u7684\u903b\u8f91\u3002\u7c7b\u578b\u4e2d\u552f\u4e00\u5fc5\u987b\u7b26\u5408 CLS \u7684\u65b9\u9762\u662f\u6210\u5458\u5b9a\u4e49\u672c\u8eab\uff08\u5373\u547d\u540d\u7ea6\u5b9a\u3001\u53c2\u6570\u548c\u8fd4\u56de\u7c7b\u578b\uff09\u3002\u8fd9\u6210\u5458\u7684\u5b9e\u73b0\u903b\u8f91\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u6570\u91cf\u7684\u975e CLS \u6280\u672f\uff0c\u56e0\u4e3a\u5916\u754c\u4e0d\u4f1a\u77e5\u9053\u5176\u4e2d\u7684\u533a\u522b\u3002<\/p>\n<p>To illustrate, the following C# Add() method is not CLS compliant, as the parameters and return values make use of unsigned data (which is not a requirement of the CLS):<br \/>\n\u4e3a\u4e86\u8bf4\u660e\u8fd9\u4e00\u70b9\uff0c\u4ee5\u4e0b C# Add\uff08\uff09 \u65b9\u6cd5\u4e0d\u7b26\u5408 CLS\uff0c\u56e0\u4e3a\u53c2\u6570\u548c\u8fd4\u56de\u503c\u4f7f\u7528\u672a\u7b7e\u540d\u7684\u6570\u636e\uff08\u8fd9\u4e0d\u662f CLS \u7684\u8981\u6c42\uff09\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nclass Calc\n{\n    \/\/ Exposed unsigned data is not CLS compliant!\n    \/\/ \u66b4\u9732\u7684\u672a\u7b7e\u540d\u6570\u636e\u4e0d\u7b26\u5408 CLS \u6807\u51c6\uff01\n    public ulong Add(ulong addend1, ulong addend2)\n    {\n        return addend1 + addend2;\n    }\n}\n<\/pre>\n<\/p>\n<p>However, consider the following code that makes use of unsigned data internally in a method:<br \/>\n\u4f46\u662f\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff0c\u8be5\u4ee3\u7801\u5728\u65b9\u6cd5\u5185\u90e8\u4f7f\u7528\u672a\u7b7e\u540d\u7684\u6570\u636e\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nclass Calc\n{\n    public int Add(int addend1, int addend2)\n    {\n        \/\/ As this ulong variable is only used internally,\n        \/\/ \u7531\u4e8e\u6b64 ulong \u53d8\u91cf\u4ec5\u5728\u5185\u90e8\u4f7f\u7528\uff0c\n        \/\/ we are still CLS compliant.\n        \/\/ \u6211\u4eec\u4ecd\u7136\u7b26\u5408CLS\u6807\u51c6\u3002\n        ulong temp = 0;\n        ......\n        return addend1 + addend2;\n    }\n}\n<\/pre>\n<\/p>\n<p>The class still conforms to the rules of the CLS and can rest assured that all .NET languages are able to invoke the Add() method.<br \/>\n\u8be5\u7c7b\u4ecd\u7136\u7b26\u5408 CLS \u7684\u89c4\u5219\uff0c\u5e76\u4e14\u53ef\u4ee5\u653e\u5fc3\uff0c\u6240\u6709 .NET \u8bed\u8a00\u90fd\u80fd\u591f\u8c03\u7528 Add\uff08\uff09 \u65b9\u6cd5\u3002<\/p>\n<p>Of course, in addition to Rule 1, the CLS defines numerous other rules. For example, the CLS describes how a given language must represent text strings, how enumerations should be represented internally (the base type used for storage), how to define static members, and so forth. Luckily, you don\u2019t have to commit these rules to memory to be a proficient .NET developer. Again, by and large, an intimate understanding of the CTS and CLS specifications is typically of interest only to tool\/compiler builders.<br \/>\n\u5f53\u7136\uff0c\u9664\u4e86\u89c4\u5219 1 \u4e4b\u5916\uff0cCLS \u8fd8\u5b9a\u4e49\u4e86\u8bb8\u591a\u5176\u4ed6\u89c4\u5219\u3002\u4f8b\u5982\uff0cCLS \u63cf\u8ff0\u7ed9\u5b9a\u8bed\u8a00\u5fc5\u987b\u5982\u4f55\u8868\u793a\u6587\u672c\u5b57\u7b26\u4e32\u3001\u679a\u4e3e\u5e94\u5728\u5185\u90e8\u8868\u793a\uff08\u7528\u4e8e\u5b58\u50a8\u7684\u57fa\u7c7b\u578b\uff09\u3001\u5982\u4f55\u5b9a\u4e49\u9759\u6001\u6210\u5458\u7b49\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u60a8\u4e0d\u5fc5\u5c06\u8fd9\u4e9b\u89c4\u5219\u63d0\u4ea4\u5230\u5185\u5b58\u4e2d\u5373\u53ef\u6210\u4e3a\u719f\u7ec3\u7684 .NET \u5f00\u53d1\u4eba\u5458\u3002\u540c\u6837\uff0c\u603b\u7684\u6765\u8bf4\uff0c\u5bf9 CTS \u548c CLS \u89c4\u8303\u7684\u6df1\u5165\u4e86\u89e3\u901a\u5e38\u53ea\u6709\u5de5\u5177\/\u7f16\u8bd1\u5668\u6784\u5efa\u8005\u611f\u5174\u8da3\u3002<\/p>\n<h2>Ensuring CLS Compliance<\/h2>\n<p>\u786e\u4fdd CLS \u5408\u89c4\u6027<\/p>\n<p>As you will see over the course of this book, C# does define a number of programming constructs that are not CLS compliant. The good news, however, is that you can instruct the C# compiler to check your code for CLS compliance using a single .NET attribute.<br \/>\n\u6b63\u5982\u60a8\u5c06\u5728\u672c\u4e66\u4e2d\u770b\u5230\u7684\u90a3\u6837\uff0cC# \u786e\u5b9e\u5b9a\u4e49\u4e86\u8bb8\u591a\u4e0d\u7b26\u5408 CLS \u7684\u7f16\u7a0b\u7ed3\u6784\u3002\u4e0d\u8fc7\uff0c\u597d\u6d88\u606f\u662f\uff0c\u60a8\u53ef\u4ee5\u6307\u793a C# \u7f16\u8bd1\u5668\u4f7f\u7528\u5355\u4e2a .NET \u5c5e\u6027\u68c0\u67e5\u4ee3\u7801\u662f\u5426\u7b26\u5408 CLS\u3002<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ Tell the C# compiler to check for CLS compliance. \n\/\/ \u544a\u8bc9 C# \u7f16\u8bd1\u5668\u68c0\u67e5 CLS \u5408\u89c4\u6027\u3002\n&#x5B;assembly: CLSCompliant(true)]\n<\/pre>\n<\/p>\n<p>Chapter 17 dives into the details of attribute-based programming. Until then, simply understand that the [CLSCompliant] attribute will instruct the C# compiler to check every line of code against the rulesof the CLS. If any CLS violations are discovered, you receive a compiler warning and a description of the offending code.<br \/>\n\u7b2c17\u7ae0\u6df1\u5165\u63a2\u8ba8\u4e86\u57fa\u4e8e\u5c5e\u6027\u7684\u7f16\u7a0b\u7684\u7ec6\u8282\u3002\u5728\u6b64\u4e4b\u524d\uff0c\u53ea\u9700\u4e86\u89e3[CLSCompliant] \u5c5e\u6027\u5c06\u6307\u793a C# \u7f16\u8bd1\u5668\u6839\u636e\u89c4\u5219\u68c0\u67e5\u6bcf\u4e00\u884c\u4ee3\u7801\u7684 CLS\u3002\u5982\u679c\u53d1\u73b0\u4efb\u4f55 CLS \u51b2\u7a81\uff0c\u60a8\u5c06\u6536\u5230\u7f16\u8bd1\u5668\u8b66\u544a\u548c\u8fdd\u89c4\u4ee3\u7801\u7684\u8bf4\u660e\u3002<\/p>\n<h2>Understanding the .NET Runtime<\/h2>\n<p>\u4e86\u89e3 .NET \u8fd0\u884c\u65f6<\/p>\n<p>In addition to the CTS and CLS specifications, the final piece of the puzzle to contend with is the .NET Runtime. Programmatically speaking, the term runtime can be understood as a collection of services that are required to execute a given compiled unit of code. For example, when Java developers deploy software to a new computer, they need to ensure the Java virtual machine (JVM) has been installed on the machine in order to run their software.<br \/>\n\u9664\u4e86 CTS \u548c CLS \u89c4\u8303\u4e4b\u5916\uff0c\u8981\u89e3\u51b3\u7684\u6700\u540e\u4e00\u5757\u96be\u9898\u662f .NET \u8fd0\u884c\u65f6\u3002\u4ece\u7f16\u7a0b\u65b9\u5f0f\u8bb2\uff0c\u672f\u8bed\u8fd0\u884c\u65f6\u53ef\u4ee5\u7406\u89e3\u4e3a\u6267\u884c\u7ed9\u5b9a\u7684\u5df2\u7f16\u8bd1\u4ee3\u7801\u5355\u5143\u6240\u9700\u7684\u670d\u52a1\u96c6\u5408\u3002\u4f8b\u5982\uff0c\u5f53 Java \u5f00\u53d1\u4eba\u5458\u5c06\u8f6f\u4ef6\u90e8\u7f72\u5230\u65b0\u8ba1\u7b97\u673a\u65f6\uff0c\u4ed6\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u5728\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5 Java \u865a\u62df\u673a \uff08JVM\uff09 \u624d\u80fd\u8fd0\u884c\u5176\u8f6f\u4ef6\u3002<\/p>\n<p>The .NET platform offers yet another runtime system. The key difference between the .NET runtime and the various other runtimes I just mentioned is that the .NET runtime provides a single, well-defined runtime layer that is shared by all languages and platforms that are .NET.<br \/>\n.NET \u5e73\u53f0\u63d0\u4f9b\u4e86\u53e6\u4e00\u4e2a\u8fd0\u884c\u65f6\u7cfb\u7edf\u3002.NET \u8fd0\u884c\u65f6\u4e0e\u6211\u521a\u624d\u63d0\u5230\u7684\u5404\u79cd\u5176\u4ed6\u8fd0\u884c\u65f6\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff0c.NET \u8fd0\u884c\u65f6\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7531 .NET \u7684\u6240\u6709\u8bed\u8a00\u548c\u5e73\u53f0\u5171\u4eab\u7684\u5355\u4e2a\u5b9a\u4e49\u660e\u786e\u7684\u8fd0\u884c\u65f6\u5c42\u3002<\/p>\n<h2>Distinguishing Between Assembly, Namespace, and Type<\/h2>\n<p>\u533a\u5206\u7a0b\u5e8f\u96c6\u3001\u547d\u540d\u7a7a\u95f4\u548c\u7c7b\u578b<\/p>\n<p>Each of us understands the importance of code libraries. The point of framework libraries is to give developers a well-defined set of existing code to leverage in their applications. However, the C# language does not come with a language-specific code library. Rather, C# developers leverage the language-neutral.NET libraries. To keep all the types within the base class libraries well organized, the .NET platform makes extensive use of the namespace concept.<br \/>\n\u6211\u4eec\u6bcf\u4e2a\u4eba\u90fd\u660e\u767d\u4ee3\u7801\u5e93\u7684\u91cd\u8981\u6027\u3002\u6846\u67b6\u5e93\u7684\u8981\u70b9\u662f\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u4e00\u7ec4\u5b9a\u4e49\u826f\u597d\u7684\u73b0\u6709\u4ee3\u7801\uff0c\u4ee5\u4fbf\u5728\u5176\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5229\u7528\u3002\u4f46\u662f\uff0cC# \u8bed\u8a00\u4e0d\u9644\u5e26\u7279\u5b9a\u4e8e\u8bed\u8a00\u7684\u4ee3\u7801\u5e93\u3002\u76f8\u53cd\uff0cC# \u5f00\u53d1\u4eba\u5458\u5229\u7528\u4e0e\u8bed\u8a00\u65e0\u5173.NET \u5e93\u3002\u4e3a\u4e86\u4f7f\u57fa\u7c7b\u5e93\u4e2d\u7684\u6240\u6709\u7c7b\u578b\u4e95\u4e95\u6709\u6761\uff0c.NET \u5e73\u53f0\u5e7f\u6cdb\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u6982\u5ff5\u3002<\/p>\n<p>A namespace is a grouping of semantically related types contained in an assembly or possibly spread across multiple related assemblies. For example, the System.IO namespace contains file I\/O-related types, the System.Data namespace defines basic database types, and so on. It is important to point out that a single assembly can contain any number of namespaces, each of which can contain any number of types.<br \/>\n\u547d\u540d\u7a7a\u95f4\u662f\u7a0b\u5e8f\u96c6\u4e2d\u5305\u542b\u7684\u8bed\u4e49\u76f8\u5173\u7c7b\u578b\u7684\u5206\u7ec4\uff0c\u6216\u8005\u53ef\u80fd\u5206\u5e03\u5728\u591a\u4e2a\u76f8\u5173\u7a0b\u5e8f\u96c6\u4e2d\u3002\u4f8b\u5982\uff0cSystem.IO \u547d\u540d\u7a7a\u95f4\u5305\u542b\u4e0e\u6587\u4ef6 I\/O \u76f8\u5173\u7684\u7c7b\u578b\uff0cSystem.Data \u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u57fa\u672c\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u7b49\u7b49\u3002\u5fc5\u987b\u6307\u51fa\u7684\u662f\uff0c\u5355\u4e2a\u7a0b\u5e8f\u96c6\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6570\u91cf\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u6bcf\u4e2a\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6570\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n<p>The key difference between this approach and a language-specific library is that any language targeting the .NET runtime uses the same namespaces and same types. For example, the following two programs all illustrate the ubiquitous Hello World application, written in C# and VB:<br \/>\n\u6b64\u65b9\u6cd5\u4e0e\u7279\u5b9a\u4e8e\u8bed\u8a00\u7684\u5e93\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff0c\u9762\u5411 .NET \u8fd0\u884c\u65f6\u7684\u4efb\u4f55\u8bed\u8a00\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4\u548c\u76f8\u540c\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4e24\u4e2a\u7a0b\u5e8f\u90fd\u8bf4\u660e\u4e86\u7528 C# \u548c VB \u7f16\u5199\u7684\u65e0\u5904\u4e0d\u5728\u7684 Hello World \u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nThe following code uses the C# 9 version of the Program class with a void Main() method to help illustrate the example. The new templates in C# 10 use top level statements (covered in Chapter 3) and global implicit using statements (covered later in this chapter).<br \/>\n\u4e0b\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u5e26\u6709 void Main\uff08\uff09 \u65b9\u6cd5\u7684 C# 9 \u7248\u672c\u7684 Program \u7c7b\u6765\u5e2e\u52a9\u8bf4\u660e\u8be5\u793a\u4f8b\u3002C# 10 \u4e2d\u7684\u65b0\u6a21\u677f\u4f7f\u7528\u9876\u7ea7\u8bed\u53e5\uff08\u5728\u7b2c 3 \u7ae0\u4e2d\u4ecb\u7ecd\uff09\u548c\u5168\u5c40\u9690\u5f0f using \u8bed\u53e5\uff08\u5728\u672c\u7ae0\u540e\u9762\u4ecb\u7ecd\uff09\u3002\n<\/p><\/blockquote>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n```\n\/\/ Hello World in C#.\nusing System;\npublic class MyApp\n{\n    static void Main()\n    {\n        Console.WriteLine(&quot;Hi from C#&quot;);\n    }\n}\n```\n<\/pre>\n<\/p>\n<p><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n```\nREM: Hello World in VB.\nImports System\nPublic Module MyApp\n    Sub Main()\n        Console.WriteLine(&quot;Hi from VB&quot;)\n    End Sub\nEnd Module\n```\n<\/pre>\n<\/p>\n<p>Notice that each language is using the Console class defined in the System namespace. Beyond some obvious syntactic variations, these applications look and feel very much alike, both physically and logically.<br \/>\n\u8bf7\u6ce8\u610f\uff0c\u6bcf\u79cd\u8bed\u8a00\u90fd\u4f7f\u7528 System \u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684 Console \u7c7b\u3002\u9664\u4e86\u4e00\u4e9b\u660e\u663e\u7684\u8bed\u6cd5\u53d8\u5316\u4e4b\u5916\uff0c\u8fd9\u4e9b\u5e94\u7528\u7a0b\u5e8f\u5728\u7269\u7406\u548c\u903b\u8f91\u4e0a\u7684\u5916\u89c2\u548c\u611f\u89c9\u90fd\u975e\u5e38\u76f8\u4f3c\u3002<\/p>\n<p>Clearly, once you are comfortable with your .NET programming language of choice, your next goal as a .NET developer is to get to know the wealth of types defined in the (numerous) .NET namespaces. The most fundamental namespace to get your head around initially is named System. This namespace provides a core body of types that you will need to leverage time and again as a .NET developer. In fact, you cannot build any sort of functional C# application without at least making a reference to the System namespace, as the core data types (e.g., System.Int32, System.String) are defined here. Table 1-3 offers a rundown of some (but certainly not all) of the .NET namespaces grouped by related functionality.<br \/>\n\u663e\u7136\uff0c\u4e00\u65e6\u4f60\u5bf9\u4f60\u9009\u62e9\u7684.NET\u7f16\u7a0b\u8bed\u8a00\u611f\u5230\u6ee1\u610f\uff0c\u4f60\u7684\u4e0b\u4e00\u4e2a\u76ee\u6807\u5c31\u662f.NET \u5f00\u53d1\u4eba\u5458\u5c06\u4e86\u89e3\uff08\u5927\u91cf\uff09.NET \u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u5927\u91cf\u7c7b\u578b\u3002\u6700\u521d\u8981\u4e86\u89e3\u7684\u6700\u57fa\u672c\u7684\u547d\u540d\u7a7a\u95f4\u540d\u4e3a System\u3002\u6b64\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e86\u4f5c\u4e3a .NET \u5f00\u53d1\u4eba\u5458\u9700\u8981\u4e00\u6b21\u53c8\u4e00\u6b21\u5229\u7528\u7684\u6838\u5fc3\u7c7b\u578b\u4e3b\u4f53\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u4e0d\u81f3\u5c11\u5f15\u7528 System \u547d\u540d\u7a7a\u95f4\uff0c\u5c31\u65e0\u6cd5\u751f\u6210\u4efb\u4f55\u7c7b\u578b\u7684\u529f\u80fd\u6027 C# \u5e94\u7528\u7a0b\u5e8f\uff0c\u56e0\u4e3a\u6b64\u5904\u5b9a\u4e49\u4e86\u6838\u5fc3\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\uff0cSystem.Int32\u3001System.String\uff09\u3002\u8868 1-3 \u63d0\u4f9b\u4e86\u6309\u76f8\u5173\u529f\u80fd\u5206\u7ec4\u7684\u4e00\u4e9b\uff08\u4f46\u80af\u5b9a\u4e0d\u662f\u5168\u90e8\uff09.NET \u547d\u540d\u7a7a\u95f4\u7684\u6982\u8981\u3002<\/p>\n<p>Table 1-3. A Sampling of .NET Namespaces<br \/>\n\u8868 1-3. .NET \u547d\u540d\u7a7a\u95f4\u7684\u793a\u4f8b<\/p>\n<table >\n<tr>\n<th>\n.NET Namespace<br \/>\n.NET \u547d\u540d\u7a7a\u95f4<\/th>\n<th>\nMeaning in Life<br \/>\n\u610f\u4e49<\/th>\n<\/tr>\n<tr>\n<td>\nSystem\n<\/td>\n<td>\nWithin System, you find numerous useful types dealing with intrinsic data, mathematical computations, random number generation, environment variables, and garbage collection, as well as a number of commonly used exceptions and attributes.<br \/>\n \u5728 System \u4e2d\uff0c\u60a8\u4f1a\u53d1\u73b0\u8bb8\u591a\u6709\u7528\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u7c7b\u578b\u5904\u7406\u5185\u90e8\u6570\u636e\u3001\u6570\u5b66\u8ba1\u7b97\u3001\u968f\u673a\u6570\u751f\u6210\u3001\u73af\u5883\u53d8\u91cf\u548c\u5783\u573e\u56de\u6536\uff0c\u4ee5\u53ca\u8bb8\u591a\u5e38\u7528\u7684\u5f02\u5e38\u548c\u5c5e\u6027\u3002<\/td>\n<\/tr>\n<tr>\n<td>\nSystem.Collections<br \/>\nSystem.Collections.Generic<\/td>\n<td>\nThese namespaces define a number of stock container types, as well as base types and interfaces that allow you to build customized collections.<br \/>\n\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u4e86\u8bb8\u591a\u5e38\u7528\u5bb9\u5668\u7c7b\u578b\uff0c\u4ee5\u53ca\u5141\u8bb8\u60a8\u751f\u6210\u81ea\u5b9a\u4e49\u96c6\u5408\u7684\u57fa\u7c7b\u578b\u548c\u63a5\u53e3\u3002\n<\/td>\n<\/tr>\n<tr>\n<td>\nSystem.Data System.Data.Common<br \/>\nSystem.Data.SqlClient\n<\/td>\n<td>\nThese namespaces are used for interacting with relational databases using ADO.NET.<br \/>\n\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u7528\u4e8e\u4f7f\u7528 ADO.NET \u4e0e\u5173\u7cfb\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\nSystem.IO System.IO.Compression<br \/>\nSystem.IO.Ports\n<\/td>\n<td>\nThese namespaces define numerous types used to work with file I\/O, compression of data, and port manipulation.<br \/>\n\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u4e86\u7528\u4e8e\u5904\u7406\u6587\u4ef6 I\/O\u3001\u6570\u636e\u538b\u7f29\u548c\u7aef\u53e3\u64cd\u4f5c\u7684\u591a\u79cd\u7c7b\u578b\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Runtime.InteropServices\n<\/td>\n<td>\nThis namespace provides facilities to allow .NET types to interact with unmanaged code (e.g., C-based DLLs and COM servers) and vice versa.<br \/>\n\u6b64\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u7684\u5de5\u5177\u5141\u8bb8 .NET \u7c7b\u578b\u4e0e\u975e\u6258\u7ba1\u4ee3\u7801\uff08\u4f8b\u5982\uff0c\u57fa\u4e8e C \u7684 DLL \u548c COM \u670d\u52a1\u5668\uff09\u4ea4\u4e92\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Drawing System.Windows.Forms\n<\/td>\n<td>\nThese namespaces define types used to build desktop applications using .NET\u2019s original UI toolkit (Windows Forms).<br \/>\n\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u7528\u4e8e\u4f7f\u7528 \u6784\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u7684\u7c7b\u578b\u3002NET\u7684\u539f\u59cbUI\u5de5\u5177\u5305\uff08Windows Forms\uff09\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Windows System.Windows.Controls<br \/>\nSystem.Windows.Shapes\n<\/td>\n<td>\nThe System.Windows namespace is the root for several namespaces that are used in Windows Presentation Foundation applications.<br \/>\nSystem.Windows \u547d\u540d\u7a7a\u95f4\u662f Windows Presentation Foundation \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u591a\u4e2a\u547d\u540d\u7a7a\u95f4\u7684\u6839\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Windows.Forms<br \/>\nSystem.Drawing\n<\/td>\n<td>\nThe System.Windows.Forms namespace is the root for several namespaces used in Windows Forms applications.<br \/>\n\u547d\u540d\u7a7a\u95f4\u662f Windows \u7a97\u4f53\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u591a\u4e2a\u547d\u540d\u7a7a\u95f4\u7684\u6839\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Linq System.Linq.Expressions\n<\/td>\n<td>\nThese namespaces define types used when programming against the LINQ API.<br \/>\n\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u9488\u5bf9 LINQ API \u8fdb\u884c\u7f16\u7a0b\u65f6\u4f7f\u7528\u7684\u7c7b\u578b\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.AspNetCore\n<\/td>\n<td>\nThis is one of many namespaces that allows you to build ASP.NET Core web applications and RESTful services.<br \/>\n\u8fd9\u662f\u5141\u8bb8\u60a8\u6784\u5efa ASP.NET \u6838\u5fc3 Web \u5e94\u7528\u7a0b\u5e8f\u548c RESTful \u670d\u52a1\u7684\u4f17\u591a\u547d\u540d\u7a7a\u95f4\u4e4b\u4e00\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Threading<br \/>\nSystem.Threading.Tasks\n<\/td>\n<td>\nThese namespaces define numerous types to build multithreaded applications that can distribute workloads across multiple CPUs.<br \/>\n\u8fd9\u4e9b\u547d\u540d\u7a7a\u95f4\u5b9a\u4e49\u4e86\u591a\u79cd\u7c7b\u578b\u6765\u6784\u5efa\u53ef\u5728\u591a\u4e2a CPU \u4e4b\u95f4\u5206\u914d\u5de5\u4f5c\u8d1f\u8f7d\u7684\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Security\n<\/td>\n<td>\n Security is an integrated aspect of the .NET universe. In the security-centric namespaces, you find numerous types dealing with permissions, cryptography, etc.<br \/>\n \u5b89\u5168\u6027\u662f .NET \u9886\u57df\u7684\u4e00\u4e2a\u96c6\u6210\u65b9\u9762\u3002\u5728\u4ee5\u5b89\u5168\u4e3a\u4e2d\u5fc3\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u60a8\u4f1a\u53d1\u73b0\u8bb8\u591a\u5904\u7406\u6743\u9650\u3001\u52a0\u5bc6\u7b49\u7684\u7c7b\u578b\u3002\n<\/td>\n<\/tr>\n<\/tr>\n<tr>\n<td>\nSystem.Xml\n<\/td>\n<td>\nThe XML-centric namespaces contain numerous types used to interact with XML data.<br \/>\n\u4ee5 XML \u4e3a\u4e2d\u5fc3\u7684\u547d\u540d\u7a7a\u95f4\u5305\u542b\u8bb8\u591a\u7528\u4e8e\u4e0e XML \u6570\u636e\u4ea4\u4e92\u7684\u7c7b\u578b\u3002\n<\/td>\n<\/tr>\n<\/table>\n<h2>Accessing a Namespace Programmatically<\/h2>\n<p>\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4<\/p>\n<p>It is worth reiterating that a namespace is nothing more than a convenient way for us mere humans to logically understand and organize related types. Consider again the System namespace. From your perspective, you can assume that System.Console represents a class named Console that is contained within a namespace called System. However, in the eyes of the .NET runtime, this is not so. The runtime engine sees only a single class named System.Console.<br \/>\n\u503c\u5f97\u91cd\u7533\u7684\u662f\uff0c\u547d\u540d\u7a7a\u95f4\u53ea\u4e0d\u8fc7\u662f\u6211\u4eec\u4eba\u7c7b\u5728\u903b\u8f91\u4e0a\u7406\u89e3\u548c\u7ec4\u7ec7\u76f8\u5173\u7c7b\u578b\u7684\u4e00\u79cd\u4fbf\u6377\u65b9\u5f0f\u3002\u518d\u6b21\u8003\u8651\u7cfb\u7edf\u547d\u540d\u7a7a\u95f4\u3002\u4ece\u60a8\u7684\u900f\u89c6\uff0c\u60a8\u53ef\u4ee5\u5047\u8bbe System.Console \u8868\u793a\u4e00\u4e2a\u540d\u4e3a Console \u7684\u7c7b\uff0c\u8be5\u7c7b\u5305\u542b\u5728\u540d\u4e3a System \u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u4f46\u662f\uff0c\u5728 .NET \u8fd0\u884c\u65f6\u770b\u6765\uff0c\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\u3002\u8fd0\u884c\u65f6\u5f15\u64ce\u53ea\u770b\u5230\u4e00\u4e2a\u540d\u4e3a System.Console \u7684\u7c7b\u3002<\/p>\n<p>In C#, the using keyword simplifies the process of referencing types defined in a particular namespace. Here is how it works. Returning to the Calc example program earlier in this chapter, there is a single using statement at the top of the file.<br \/>\n\u5728 C# \u4e2d\uff0cusing \u5173\u952e\u5b57\u7b80\u5316\u4e86\u5f15\u7528\u7279\u5b9a\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\u7684\u8fc7\u7a0b\u3002\u8fd9\u662f\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u3002\u56de\u5230\u672c\u7ae0\u524d\u9762\u7684 Calc \u793a\u4f8b\u7a0b\u5e8f\uff0c\u6587\u4ef6\u9876\u90e8\u6709\u4e00\u4e2a using \u8bed\u53e5\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\n<\/pre>\n<\/p>\n<p>That statement is a shortcut to enable this line of code:<br \/>\n\u8be5\u8bed\u53e5\u662f\u542f\u7528\u6b64\u4ee3\u7801\u884c\u7684\u5feb\u6377\u65b9\u5f0f\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nConsole.WriteLine(&quot;10 + 84 is {0}.&quot;, ans);\n<\/pre>\n<\/p>\n<p>Without the using statement, the code would need to be written like this:<br \/>\n\u5982\u679c\u6ca1\u6709 using \u8bed\u53e5\uff0c\u4ee3\u7801\u9700\u8981\u50cf\u8fd9\u6837\u7f16\u5199\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nSystem.Console.WriteLine(&quot;10 + 84 is {0}.&quot;, ans);\n<\/pre>\n<\/p>\n<p>While defining a type using the fully qualified name provides greater readability, I think you\u2019d agree that the C# using keyword reduces keystrokes. In this text, we will avoid the use of fully qualified names (unless there is a definite ambiguity to be resolved) and opt for the simplified approach of the C# using keyword.<br \/>\n\u867d\u7136\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u540d\u79f0\u5b9a\u4e49\u7c7b\u578b\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u53ef\u8bfb\u6027\uff0c\u4f46\u6211\u8ba4\u4e3a\u60a8\u4f1a\u540c\u610f C# using \u5173\u952e\u5b57\u53ef\u4ee5\u51cf\u5c11\u51fb\u952e\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u907f\u514d\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u7684\u540d\u79f0\uff08\u9664\u975e\u6709\u660e\u786e\u7684\u6b67\u4e49\u9700\u8981\u89e3\u51b3\uff09\uff0c\u5e76\u9009\u62e9 C# using \u5173\u952e\u5b57\u7684\u7b80\u5316\u65b9\u6cd5\u3002<\/p>\n<p>However, always remember that the using keyword is simply a shorthand notation for specifying a type\u2019s fully qualified name, and either approach results in the same underlying CIL (given that CIL code always uses fully qualified names) and has no effect on performance or the size of the assembly.<br \/>\n\u4f46\u662f\uff0c\u8bf7\u59cb\u7ec8\u8bb0\u4f4f\uff0cusing \u5173\u952e\u5b57\u53ea\u662f\u7528\u4e8e\u6307\u5b9a\u7c7b\u578b\u7684\u5b8c\u5168\u9650\u5b9a\u540d\u79f0\u7684\u7b80\u5199\u8868\u793a\u6cd5\uff0c\u5e76\u4e14\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u5bfc\u81f4\u76f8\u540c\u7684\u57fa\u7840 CIL\uff08\u5047\u8bbe CIL \u4ee3\u7801\u59cb\u7ec8\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u540d\u79f0\uff09\uff0c\u5e76\u4e14\u5bf9\u6027\u80fd\u6216\u7a0b\u5e8f\u96c6\u7684\u5927\u5c0f\u6ca1\u6709\u5f71\u54cd\u3002<\/p>\n<h2>Global Using Statements (New 10.0)<\/h2>\n<p>\u5168\u5c40Using\u8bed\u53e5\uff08\u65b0 10.0\uff09<\/p>\n<p>As you build more complex C# applications, you will most likely have namespaces repeated in multiple files. Introduced in C# 10, namespaces can be referenced globally, and then be available in every file in the project automatically. Simply add the global keyword in front of your using statements, like this:<br \/>\n\u5728\u751f\u6210\u66f4\u590d\u6742\u7684 C# \u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5f88\u53ef\u80fd\u4f1a\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u91cd\u590d\u547d\u540d\u7a7a\u95f4\u3002\u5728 C# 10 \u4e2d\u5f15\u5165\uff0c\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5168\u5c40\u5f15\u7528\uff0c\u7136\u540e\u81ea\u52a8\u5728\u9879\u76ee\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u53ef\u7528\u3002\u53ea\u9700\u5728 using \u8bed\u53e5\u524d\u9762\u6dfb\u52a0 global \u5173\u952e\u5b57\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nglobal using System;\n<\/pre>\n<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nall global using statements must come before any non-global using statements.<br \/>\n\u6240\u6709\u5168\u5c40 using \u8bed\u53e5\u90fd\u5fc5\u987b\u4f4d\u4e8e\u4efb\u4f55\u975e\u5168\u5c40 using \u8bed\u53e5\u4e4b\u524d\u3002\n<\/p><\/blockquote>\n<p>A recommendation is that you place the global using statements along with your top level statements (covered in Chapter 3) or a completely separate file (such as GlobalUsings.cs) for better visibility. You will see many examples of this throughout this text.<br \/>\n\u5efa\u8bae\u5c06\u5168\u5c40 using \u8bed\u53e5\u4e0e\u9876\u7ea7\u8bed\u53e5\uff08\u5728\u7b2c 3 \u7ae0\u4e2d\u4ecb\u7ecd\uff09\u6216\u5b8c\u5168\u72ec\u7acb\u7684\u6587\u4ef6\uff08\u5982 GlobalUsings.cs\uff09\u4e00\u8d77\u653e\u7f6e\uff0c\u4ee5\u63d0\u9ad8\u53ef\u89c1\u6027\u3002\u5728\u672c\u6587\u4e2d\uff0c\u60a8\u5c06\u770b\u5230\u8bb8\u591a\u793a\u4f8b\u3002<\/p>\n<p>In addition to placing the global using statements in Program.cs (or a separate file), they can be placed in the project file for the application using the following format:<br \/>\n\u9664\u4e86\u5c06\u5168\u5c40 using \u8bed\u53e5\u653e\u5728 Program.cs\uff08\u6216\u5355\u72ec\u7684\u6587\u4ef6\uff09\u4e2d\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u683c\u5f0f\u5c06\u5b83\u4eec\u653e\u7f6e\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u9879\u76ee\u6587\u4ef6\u4e2d\uff1a<\/p>\n<p><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;ItemGroup&gt;\n    &lt;Using Include=\u201dSystem.Text\u201d \/&gt;\n    &lt;Using Include=\u201dSystem.Text.Encodings.Web\u201d \/&gt;\n    &lt;Using Include=\u201dSystem.Text.Json\u201d \/&gt;\n    &lt;Using Include=\u201dSystem.Text.Json.Serialization\u201d \/&gt;\n&lt;\/ItemGroup&gt;\n<\/pre>\n<\/p>\n<h2>Implicit Global Using Statements (New 10.0)<\/h2>\n<p>\u9690\u5f0f\u5168\u5c40Using\u8bed\u53e5\uff08\u65b0 10.0\uff09<\/p>\n<p>Another new feature included with .NET 6\/C# 10 are implicit global using statements. The implicit global using statements supplied by .NET 6 varies based on the type of application you are building. Table 1-4 lists the types of applications and the included namespaces.<br \/>\n.NET 6\/C# 10 \u4e2d\u5305\u542b\u7684\u53e6\u4e00\u4e2a\u65b0\u529f\u80fd\u662f\u9690\u5f0f\u5168\u5c40 using \u8bed\u53e5\u3002.NET 6 \u63d0\u4f9b\u7684\u9690\u5f0f\u5168\u5c40 using \u8bed\u53e5\u56e0\u8981\u751f\u6210\u7684\u5e94\u7528\u7a0b\u5e8f\u7c7b\u578b\u800c\u5f02\u3002\u8868 1-4 \u5217\u51fa\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u7c7b\u578b\u548c\u5305\u542b\u7684\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<p>Table 1-4. A Sampling of .NET Namespaces<br \/>\n\u8868 1-4. .NET \u547d\u540d\u7a7a\u95f4\u7684\u793a\u4f8b<\/p>\n<table >\n<tr>\n<th>\n.NET Application Type<br \/>\n.NET \u5e94\u7528\u7a0b\u5e8f\u7c7b\u578b\n<\/th>\n<th>\nNamespaces covered by implicit global using statements<br \/>\n\u9690\u5f0f\u5168\u5c40 using \u8bed\u53e5\u6db5\u76d6\u7684\u547d\u540d\u7a7a\u95f4\n<\/th>\n<\/tr>\n<tr>\n<td>\nClient (Microsoft.NET.Sdk)\n<\/td>\n<td>\nSystem<br \/>\nSystem.Collections.Generic<br \/>\nSystem.IO<br \/>\nSystem.Linq<br \/>\nSystem.Net.Http<br \/>\nSystem.Threading<br \/>\nSystem.Threading.Tasks\n<\/td>\n<\/tr>\n<tr>\n<td>\nWeb (Microsoft.NET.Sdk.Web)\n<\/td>\n<td>\n All from Microsoft.NET.Sdk plus:<br \/>\nSystem.Net.Http.Json<br \/>\nMicrosoft.AspNetCore.Builder<br \/>\nMicrosoft.AspNetCore.Hosting<br \/>\nMicrosoft.AspNetCore.Http<br \/>\nMicrosoft.AspNetCore.Routing<br \/>\nMicrosoft.Extensions.Configuration<br \/>\nMicrosoft.Extensions.DependencyInjection<br \/>\nMicrosoft.Extensions.Hosting<br \/>\nMicrosoft.Extensions.Logging\n<\/td>\n<\/tr>\n<tr>\n<td>\nWorker Service (Microsoft.NET.Sdk.Worker)\n<\/td>\n<td>\nAll from Microsoft.NET.Sdk plus:<br \/>\nMicrosoft.Extensions.Configuration<br \/>\nMicrosoft.Extensions.DependencyInjection<br \/>\nMicrosoft.Extensions.Hosting<br \/>\nMicrosoft.Extensions.Logging\n<\/td>\n<\/tr>\n<\/table>\n<p>The vast majority of the C# 10 project templates enable global implicit using statements by default with the ImplicitUsings element in the project\u2019s main Property group. To disable the setting, update the project file to the following:<br \/>\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7edd\u5927\u591a\u6570 C# 10 \u9879\u76ee\u6a21\u677f\u4f7f\u7528\u9879\u76ee\u4e3b\u5c5e\u6027\u7ec4\u4e2d\u7684 ImplicitUsings \u5143\u7d20\u542f\u7528\u5168\u5c40\u9690\u5f0f using \u8bed\u53e5\u3002\u82e5\u8981\u7981\u7528\u8be5\u8bbe\u7f6e\uff0c\u8bf7\u5c06\u9879\u76ee\u6587\u4ef6\u66f4\u65b0\u4e3a\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<p><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;PropertyGroup&gt;\n    &lt;TargetFramework&gt;net6.0&lt;\/TargetFramework&gt;\n    &lt;Nullable&gt;enable&lt;\/Nullable&gt;\n    &lt;ImplicitUsings&gt;disable&lt;\/ImplicitUsings&gt;\n&lt;\/PropertyGroup&gt;\n<\/pre>\n<\/p>\n<p>To see the global using statements in your project, look for the <ProjectName>.GlobalUsings.g.cs file in the \\obj\\Debug\\net6.0 folder. For the Calc.cs project, the following is the generated code:<br \/>\n\u82e5\u8981\u67e5\u770b\u9879\u76ee\u4e2d\u7684\u5168\u5c40 using \u8bed\u53e5\uff0c\u8bf7\u67e5\u627e <ProjectName>\u3002GlobalUsings.g.cs \u6587\u4ef6\u5728 \\obj\\Debug\\net6.0 \u6587\u4ef6\u5939\u4e2d\u3002\u5bf9\u4e8e Calc.cs \u9879\u76ee\uff0c\u4ee5\u4e0b\u662f\u751f\u6210\u7684\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ &lt;auto-generated\/&gt;\nglobal using global::System;\nglobal using global::System.Collections.Generic;\nglobal using global::System.IO;\nglobal using global::System.Linq;\nglobal using global::System.Net.Http;\nglobal using global::System.Threading;\nglobal using global::System.Threading.Tasks;\n<\/pre>\n<\/p>\n<h2>File Scoped Namespaces (New 10.0)<\/h2>\n<p>\u6587\u4ef6\u8303\u56f4\u7684\u547d\u540d\u7a7a\u95f4\uff08\u65b0\u7248 10.0\uff09<\/p>\n<p>Also new in C# 10, file-scoped namespaces remove the need to wrap your code in braces when placing it in a custom namespace. Take the following example of the Calculator class, contained in the CalculatorExamples namespace. Prior to C# 10, to place a class in a namespace required the namespace declaration, an opening curly brace, the code (Calculator), and then a closing curly brace. In the example, the extra code is in bold:<br \/>\n\u6587\u4ef6\u8303\u56f4\u7684\u547d\u540d\u7a7a\u95f4\u4e5f\u662f C# 10 \u4e2d\u7684\u65b0\u589e\u529f\u80fd\uff0c\u5728\u5c06\u4ee3\u7801\u653e\u5165\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u65f6\uff0c\u65e0\u9700\u5c06\u4ee3\u7801\u5305\u88c5\u5728\u5927\u62ec\u53f7\u4e2d\u3002\u4ee5\u4ee5\u4e0b\u8ba1\u7b97\u5668\u7c7b\u4e3a\u4f8b\uff0c\u8be5\u7c7b\u5305\u542b\u5728\u8ba1\u7b97\u5668\u793a\u4f8b\u547d\u540d\u7a7a\u95f4\u3002\u5728 C# 10 \u4e4b\u524d\uff0c\u82e5\u8981\u5c06\u7c7b\u653e\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u9700\u8981\u547d\u540d\u7a7a\u95f4\u58f0\u660e\u3001\u5de6\u5927\u62ec\u53f7\u3001\u4ee3\u7801\uff08\u8ba1\u7b97\u5668\uff09\uff0c\u7136\u540e\u662f\u53f3\u5927\u62ec\u53f7\u3002\u5728\u793a\u4f8b\u4e2d\uff0c\u989d\u5916\u4ee3\u7801\u4ee5\u7c97\u4f53\u663e\u793a\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace CalculatorExamples\n{\n    class Calculator()\n    {\n\n    }\n}\n<\/pre>\n<\/p>\n<p>As your code becomes more complex, this can add a lot of extra code and indentation. With file scoped namespaces, the following code achieves the same effect:<br \/>\n\u968f\u7740\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\uff0c\u8fd9\u53ef\u80fd\u4f1a\u6dfb\u52a0\u5927\u91cf\u989d\u5916\u7684\u4ee3\u7801\u548c\u7f29\u8fdb\u3002\u5bf9\u4e8e\u6587\u4ef6\u8303\u56f4\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4ee5\u4e0b\u4ee3\u7801\u53ef\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\uff1a<\/p>\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace CalculatorExamples\nclass Calculator()\n{\n...\n}\n<\/pre>\n<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nCustom namespaces are covered in depth in Chapter 16<br \/>\n\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u5728\u7b2c 16 \u7ae0\u4e2d\u6709\u6df1\u5165\u4ecb\u7ecd\n<\/p><\/blockquote>\n<h2>Referencing External Assemblies<\/h2>\n<p>\u5f15\u7528\u5916\u90e8\u7a0b\u5e8f\u96c6<\/p>\n<p>Prior versions of the .NET Framework used a common installation location for framework libraries known as the Global Assembly Cache (GAC). Instead of having a single installation location, .NET does not use the GAC. Instead, each version (including minor releases) is installed in its own location (by version) on the computer. When using Windows, each version of the runtime and SDK gets installed into c:\\Program Files\\dotnet.<br \/>\n\u65e9\u671f\u7248\u672c\u7684 .NET Framework \u4f7f\u7528\u6846\u67b6\u5e93\u7684\u901a\u7528\u5b89\u88c5\u4f4d\u7f6e\uff0c\u79f0\u4e3a\u5168\u5c40\u7a0b\u5e8f\u96c6\u7f13\u5b58 \uff08GAC\uff09\u3002.NET \u4e0d\u4f7f\u7528 GAC\uff0c\u800c\u4e0d\u662f\u53ea\u6709\u4e00\u4e2a\u5b89\u88c5\u4f4d\u7f6e\u3002\u76f8\u53cd\uff0c\u6bcf\u4e2a\u7248\u672c\uff08\u5305\u62ec\u6b21\u8981\u7248\u672c\uff09\u90fd\u5b89\u88c5\u5728\u8ba1\u7b97\u673a\u4e0a\u81ea\u5df1\u7684\u4f4d\u7f6e\uff08\u6309\u7248\u672c\uff09\u3002\u4f7f\u7528 Windows \u65f6\uff0c\u6bcf\u4e2a\u7248\u672c\u7684\u8fd0\u884c\u65f6\u548c SDK \u90fd\u4f1a\u5b89\u88c5\u5230 c\uff1a\\Program Files\\dotnet \u4e2d\u3002<\/p>\n<p>Adding assemblies into most .NET projects is done by adding NuGet packages (covered later in this text). However, .NET applications targeting (and being developed on) Windows still have access to COM libraries.<br \/>\n\u5c06\u7a0b\u5e8f\u96c6\u6dfb\u52a0\u5230\u5927\u591a\u6570 .NET \u9879\u76ee\u4e2d\u662f\u901a\u8fc7\u6dfb\u52a0 NuGet \u5305\u6765\u5b8c\u6210\u7684\uff08\u672c\u6587\u7a0d\u540e\u5c06\u4ecb\u7ecd\uff09\u3002\u4f46\u662f\uff0c\u9762\u5411 Windows\uff08\u5e76\u5728 Windows \u4e0a\u5f00\u53d1\uff09\u7684 .NET \u5e94\u7528\u7a0b\u5e8f\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee COM \u5e93\u3002<\/p>\n<p>For an assembly to have access to another assembly that you are building (or someone built for you), you need to add a reference from your assembly to the other assembly and have physical access to the other assembly. Depending on the development tool you are using to build your .NET applications, you will have various ways to inform the compiler which assemblies you want to include during the compilation cycle.<br \/>\n\u8981\u4f7f\u7a0b\u5e8f\u96c6\u80fd\u591f\u8bbf\u95ee\u60a8\u6b63\u5728\u751f\u6210\u7684\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u96c6\uff08\u6216\u4e3a\u60a8\u6784\u5efa\u7684\u4eba\uff09\uff0c\u60a8\u9700\u8981\u5c06\u7a0b\u5e8f\u96c6\u4e2d\u7684\u5f15\u7528\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u96c6\uff0c\u5e76\u5177\u6709\u5bf9\u5176\u4ed6\u7a0b\u5e8f\u96c6\u7684\u7269\u7406\u8bbf\u95ee\u6743\u9650\u3002\u6839\u636e\u7528\u4e8e\u751f\u6210 .NET \u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u5de5\u5177\uff0c\u60a8\u5c06\u6709\u591a\u79cd\u65b9\u6cd5\u901a\u77e5\u7f16\u8bd1\u5668\u8981\u5728\u7f16\u8bd1\u5468\u671f\u4e2d\u5305\u542b\u54ea\u4e9b\u7a0b\u5e8f\u96c6\u3002<\/p>\n<h2>Exploring an Assembly Using ildasm.exe<\/h2>\n<p>\u4f7f\u7528 ildasm \u63a2\u7d22\u7a0b\u5e8f\u96c6.exe<\/p>\n<p>If you are beginning to feel a tad overwhelmed at the thought of gaining mastery over every namespace in the .NET platform, just remember that what makes a namespace unique is that it contains types that are somehow semantically related. Therefore, if you have no need for a user interface beyond a simple console application, you can forget all about the desktop and web namespaces (among others). If you are building a painting application, the database namespaces are most likely of little concern. You will learn over time the namespaces that are most relevant to your programming needs.<br \/>\n\u5982\u679c\u4f60\u5f00\u59cb\u5bf9\u638c\u63e1 .NET \u5e73\u53f0\u4e2d\u7684\u6bcf\u4e2a\u547d\u540d\u7a7a\u95f4\u7684\u60f3\u6cd5\u611f\u5230\u6709\u70b9\u4e0d\u77e5\u6240\u63aa\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u547d\u540d\u7a7a\u95f4\u7684\u72ec\u7279\u4e4b\u5904\u5728\u4e8e\u5b83\u5305\u542b\u67d0\u79cd\u8bed\u4e49\u76f8\u5173\u7684\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u4e0d\u9700\u8981\u7b80\u5355\u7684\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\u4e4b\u5916\u7684\u7528\u6237\u754c\u9762\uff0c\u5219\u53ef\u4ee5\u5fd8\u8bb0\u6709\u5173\u684c\u9762\u548cWeb\u547d\u540d\u7a7a\u95f4\uff08\u4ee5\u53ca\u5176\u4ed6\uff09\u7684\u6240\u6709\u5185\u5bb9\u3002\u5982\u679c\u60a8\u6b63\u5728\u6784\u5efa\u7ed8\u753b\u5e94\u7528\u7a0b\u5e8f\uff0c\u5219\u6570\u636e\u5e93\u547d\u540d\u7a7a\u95f4\u5f88\u53ef\u80fd\u65e0\u5173\u7d27\u8981\u3002\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u60a8\u5c06\u4e86\u89e3\u4e0e\u60a8\u7684\u7f16\u7a0b\u9700\u6c42\u6700\u76f8\u5173\u7684\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<p>The Intermediate Language Disassembler utility (ildasm.exe) allows you to create a text document representing a .NET assembly and investigate its contents, including the associated manifest, CIL code, and type metadata. This tool allows you to dive deeply into how the C# code maps to CIL and ultimately helps you understand the inner workings of the .NET platform. While you never need to use ildasm.exe to become a proficient .NET programmer, I highly recommend you fire up this tool from time to time to better understand how your C# code maps to runtime concepts.<br \/>\n\u4e2d\u95f4\u8bed\u8a00\u53cd\u6c47\u7f16\u7a0b\u5e8f\u5b9e\u7528\u5de5\u5177 \uff08ildasm.exe\uff09 \u5141\u8bb8\u60a8\u521b\u5efa\u8868\u793a .NET \u7a0b\u5e8f\u96c6\u7684\u6587\u672c\u6587\u6863\u5e76\u8c03\u67e5\u5176\u5185\u5bb9\uff0c\u5305\u62ec\u5173\u8054\u7684\u6e05\u5355\u3001CIL \u4ee3\u7801\u548c\u7c7b\u578b\u5143\u6570\u636e\u3002\u6b64\u5de5\u5177\u5141\u8bb8\u60a8\u6df1\u5165\u4e86\u89e3 C# \u4ee3\u7801\u5982\u4f55\u6620\u5c04\u5230 CIL\uff0c\u5e76\u6700\u7ec8\u5e2e\u52a9\u60a8\u4e86\u89e3 .NET \u5e73\u53f0\u7684\u5185\u90e8\u5de5\u4f5c\u539f\u7406\u3002\u867d\u7136\u4f60\u6c38\u8fdc\u4e0d\u9700\u8981\u4f7f\u7528 ildasm.exe \u6210\u4e3a\u4e00\u540d\u719f\u7ec3\u7684 .NET \u7a0b\u5e8f\u5458\uff0c\u4f46\u6211\u5f3a\u70c8\u5efa\u8bae\u4f60\u4e0d\u65f6\u542f\u52a8\u6b64\u5de5\u5177\uff0c\u4ee5\u66f4\u597d\u5730\u4e86\u89e3 C# \u4ee3\u7801\u5982\u4f55\u6620\u5c04\u5230\u8fd0\u884c\u65f6\u6982\u5ff5\u3002<\/p>\n<blockquote><p>\n\u25a0 Note<br \/>\nThe ildasm.exe program no longer ships with the .NET 6 runtime. There are two options for getting this tool into your workspace. The first is to compile from the .NET 6 runtime source located at https:\/\/ github.com\/dotnet\/runtime. The second, and easier method, is to pull down the desired version from www.nuget.org. ILdasm on Nuget is at <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.NETCore\">https:\/\/www.nuget.org\/packages\/Microsoft.NETCore<\/a>. ILDAsm\/. Make sure to select the correct version (for this book you will want version 6.0.0 or higher). add the ILdasm Nuget package to your project with the following command: dotnet add package Microsoft. NETCore.ILDAsm --version 6.0.0.<br \/>\nildasm.exe \u7a0b\u5e8f\u4e0d\u518d\u9644\u5e26 .NET 6 \u8fd0\u884c\u65f6\u3002\u6709\u4e24\u4e2a\u9009\u9879\u53ef\u7528\u4e8e\u5c06\u6b64\u5de5\u5177\u653e\u5165\u5de5\u4f5c\u533a\u3002\u7b2c\u4e00\u79cd\u662f\u4ece\u4f4d\u4e8e github.com\/dotnet\/runtime \u7684 .NET 6 \u8fd0\u884c\u65f6\u6e90\u7f16\u8bd1 <a href=\"https:\/\/\u3002\u7b2c\u4e8c\u79cd\u4e5f\u662f\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4ece\">https:\/\/\u3002\u7b2c\u4e8c\u79cd\u4e5f\u662f\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4ece<\/a> www.nuget.org \u4e0b\u62c9\u6240\u9700\u7684\u7248\u672c\u3002Nuget\u4e0a\u7684ILdasm\u6b63\u5728 <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.NETCore\">https:\/\/www.nuget.org\/packages\/Microsoft.NETCore<\/a>\u3002 \u4f0a\u5c14\u8fbe\/.\u786e\u4fdd\u9009\u62e9\u6b63\u786e\u7684\u7248\u672c\uff08\u5bf9\u4e8e\u672c\u4e66\uff0c\u60a8\u9700\u89816.0.0\u6216\u66f4\u9ad8\u7248\u672c\uff09\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06 ILdasm Nuget \u5305\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\uff1adotnet \u6dfb\u52a0\u5305Microsoft\u3002NETCore.ILDAsm --version 6.0.0.<br \/>\nThis doesn\u2019t actually load ILDasm.exe into your project but places it in your package folder (on Windows):%userprofile%.nuget\\packages\\microsoft.netcore.ildasm\\6.0.0\\runtimes\\native.<br \/>\n\u8fd9\u5b9e\u9645\u4e0a\u4e0d\u4f1a\u5c06 ILDasm.exe \u52a0\u8f7d\u5230\u60a8\u7684\u9879\u76ee\u4e2d\uff0c\u800c\u662f\u5c06\u5176\u653e\u5728\u60a8\u7684\u5305\u6587\u4ef6\u5939\u4e2d\uff08\u5728 Windows \u4e0a\uff09\uff1a%userprofile%.nuget\\packages\\microsoft.netcore.ildasm\\6.0.0\\runtimes\\native.<br \/>\nI have also included the 6.0.0 version of ILDasm.exe in this book\u2019s github repo in the root folder for the code samples.<br \/>\n\u6211\u8fd8\u5728\u672c\u4e66\u7684 github \u5b58\u50a8\u5e93\u4e2d\u5305\u542b\u4e86 ILDasm \u7684 6.0.0 \u7248\u672c.exe\u5728\u4ee3\u7801\u793a\u4f8b\u7684\u6839\u6587\u4ef6\u5939\u4e2d\u3002\n<\/p><\/blockquote>\n<p>After you get ildasm.exe loaded onto your machine, you can run the program from the command line without any arguments to see the help comments. At a minimum, you have to specify the assembly to extract the CIL.<br \/>\n\u5c06 ildasm.exe \u52a0\u8f7d\u5230\u8ba1\u7b97\u673a\u4e0a\u540e\uff0c\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u8fd0\u884c\u7a0b\u5e8f\uff0c\u800c\u65e0\u9700\u4efb\u4f55\u53c2\u6570\u6765\u67e5\u770b\u5e2e\u52a9\u6ce8\u91ca\u3002\u81f3\u5c11\u5fc5\u987b\u6307\u5b9a\u7a0b\u5e8f\u96c6\u4ee5\u63d0\u53d6 CIL\u3002<\/p>\n<p>An example command line is as follows:<br \/>\n\u547d\u4ee4\u884c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nildasm \/all \/METADATA \/out=csharp.il calc.cs.dll\n<\/pre>\n<\/p>\n<p>This will create a file named csharp.il exporting all available data into the file. This is the file where the previous IL examples came from.<br \/>\n\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a csharp.il \u7684\u6587\u4ef6\uff0c\u5c06\u6240\u6709\u53ef\u7528\u6570\u636e\u5bfc\u51fa\u5230\u6587\u4ef6\u4e2d\u3002\u8fd9\u662f\u524d\u9762\u7684 IL \u793a\u4f8b\u7684\u6765\u6e90\u6587\u4ef6\u3002<\/p>\n<h2>Summary<\/h2>\n<p>\u603b\u7ed3<\/p>\n<p>The point of this chapter was to lay out the conceptual framework necessary for the remainder of this book. I began by examining a number of limitations and complexities found within the technologies prior to .NET and followed up with an overview of how .NET and C# attempt to simplify the current state of affairs.<br \/>\n\u672c\u7ae0\u7684\u8981\u70b9\u662f\u9610\u8ff0\u672c\u4e66\u5176\u4f59\u90e8\u5206\u6240\u9700\u7684\u6982\u5ff5\u6846\u67b6\u3002\u6211\u9996\u5148\u68c0\u67e5\u4e86 .NET \u4e4b\u524d\u7684\u6280\u672f\u4e2d\u7684\u4e00\u4e9b\u9650\u5236\u548c\u590d\u6742\u6027\uff0c\u7136\u540e\u6982\u8ff0\u4e86 .NET \u548c C# \u5982\u4f55\u5c1d\u8bd5\u7b80\u5316\u5f53\u524d\u4e8b\u52a1\u72b6\u6001\u3002<\/p>\n<p>.NET basically boils down to a runtime execution engine (the .NET Runtime) and base class libraries.The runtime is able to host any .NET binary (aka assembly) that abides by the rules of managed code. As you saw, assemblies contain CIL instructions (in addition to type metadata and the assembly manifest) that are compiled to platform-specific instructions using a just-in-time compiler. In addition, you explored the role of the Common Language Specification and Common Type System.<br \/>\n.NET \u57fa\u672c\u4e0a\u5f52\u7ed3\u4e3a\u8fd0\u884c\u65f6\u6267\u884c\u5f15\u64ce\uff08.NET \u8fd0\u884c\u65f6\uff09\u548c\u57fa\u7c7b\u5e93\u3002\u8fd0\u884c\u65f6\u80fd\u591f\u627f\u8f7d\u9075\u5b88\u6258\u7ba1\u4ee3\u7801\u89c4\u5219\u7684\u4efb\u4f55 .NET \u4e8c\u8fdb\u5236\u6587\u4ef6\uff08\u4e5f\u79f0\u4e3a\u7a0b\u5e8f\u96c6\uff09\u3002\u5982\u60a8\u6240\u89c1\uff0c\u7a0b\u5e8f\u96c6\u5305\u542b CIL \u6307\u4ee4\uff08\u9664\u4e86\u7c7b\u578b\u5143\u6570\u636e\u548c\u7a0b\u5e8f\u96c6\u6e05\u5355\u4e4b\u5916\uff09\uff0c\u8fd9\u4e9b\u6307\u4ee4\u4f7f\u7528\u5b9e\u65f6\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e3a\u7279\u5b9a\u4e8e\u5e73\u53f0\u7684\u6307\u4ee4\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u63a2\u8ba8\u4e86\u516c\u5171\u8bed\u8a00\u89c4\u8303\u548c\u516c\u5171\u7c7b\u578b\u7cfb\u7edf\u7684\u4f5c\u7528\u3002<\/p>\n<p>In the next chapter, you will take a tour of the common integrated development environments you can use when you build your C# programming projects. You will be happy to know that in this book you will use completely free (and feature-rich) IDEs, so you can start exploring the .NET universe with no money down.<br \/>\n\u5728\u4e0b\u4e00\u7ae0\u4e2d\uff0c\u60a8\u5c06\u6d4f\u89c8\u5728\u751f\u6210 C# \u7f16\u7a0b\u9879\u76ee\u65f6\u53ef\u4ee5\u4f7f\u7528\u7684\u5e38\u89c1\u96c6\u6210\u5f00\u53d1\u73af\u5883\u3002\u4f60\u4f1a\u5f88\u9ad8\u5174\u5730\u77e5\u9053\uff0c\u5728\u672c\u4e66\u4e2d\uff0c\u4f60\u5c06\u4f7f\u7528\u5b8c\u5168\u514d\u8d39\uff08\u4e14\u529f\u80fd\u4e30\u5bcc\uff09\u7684IDE\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u63a2\u7d22.NET\u4e16\u754c\u4e86\uff0c\u4e0d\u7528\u82b1\u94b1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pro C# 10 with .NET 6 PART I Introducing C# and .NET 6 \u4ecb\u7ecd C# \u548c .NET 6 CHAPTER 1 \u7b2c\u4e00\u7ae0 Introducing C# and .NET 6 \u4ecb\u7ecd C# \u548c .NET 6 Microsoft\u2019s .NET platform and the C# programming language were formally introduced circa 2002 and have quickly become a mainstay of modern-day software development. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[22],"class_list":["post-270","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-pro-csharp10-with-net6"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=270"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/270\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}