Inside c#(Foreword)_2

Simple Preservation

Hippocrates said, “Make a habit of two things: to help, or at least do no harm.” The “do no harm” part played a prominent role in our design of C#. If a C or C++ feature addressed a problem well, we kept it without modification. Most significantly, C# borrows from C and C++ in core areas such as expressions, statements, and overall syntax. Because so much of a typical program consists of these features, C and C++ developers instantly feel comfortable with C#.

简洁的保留

Hippocrates说过:“让习惯有两种状态:有帮助,或至少无损害”。我们在设计C#时尤其关注“无损害”这一部分。如果C/C++的摩伊功能能够很好的解决问题,我们将保留它并不作修改。更值得关注的是C#引入了C/C++的核心,例如表达式、语句以及总体的语法。因为典型的程序一般都有这些部分组成,所以C/C++程序员能够很快适应C#。

Incremental Improvement

Many incremental improvements have been made—too many to mention in this brief foreword—but it’s worth calling attention to a few changes that eliminate some common and time-consuming C and C++ errors:

  • Variables must be initialized before use, so bugs resulting from uninitialized variables are eliminated.
  • Statements like if and while require Boolean values, so a developer who accidentally uses the assignment operator (=) instead of the equality operator (= =) finds the mistake at compilation time.
  • Silent fall-through in switch statements is disallowed, so a developer who accidentally omits a break statement finds the mistake at compilation time.
  • 增加的改进

    增加的改进太多而无法在序言中一一列举,但是几个消除了C/C++中易犯的耗费时间的错误的改进值得关注:

  • 变量必须在使用前初始化,因为使用未初始化的变量的错误被消除
  • 像If和While这样的语句的条件部分要求一个Boolean类型的值,程序员不小心使用赋值运算符(=)取代了相等性运算符(==)的错误在编译时就会被发现。
  • Switch语句将不再允许“穿透”(译注:不使用break;显式的跳出switch结构而造成多个case分支的语句被执行的情况)。程序员遗漏break语句的情况会在编译时被发现。
  • Thoughtful Innovation

    Deeper innovation is found in C#’s type system, which includes the following advances:

  • The C# type system employs automatic memory management, thereby freeing developers from time-consuming and bug-prone manual memory management. Unlike most type systems, the C# type system also allows direct manipulation of pointer types and object addresses. (These manual memory management techniques are only permitted in certain security contexts.)
  • The C# type system is unified—everything is an object. Through innovative use of concepts such as boxing and unboxing, C# bridges the gap between value types and reference types, allowing any piece of data to be treated as an object.
  • Properties, methods, and events are fundamental. Many languages omit intrinsic support for properties and events, creating an unnecessary mismatch between the language and associated frameworks. For instance, if the framework supports properties and the language doesn’t, incrementing a property is awkward (for example, o.SetValue(o.GetValue( ) + 1)). If the language also supports properties, the operation is simple (o.Value++).
  • C# supports attributes, which enable the definition and use of declarative information about components. The ability to define new kinds of declarative information has always been a powerful tool for language designers. Now all C# developers have this capability.
  • Scott Wiltamuth

    Visual C# .NET Group Program Manager

    Microsoft Corporation

    有思想性的创新

    C#的类型系统包含了几个重大的创新,主要有以下几点:

  • C#类型系统使用自动的内存管理,将程序员从耗费时间的和易于产生错误的手工内存管理中解放出来。与大多数类型系统不同,C#的类型系统同时也允许手工的操作指针类型和对象地址(这一手工管理对象的技术仅只允许在认可的安全上下文中操作)。
  • C#的类型系统是统一的,所有的类型都是对象。通过革命性的使用装箱(boxing)和开箱(unboxing)的概念,C#跨越了值类型和引用类型的隔阂,允许任何的数据都可以像对象一样进行处理。
  • 特性、方法和事件是基础。许多语言省略了对特性和事件的内部支持,造成了语言和关联的框架不匹配。事实上,如果框架支持特性而语言部支持那么对特性递增的操作将会非常困难(例如:o.SetValue(o.GetValue()+1)),如果语言也支持特性的话操作将会非常简单(o.Value++)。
  • C#支持属性,可以直接定义或使用组件的声明性信息。定义新的声明性信息对于语言设计器来讲是一件功能强大的工具,现在所有的C#程序员都具备这种能力。
  • (后面的部分主要是进一步说明C#的优点和本书的优点,不翻了,自己看吧!)

    Inside C#

    I claim that forewords are perhaps the most difficult rhetoric to write. Consider for a moment that with a foreword you have a very limited amount of space in which to write a compelling “introduction” that conveys both the breadth and depth of the book and that also speaks to the care with which it was written. To do this successfully, I’m of the firm belief that a foreword needs a theme, a pervasive idea that can be summed up in a single sentence. That sentence for Inside C# is, “It’s about the details.”

    Scott has done an awesome job talking about why C# is a great language; now I want to spend a bit of time talking about why reading Inside C# is a great way to learn it. I’ve spent a lot of time programming in many languages over the years, and I have learned two indelible truths: it is impossible to write the “best” code, and it’s only possible to write “better” code when you know the details. Let me explain the first statement a bit. As soon as a software project grows over a 100 lines or so, the domain of possible correct implementations—a correct implementation being one that gets the job done—increases to such a large number that it’s essentially impossible to find the one that solves that domain space in the fastest, most efficient way possible. Even disregarding what people consider the best solution—“mine only has one line of code and is easily maintainable!” and “mine has 300 lines of code but executes 30 times faster!”—it should be obvious that our job as software engineers is to come up with a good solution, not necessarily the best.

    Which brings me to my second point: the good solution cannot be found without knowing what bits to twiddle. Don’t get me wrong; it’s also impossible to write good code without good algorithms. But regardless of what those algorithms are, if they’re implemented without knowledge of both the platform and language, I guarantee that taking that code one step closer to the perfect solution would be as easy as changing a class to a struct. And that leads us right back to exactly why Inside C# is well worth the read—because Tom delves into the details, and in the end the details lead to better code.

    Anson Horton

    Visual C# .NET Program Manager

    Microsoft Corporation

    posted on 2004-11-12 23:10  Dragoon  阅读(1006)  评论(0)    收藏  举报

    导航