WHAT IS THE .NET FRAMEWORK?
The .NET Framework (now at version 4) is aapplication developing platform.
What’s in the .NET Framework?
Part of the .NET Framework library definessome basic types. This is called the Common Type System
(CTS).
As well as supplying this library, the .NetFramework also includes the .NET Common Language
Runtime (CLR), which is responsible formaintaining the execution of all applications developed usingthe .NET library.
Common Intermediate Language (CIL) code
just-in-time (JIT) compiler
the CIL code created is stored in anassembly
In addition to containing CIL, assembliesalso include meta information (that is, information about the informationcontained in the assembly, also known as metadata) and optional resources(additional data used by the CIL, such as sound files and pictures).
You might write some code that performstasks required by multiple applications. In situations like that, it is oftenuseful to place the reusable code in a place accessible to all applications. Inthe .NET Framework, this is the global assembly cache (GAC).
Code written using the .NET Framework ismanaged when it is executed (a stage usually referred to as runtime). Thismeans that the CLR looks after your applications by managing memory, handlingsecurity, allowing cross-language debugging, and so on.
garbage collection
linking. It is required because it is fareasier to work with several smaller files than one enormous one.
C#, as mentioned earlier, is one of thelanguages you can use to create applications that will run in the .NET CLR.
type-safe language (unlike C++), this meansthat once some data has been assigned to a type, it cannot subsequentlytransform itself into another unrelated type.
Console.WriteLine();
Console.ReadKey();
The References entry contains a list of the.NET libraries you are using in your project.
MessageBox.Show();
class Program
decimal type, decimal为16字节;double到decimal不可隐式转换,是因为后者精度更高,但是其数值表示范围反倒不如前者大
。
C#不支持unsigned关键字,取而代之的是在类型之前以u替代。比如,声明一个无符号的int类型,对于C++来说是这样:
unsigned int abc;
但对于C#来说,则为:
uint abc;
string在C#中是内置类型,但在C++里却必须要借助于STL。string在C#中存储的是UNICODE,但在C++却是ANSIC。如果要让C++支
持UNICODE的字符串,则必须使用wstring。
PascalCase and camelCase
Literal Values
Unicode escape sequence
implicit conversion
Convert.ToDouble()
global namespace
对于C++来说,命名空间的必须使用“::”来进行标注。
但C#却是采用“.”
如果以操作的观点来看,在C++中更像是类的类型,而C#则是对象的类型。
C#不能直接在命名空间中直接声明变量或函数。
C#中switch()括号中的变量类型可以是byte,short,int,long,bool,enum。另外还有string,这个在C++中是不行的.
The behavior where the flow of execution isforbidden from flowing from one case block to the next is one area in which C#differs from C++.
Two keywords exist for setting what iscalled the overflow checking context for an expression: checked and unchecked.
Such as Convert.ToInt32(), theseconversions is that they are always overflow-checked, and the checked andunchecked keywords and project property settings have no effect.
对于C++来说,枚举变量可以直接使用定义的类型,而C#则必须增加一个.标示,如:
//C++
Format format = RGB888;
//C#
Format format = Format.RGB888;
Enumerations have an underlying type usedfor storage, by default is int.
C++中的enum本质只是一组数值的集合,名称也仅仅是数值的标识,但对于C#而言,却又向前跨进了一步,enum还可以获得相应
的名称字符串,如:
string strFormat =Format.RGB888.ToString();
除了数值类型,string也能转换为enum类型,如:
Format format =(Format)Enum.Parse(typeof(Format), "RGB888");
不过需要注意,这里的字符串也是大小写敏感,如果该转换的字符串不在enum类型的名称之列,则会出错。
在C++中,类和结构体是一致的,唯一的区别在于,类默认的访问域是private,而类是public。但对于C#来说,结构体的成员默
认访问域为private,如果要指定public,则必须每个成员变量都指定。
C#里面能不能也像C++这样:
struct Size
{
public:
int x;
int y;
};
struct Size
{
public:
int x;
int y;
};
这种写法是在C#中是不成立的。
Arrays must be initialized before you haveaccess to them.
C++和C#都是用“[]”来表示数组,只不过不同的是,一个是在后,一个是在前,如:
//C++
int Age[] = {5};
//C#
int []Age = {5};
如果要指定数组的大小,C++可以在“[]”中直接填入数字,而C#必须重新new一次,如:
//C++
int Age[10];
//C#
//int [10]Age; 无法编译通过
int[] Age = new int [10];//可以编译通过
另外,这两者语言都可以指定数组的大小,但这里有个区别,C#中数组大小必须要和元素个数相匹配,如:
//C++
int Age[10] = {5,10};
//C#,无法编译通过
int[] Age = new int[10] { 5, 10 };
C++能通过,是因为它可以自动给没有赋值的元素自动给予0的数值,而C#则必须要一一指定
foreach loops
如果要对数组进行赋值,就不能使用foreach,而只能是for。
Multidimensional Arrays
Arrays of Arrays
C#的数组还有一个很有意思的特性,就是数组的数组,也就是很多教科书上所说的变长数组。多维数组和变长数组的区别在于,
前者以“,”分隔,而后者是以多个“[]”分隔,如:byte[][] Array = { newbyte[10],
new byte[20],
new byte[30]};
byte Value = Array[1][10];
但在C++很多教科书里,却很少提到数组的数组这个词语,是不是C++不支持呢?答案当然不是,因为数组的数组在C++里其
实也就是数组的概念,对于上面的C#代码,C++中可以变更表示如下: byte* Array[] = {new byte[10],
new byte [20],
new byte [30]};
byte Value = Array[1][10];
为什么C++里面没有特意的声明变长数组的语法,但却能实现C#的相应的功能呢?其实很简单,并不是C++不能,而是C#不行
。这个就要从指针说起,因为C#为了更安全,所以取消了指针这个遭人诟病的隐患,而C++的这个实现,其本质是存储指针的数
组,正因为如此,取消了指针的C#就不能使用声明多维数组的方式来实现变长数组,所以才会独立出一个声明变长数组的语法。
a string type variable can be treated as aread-only array of char variables. This means that you can access individualcharacters using syntax like the following:
string myString = "A string";
char myChar = myString[1];
ToCharArray()
对于C#来说入口函数就是Main,它有四种形式,如:
static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)
parameter array
Reference and Value Parameters
C#的引用和C++的写法是不同的,C++用&符号表示,而C#用的是ref关键字。而最大的区别还在于函数的调用,C++是直接输入变
量名,而C#则必须增加ref关键字。
在C++中如果你仅仅是查看函数的调用:Swap(a,b),你能判断得出a和b这两个形参是否会被改变么?因为无论是否引用,其调用
方式都是一样的,所以你必须要返回查看Swap的定义你才能确定。而这歧义在C#中根本就不存在,Swap(a,b)这语句绝对不会改
变a和b的值;如果存在改变的可能,那么必定使用ref标注。
C# doesn’t allow you to assume that a refparameter will be initialized in the function that uses it.
out parameter
there are important differences:
➤ Whereas it is illegal to use anunassigned variable as a ref parameter, you can use an unassigned variable asan out parameter.
➤ An out parameter must be treated as anunassigned value by the function that uses it.
Structs are can contain functions as wellas data.
The static keyword isn’t required forstruct functions.
在C#中,是不存在指针的,自然也不会有指向函数的指针,取而代之的是委托。
其实和C++的typedef非常相像,都是必须先定义一个类型,然后用该类型去声明一个变量。最大的不同在赋值阶段,C++只需要简单的将函数地址赋给指针,而C#必须用new声明一个对象,并且还要求相应的函数作为形参传入。
Debug.WriteLine()
Trace.WriteLine()
Debug.Write()
Trace.Write()
Debug.WriteLineIf()
Trace.WriteLineIf()
Debug.WriteIf()
Trace.WriteIf()
one key difference: The first command worksin debug builds only; the latter works for release builds as well.
These functions don’t work exactly likeConsole.WriteLine(). They work with only a single string parameter for themessage to output, rather than letting you insert variable values using{X}syntax.
tracepoints, Essentially, they enable youto output debugging information without modifying your code. The chiefdisadvantage of tracepoints is also their major advantage, which is that theyare stored in VS.
Debug.Assert()
Trace.Assert()
try . . . catch . . . finally
structured exception handling (SEH).
finally — Contains code that is alwaysexecuted
throw (new System.Exception());
When you use throw in a case block, nobreak; statement is necessary. throw is enough to end execution of the block.
C++,如果需要调用基类的非默认构造函数,那么我们必须要在派生类的构造函数中明确指出,也就是明确调用基类的非默认构
造函数,如://明确指出调用的是基类的非默认构造函数
CDerive(inti):CMyBase(i){};
C#的手法和C++其实也差不多,不过这里并不需要明确指出基类的构造函数,而是用base关键字替代,如:
//以base关键字来明确指出调用的是基类的非默认构造函数
public CMyDerive(int i): base(i){}
那么为什么C++为什么不能也增加一个类似于base的关键字呢?这个和继承的机制有关。因为C#只支持单继承,也就是只能允许
有一个基类,所以base关键字指向非常明确;而C++因为是能够多重继承的,也就是说有多个基类,单单的一个base关键字根本
无法明确指出是哪个基类。
除了base关键字以外,C#还支持一个this关键字。和base指向基类的不同,this指向的是当前类。
Static and Instance Class Members
Static Constructors
When using static members in a class, youmay want to initialize these members beforehand. You can use a static
constructor to perform initialization tasksof this type.
Interfaces
An interface is a collection of publicinstance (that is, nonstatic) methods and properties that are grouped togetherto encapsulate specific functionality.
Disposable Objects
IDisposable
Dispose()
Members of a base class may be virtual,which means that the member can be overridden by the class that inherits it.Virtual members cannot be private
abstract classes. An abstract class can’tbe instantiated directly; to use it you need to inherit from it.
sealed. A sealed class may not be used as abase class, so no derived classes are possible
抽象方法的特点:
抽象方法是隐式的虚方法,抽象方法只能在抽象类中声明。
抽象方法不能使用private、static和virtual修饰符。(抽象的方法默认是一个virtual方法)
抽象方法不能在派生类用base关键字进行访问。
抽象方法声明不提供实际的实现,没有方法体。若要实现抽象方法,需要在派生类(非抽象类)中进行重写该抽象方法,继承类
只有实现过所有抽象类的抽象方法后才能被实例化。
in C# all classes derive from the baseclass object at the root of their inheritance hierarchies.
Relationships Between Objects
Containment
Collections
The key difference between struct types andclasses is that struct types are value types.
By default, classes are declared as internal,meaning that only code in the current project will have access to them.
sealed classes may be public or internal.
The compiler does not allow a derived classto be more accessible than its base class. This means that an internal classcan inherit from a public base, but a public class can’t inherit from aninternal base.
interfaces are defined as internal bydefault. The keywords abstract and sealed are not allowed because neither modifiermakes sense in the context of interfaces (they contain no implementation, sothey can’t be
instantiated directly, and they must beinheritable to be useful).
Using a combination of GetType() and typeof(a C# operator that converts a class name into a System.Type object), you canperform comparisons
For a derived class to be instantiated, itsbase class must be instantiated. For this base class to be instantiated, itsown base class must be instantiated, and so on all the way back toSystem.Object. As a result,
whatever constructor you use to instantiatea class, System.Object.Object is always called first.
constructor initializer
base keyword
The only limitation here is that you canonly specify a single constructor using a constructor initializer.
class library
INTERFACES VERSUS ABSTRACT CLASSES
First the similarities: both abstractclasses and interfaces may contain members that can be inherited by a derivedclass. Neither interfaces nor abstract classes may be directly instantiated,but you can declare variables of these types.
Now the differences: derived classes mayonly inherit from a single base class, which means that only a single abstractclass can be inherited directly (although it is possible for a chain ofinheritance to include multiple abstract classes). Conversely, classes can useas many interfaces as they want
interfaces can’t contain fields,constructors, destructors, static members, or constants.
Abstract classes are intended for use asthe base class for families of objects that share certain centralcharacteristics, such as a common purpose and structure. Interfaces areintended for use by classes that might differ on a far more fundamental level,but can still do some of the same things.