资源描述
Chapter one
1. C# is a simple, modern, object-oriented, and type-safe programming language. C# is an object-oriented language, but C# further includes support for component-oriented programming. C# has a unified type system.
2. The key organizational concepts in C# are programs, namespaces, types, members, and assemblies. C# programs consist of one or more source files. Programs declare type, which contain members, and can be organized into namespaces. Classes and interfaces are examples of types. Files, methods, properties, events are examples of members.
3. Assemblies contain executable code in the form of intermediate language (IL) instructions, and symbolic information in the form of metadata. Before it is execute, 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.
4. An assembly is a self-describing unit of functionality both code and metadata, there is no need to for # include directives and header files in C#. The public types members contained in a particular assembly are made available in a C# program simply by referencing that assembly when compiling.
5. There are two kinds of types in C#: value type and reference types. Variables of value types directly contain their data whereas variables of reference types store reference to their data, the latter being known as objects. With reference types, it is possible for two variables to reference the same object and thus possible foe operations on one variable to affect the object referenced by the other variable. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other (except in the case of ref and out parameter variables).
6. C# programs use type declarations to create new types. A type declaration specifics the name and the members of the new type .Five of C#”s categories of types are user-definable; class types, struck types, interface types, enum types, and delegate types.
7. A class type defines a data structure that contains data members and functions members. Class types support inheritance and polymorphism, mechanisms whereby derived classes can extend and specialize base classes.
8. A struct type is similar to a class type in that it represents a structure with data members and functions members. However unlike classes, structs are value types and do not require heap allocation. Struct types do not support user-specified inheritance, and all struct types implicitly inherit from type object.
9. A interface type defines a contract as a named set of functions members. A class or struct that implements an interface must provide implementations of the interface’s function members. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.
10. An enum type is a distinct type with named constants. Every enmu type has an underlying type, which must be one of the eight integral types. The set of values of an enum type is the same as the set of values of the underlying type.
11. 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 parameters. Delegates are similar to the concept of functions pointers found in some other languages, but unlike functions pointers, delegates are object-oriented and type-safe.
12. C# supports single- and multi-dimensional arrays of any type. Unlike other types, array types do not have to be declared before they can be used. Instead, array types are constructed by following a type name with square brackets.
13. C #’s type system unified such that a value of any type can be treated as an object. Every type in C# directly or indirectly derives from the object class type, and object is the ultimate base class of all types. Value of reference types are treated as object simply by viewing the value of as type object. Values of value types are treated as objects by performing boxing and unboxing operations.
14. Using System;
Class test
{
Static void main () {
int i =123;
object o = I;
int j = (int) o;
}
}
When a value of a value type is converted to type object, an object instance, also called a “box,” is allocated to hold the value, and the value is copied into that box. Conversely, when a object reference type is cast to a value type, a check is made that the referenced object is a box of the correct value type, and, if the check succeeds, the value in the box is copied out.
15. There are several kinds of variables in C#, including fields, array elements, local variables, and parameters. Variables represents storage locations, and every variable has a type that and determines what values can be stored in the variable, as shown by the following table.
16. Expressions are constructed from operands and operators. Most operators can be overloaded. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of operands are of a user-defined class or struct type.
17. The actions of program are expressed using statements. C# supports several different kinds of statements, a number of which are defined in terms of embedded statements.
18. A block permits multiple statements to be written in contexts where a single statement is allowed. A block consists of a list of statements written between the delimiters {and}.
19. Declarations statements are used to declare local variable and constants.
20. Expressions statements are used to evaluate expressions. Expressions that can be used as statements include method invocations, object allocations using the new operator, assignment using = and the compound assignment operator, and increment and decrement operations using the ++and --operators.
21. Selection statements are used to select one of a number of possible statements for execution based on the value of some expression. In this group are the if and switch statements.
22. Iteration statements are used repeatedly execute an embedded statement. In this group are the while, do, for, and foreach statements.
23. Jump statements are used to transfer control. In this group are the break, continue, goto, throw, and return statements.
24. Classes are the most fundamental of C#’s types. A class is a data structure that combines state field and actions in a single unit. A class provides a definition for dynamically created instances of the class, also known as objects. Classes support inheritance and polymorphism, mechanisms whereby derived classes can extend and specialize base class.
25. New classes are created using class declarations. A class declaration starts with a header that specifies the attributes and modifiers of the class, the name of the class, the base class, and the interfaces implemented by the class. The header is followed by the class body, which consists of a list of member declarations written between the delimiter {and}.
26. Instances of classes are created using the new operator, which allocates memory for a new instance, invokes a constructor to initialize the instance, and return a reference to the instance.
27. The members of a class are either static members or instance members. Static members belong to class, and instance members belong to objects.
28. Each member of a class has an associated accessibility, which control the regions of program text that are able to access the member. There five possible forms of accessibility.
Accessibility meaning
public Access not limited
protected Access limited to this class and classes derived from this class
internal Access limited to this program
protected internal Access limited to this program and classes derived from this class
private Access limited to this class
29. A class inherits the members of its base class. Inheritance means that a class implicitly contains all members of its base class, except for the constructors of the base class. A derived class can add new members to those it inherits, but in cannot remove the definition of an inherited member.
30. An implicit conversion exists from a class type to any of its base class types. Therefore, a variable of a class type can reference an instance of that class or an instance of any derived class.
31. A field is a variable that is associated with a class or with an instance of a class.
A field declared with the static modifier defines a static field. A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only one copy ever of a static field.
A field declared without the static modifier defines an instance field. Every instance of class contains a separate copy of all the instances fields of that class.
32. Method is a member that implements a computation or action that can be performed by an object or class. Static members are accessed through the class. Instance methods are accessed through instance of the class.
Methods have a list of parameters, which represent value or variable reference passed to the method, and return type, which specifics the type of the value computed and return by the method.
The signature of a method must be unique in the class in which the method is declared. The signature of a method consists of the name of the method and the number, modifiers, and types of its parameters. The signature of a method does not include the return type.
33. Parameters are used to pass values or variable references to the methods. The parameters of a method get their actual values from the arguments that are specified when the method is invoked. There are four kinds of parameters: value parameters, reference parameters, output parameters, and parameter arrays.
A value parameter is used for input parameter passing. A value parameter corresponds to a local variable that gets its initial value from the argument that was passed for the parameter. Modifications to a value parameter do not affect the argument that was passed for the parameter.
A reference parameter is used for both input and output parameter passing. The argument passed for a reference parameter must be a value, and during execution of the method, the reference parameter represents the same storage location as the argument variable. A reference parameter is declared with the ref modifier.
using System;
class test
{
static void Swap(ref int x, ref int y){
int temp = x;
x = y;
y = temp;
}
Static void Main () {
int i =1; j =2;
swap (ref i, ref j);
Console.WriteLine (“{0} {1}”, i, j);
}
}
Outputs”2 1”
An output parameter is used for output parameter passing. An output parameter is similar to a reference parameter except that the initial value of the caller-provided argument is unimportant. An output parameter is declared with the out modifier.
using System;
class Test;
{
static void Divide (int x, int y, out int result, out int remainder){
result = x / y;
remainder = x % y;
}
static void Main ()
{
int res, rem;
Divide (10, 3, out res, out rem);
Console.writeLine(“{0} {1}”, res, rem);
}
}
Outputs “3 1”
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.
34. A method’s body specifies the statements to execute when the method is invoked. A method body can declare variables that are specific to the invocation of the method. Such variable are called local variables. A local variable declaration specifies a type name, a variable name, and possibly an initial value.
35. A method declared with a static modifier is a static method. A static method does not operate on a specific instance and can only access static members.
A method declared without a static modifier is an instance method. An instance method operates on a specific instance and can access both static and instance members. The instance on which an instance method was invoked can be explicitly accessed as this.
36.When an instance declaration with a virtual modifier, the modifier is said to be a virtual method. When no virtual modifier is present, the method is said to be a non-virtual method. When a virtual method is invoked, the runtime type of the instance for which that invocation takes place determines the actual method implementation to invoke. In a nonvirtual method invocation, the compile-time type of the instance is the determining factor.
37. A virtual method can be overridden in a derived class. When an instance method declaration includes an overridden modifier, the method overrides an inherited virtual method with the same signature.
38. An abstract method is a variable 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.
39. Method overloading permits multiple methods in the same class to have the same name as long as they have unique signatures.
40. An interface defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers. An interface does not provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface.
Interfaces may employ multiple inheritances.
41. 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 concept of function pointers found in some other languages, but unlike functions pointer, delegates are object-oriented and type-safe.
Chapter four
1. The types of the C# language are divided into two main categories: value types and reference types. A value type is either a struct type or enumeration type. C# provides a set of predefined struct types called the simple types.
2. All value types implicitly declare a public parameterless instance constructor called the default constructor.
For sbyte, byte, short, ushort, int, unit, long, and ulong, the default value is 0.
For char, the default value is ‘\x0000’.
展开阅读全文