C# Programming Language 学习笔记(一)

C Programming Language 和C++ Programming Language 成为了不朽的经典著作,于是当企盼已久的天才Anders Hejlsberg写了C# Programming Language的时候,便迫不及待地想买一本瞧瞧,不过看了评论,这本书让很多人失望,说是MSDN的照抄,没有思想上的升华.于是也就没买.
事情搁置了很久,突然想系统地学习一下C#,MSDN的照抄也好,没有思想的升华也罢,系统学习C#的语法,C# Programming Language是不二之选,毕竟,还有谁会比Anders更了解C#那?

我看的是C# Programming Language的电子英文版,太对不起我的偶像Anders同学了,但是没办法,谁让你照抄MSDN的.
我通篇读一下这本书,遇到原来不知道的或者跟原来的想法不一致的就摘录下来,翻译一下,还好这本书的英文浅显易懂.
第一章 简介
1.Note that C# itself does not have a separate runtime library. Instead, the .NET Framework is the runtime library of C#.
C#本身没有单独的运行库,.Net Framework就是C#的运行库.
2.Assemblies contain executable code in the form of Intermediate Language (IL) instructions, and symbolic information in the form of metadata. Before it is executed, the IL code in an assembly is automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of .NET Common Language Runtime.
Assemblies(一般翻译为装配件,我觉得这些单词根本就不用翻译,怎么没人把C翻译汉语?)包含IL指令形式的可执行代码,并且以metadata的形式存放符号信息.在在执行前,assembly的IL代码会被.Net CLR的JIT自动转换成针对具体处理器的代码.
3.Because an assembly is a self-describing unit of functionality containing both code and metadata, there is no need for #include directives and header files in C#. The public types and members contained in a particular assembly are made available in a C# program simply by referencing that assembly when compiling the program.
assembly是包含代码和metadata的自描述的功能单元,所以在C#里没有必要用#include指示符和头文件.包含在一个特定assembly中的public的types和members只要在编译的时候引用(这个特定的assembly,这些types和members)就可以在C#程序中可见.
4.Forward declarations are never needed in C# because, with very few exceptions, declaration order is insignificant. C# does not limit a source file to declaring only one public type nor does it require the name of the source file to match a type declared in the source file.
在C#中永远不会需要事先声明,因为除极少数的异常外,申明的顺序是无关紧要的.C#不限制一个源文件只能声明一个public type,也不需要源文件的名字匹配在源文件中声明的type.
5.There are two kinds of types in C#: value types and reference types.
C#'s value types are further divided into simple types, enum types, and struct types, and C#'s reference types are further divided into class types, interface types, array types, and delegate types.
C#有两种types,value typesreference types
value type进一步分为simple types, enum types, and struct types,reference type进一步分为class types, interface types, array types, and delegate types.

6.Five of C#'s categories of types are user-definable: class types, struct types, interface types, enum types, and delegate types.
C#有五种自定义types, class types, struct types, interface types, enum types, and delegate types.
7.A delegate type represents references to methods with a particular parameter list and return type. Delegates make it possible to treat methods as entities that can be assigned to variables and passed as parameters. Delegates are similar to the concept of function pointers found in some other languages, but unlike function pointers, delegates are object-oriented and type-safe.
delegate type 表现为具有特定参数列表和返回类型的方法的引用.Delegates是把methods看成entities成为可能,这样方法可以赋值给变量和作为参数传递.Delegates跟一些其它语言里的函数指针类似,不过不同的是,Delegates是OO的和类型安全的.
8.int[] is a single-dimensional array of int, int[,] is a two-dimensional array of int, and int[][] is a single-dimensional array of single-dimensional arrays of int.
int[]是int类型的一维数组,int[,]为int类型的二维数组,int[][]为int类型的,(元素为)一维数组(类型)的一维数组.
这个东西真是饶口,也不知所言,估计后面第12章会介绍吧.
9.The checked and unchecked statements are used to control the overflow checking context for integral-type arithmetic operations and conversions.
checked和unchecked语句用来控制整型数据的算术运算和转化出现的overflow检查情况.

static void Main() {

  
int i = int.MaxValue;

  
checked {

    Console.WriteLine(i 
+ 1);// Exception

  }


  
unchecked {

    Console.WriteLine(i 
+ 1);// Overflow

  }


}

10.The lock statement is used to obtain the mutual-exclusion lock for a given object, execute a statement, and then release the lock.
lock statement用来给一个给定对象的获取排他锁,执行一个statement,然后释放锁.

class Account

{

  
decimal balance;

  
public void Withdraw(decimal amount) {

    
lock (this{

      
if (amount > balance) {

        
throw new Exception("Insufficient funds");

      }


      balance 
-= amount;

    }


  }


}

11.The using statement is used to obtain a resource, execute a statement, and then dispose of that resource.
using statement 用来获取一个资源,执行statement,然后释放资源.

static void Main() {

  
using (TextWriter w = File.CreateText("test.txt")) {

    w.WriteLine(
"Line one");

    w.WriteLine(
"Line two");

    w.WriteLine(
"Line three");

  }


}

12.Assignment to a readonly field can only occur as part of the field's declaration or in an instance constructor or static constructor in the same class.
给readonly field的赋值只能发生在如下情况:作为字段声明的一部分,实例的构造函数或者同一个class的静态构造函数.
13.A parameter array permits a variable number of arguments to be passed to a method. A parameter array is declared with the params modifier. Only the last parameter of a method can be a parameter array, and the type of a parameter array must be a single-dimensional array type.
参数数组允许给一个方法传递一组不定数量的变量.参数数组用params修饰符声明.只有方法的最后一个参数可以为参数数组,并且参数数组的类型必须为一维数组.
14.An abstract method is a virtual method with no implementation. An abstract method is declared with the abstract modifier and is permitted only in a class that is also declared abstract. An abstract method must be overridden in every nonabstract derived class.
一个抽象方法是没有实现的虚方法.抽象方法用abstract修饰符修饰并且只允许出现在同样被声明为abstract的类里.抽象方法必须在每个继承的非抽类中重写.
15.An indexer is a member that enables objects to be indexed in the same way as an array. An indexer is declared like a property except that the name of the member is this followed by a parameter list written between the delimiters [ and ]. The parameters are available in the accessor(s) of the indexer. Similar to properties, indexers can be read-write, read-only, and write-only, and the accessor(s) of an indexer can be virtual.
indexer是一个成员,它使象数组那样索引对象成为可能.indexer的声明跟property相似,除了成员的名字是this[参数],这些参数在indexer的访问器中可见.跟properties类似,indexers可以读写,只读,只写,并且indexer的访问器可以为虚函数.


public object this[int index] {

    
get {

        
return items[index];

    }


    
set {

        items[index] 
= value;

        OnListChange();

    }


}

16.An event is a member that enables a class or object to provide notifications. An event is declared like a field except that the declaration includes an event keyword and the type must be a delegate type.
Within a class that declares an event member, the event behaves just like a field of a delegate type (provided the event is not abstract and does not declare accessors). The field stores a reference to a delegate that represents the event handlers that have been added to the event. If no event handlers are present, the field is null.
事件是一个是类或者对象能够发出通知的成员.事件的声明象字段,只不过包含了event关键字并且类型必须是delegate.
在类里声明一个事件成员,事件的行为和委托类型的字段很相似(如果事件不是抽象的并且没有声明访问器的话).这个字段存储一个委托的引用,该委托表现为已经添加到事件的事件处理.如果没有处理事件,这个字段为null.

17.The notion of raising an event is precisely equivalent to invoking the delegate represented by the event—thus, there are no special language constructs for raising events.
Clients react to events through event handlers. Event handlers are attached using the += operator and removed using the -= operator.
触发事件的概念跟调用通过事件呈现的委托正好一样--所以,没有特别的触发事件的构造.
调用程序通过events handlers与事件关联.Event handlers用+=添加,用-=移除.

18.An operator is a member that defines the meaning of applying a particular expression operator to instances of a class. Three kinds of operators can be defined: unary operators, binary operators, and conversion operators. All operators must be declared as public and static.
operator 是一个成员,它重定义了类实例的一些应用于特定表单式运算符的意义(译注:真是别扭,意思是说运算符重载)三种类型的运算符可以定义:一元运算符,二元运算符和转化符.所有的操作符必须定义为public和static.
19.Destructors cannot have parameters, they cannot have accessibility modifiers, and they cannot be invoked explicitly. The destructor for an instance is invoked automatically during garbage collection.
The garbage collector is allowed wide latitude in deciding when to collect objects and run destructors. Specifically, the timing of destructor invocations is not deterministic, and destructors may be executed on any thread. For these and other reasons, classes should implement destructors only when no other solutions are feasible.
析构函数不能有参数,不能有访问级别修饰符,不能被显式调用.实例的析构函数在垃圾收集时自动调用.
垃圾回收器在何时回收对象和运行析构函数有很大的自由决定权.确切地说,析构函数运行的时候是不确定的,并且析构函数可以在任意线程中执行.鉴于这些和其它一些原因,实现析构函数应该在其它方法都不可行的情况下的选择.

原文地址:https://www.cnblogs.com/Farseer1215/p/257382.html