【正文】
(SaveChanges: false)。 } That’s some pretty horrible codeit’s hard to see what the example does because the goal is lost in the details. The reason it is so unpleasant is that Office’s programming model is designed for dynamic languages that can fill in a lot of the details at runtime. C wasn’t able to do this, so developers were forced to do all the work by hand. Example 14 shows how to do exactly the same job in C . This is a lot easier to follow, because the code contains only the relevant details. It’s easy to see the sequence of operationsopen the document, get its properties, retrieve the Author property’s value, and close the document. C is now able to fill in all the details for us, thanks to its new dynamic language features. Example 14. Office interop with C static void Main(string[] args) { var wordApp = new ()。 string authorName = (Value, |,null, authorProp,new object[] { }).ToString()。 object docProperties = 。 object fileName = 。 } Despite appearances, C doesn’t know anything about SQL or databases. To enable this syntax, C added a raft of language features which, in bination, allow code of this sort to be used not just for database access, but also for XML parsing, or working with object models. Moreover, many of the individual features can be used in other contexts, as we’ll see in later chapters. C prefers small, posable, generalpurpose features over monolithic, specialized ones. A striking example of this philosophy is a feature that was demonstrated in prototype form in C, but which eventually got left out: XML literals. This experimental syntax allowed inline XML, which piled into code that built an object model representing that XML. The C team’s decision to omit this feature illustrates a stylistic preference for generality over highly specialized features—while the LINQ syntax has many applications, XML literal syntax cannot be used for anything other than XML, and this degree of specialization would feel out of place in C. Managed Code The .NET Framework provides more than just a class library. It also provides services in subtler ways that are not accessed explicitly through library calls. For example, earlier we mentioned that C can automate some aspects of memory management, a notorious source of bugs in C++ code. Abandoning heapallocated objects once you’re done with them is a coding error in C++, but it’s the normal way to free them in .NET. This service is provided by the CLRthe .NET Framework’s runtime environment. Although the C piler works closely with the runtime to make this possible, providing the necessary information about how your code uses objects and data, it’s ultimately the runtime that does the work of garbage collection. Depending on what sorts of languages you may have worked with before, the idea that the language depends heavily on the runtime might seem either pletely natural or somewhat disconcerting. It’s certainly different from how C and C++ workwith those languages, the piler’s output can be executed directly by the puter, and although those languages have some runtime services, it’s possible to write code that can run without them. But C code cannot even execute without the help of the runtime. Code that depends entirely on the runtime is called managed code. Managed pilers do not produce raw executable code. Instead, they produce an intermediate form of code called IL, the Intermediate Language. The runtime decides exactly how to convert it into something executable. One practical upshot of managed code is that a piled C program can run on both 32bit and 64bit systems without modification, and can even run on different processor architecturesit’s often possible for code that runs on an ARMbased handheld device to run unmodified on Intelbased PCs, or on the PowerPC architecture found in the Xbox 360 game console. As interesting as CPU independence may be, in practice the most useful aspect of managed code and IL is that the .NET runtime can provide useful services that are very hard for traditional pilation systems to implement well. In other words, the point is to make developers more productive. The memory management mentioned earlier is just one example. Others include a security model that takes the origin of code into account rather than merely the identity of the user who happens to be running the code。 } 附件 2:外文原文 Introducing C Cpronounced “See Sharp”is a programming language designed for Microsoft’s .NET platform. Since its first release in 2020, C has found many roles. It is widely used on the server side of websites, and also on both the client and server in lineofbusiness Windows desktop applications. You can write smartphone user interfaces and Xbox 360 games in C. More recently, Microsoft’s Silverlight platform has made C an option for writing Rich Inter Applications that run in a web browser. But what kind of language is C? To understand a language well enough to use it effectively, it’s not enough to focus purely on the details and mechanisms, although we’ll be spending plenty of time on those in this book. It is equally important to understand the thinking behind the details. So in this chapter, we’ll look at what problems C was built to solve. Then we’ll explore the style of the language, through aspects that distinguish it from other languages. And we’ll finish the chapter with a look at the latest step in the evolution of C, its fourth version. Why C? Why .NET? Programming languages exist to help developers be more productive. Many