来学习一下C#!

C# for C++ Developers

  • C++中struct和class是几乎一致的;而C#中这两种是不同的类型定义,struct不支持继承以及构造函数
  • C++对内存的抽象层度较少,数组其实是指向内存的指针;C#中的数组则是一个抽象类型
  • C++中整形可以当作布尔类型使用;而C#中则不可。我想以后这个限制也会出现在C++中的
  • C++中函数调用基本上都是传值(指针和引用也是一种值);而C#中class基本上都是都是传引用(struct类型除外,因为其传的是引用)
  • C#中的switch不想C++中的switch那样,可以fallthrough。这一点我感觉C++要向C#学习。
  • C++中函数指针类型比较弱;而C#中的类似功能的delegate则更类型安全
  • C#中可以在派生类中使用base关键字来调用基类方法,另外,覆盖基类方法需要显示使用override关键字
  • C++的派生类中定义了和基类同名的非虚函数,则会隐藏所有基类中的同名函数;而C#需要显示使用new关键字
  • C#不支持多继承;C#不使用头文件;C#中的宏只用来条件编译
  • C#中支持的操作符更多,比如is和typeof也是操作符
  • C++中可以使用typedef和using来给类型起别名;而C#中则简单直接地使用using
  • C#的unsafe模式中可以使用指针
  • C#中的string是内建类型
  • C#中的常量必须使用enum或者类成员变量的方式创建
  • C#中所引用第三方库的类型信息不来自头文件,而直接来自库的元信息(metadata)
  • C#中局部变量必须先初始化才能使用
  • C#自带垃圾收集功能;C#的析构函数支持多种语法
  • C#中也有默认构造函数的概念
  • C#中的extern关键字用于组装件(assembly)的不同版本
  • C++中的static关键字可以用声明编译单元内部数据;而C#中没有这个概念
  • C#中的异常处理必须用到finally
  • C#中操作符重载的方式和C++不同
  • C#中的foreach可以用来遍历数组和合集
  • C#不支持全局对象
  • C#不支持结构的位级字段
  • C#中的输入输出以及格式化需要.Net Framework的支持
  • C#不支持函数的默认参数
  • 虽然C#中的泛型有点类似C++模板,但是C#中的泛型信息是在执行期间保留的
  • C#中使用as关键字来进行类型指定,如果失败,会返回null,而不是抛出异常

A Tour of the C# Language摘抄

  • C# is an object-oriented language, but C# further includes support for component-oriented programming
  • C# has a unified type system. All C# types, including primitive types such as int and double, inherit from a single root object type.
  • Furthermore, C# supports both user-defined reference types and value types, allowing dynamic allocation of objects as well as in-line storage of lightweight structures.
  • Aspects of C#’s design that were directly influenced by versioning considerations include the separate virtual and override modifiers, the rules for method overload resolution, and support for explicit interface member declarations.

boxing/unboxing

C#’s type system is 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. Values of reference types are treated as objects simply by viewing the values as type object. Values of value types are treated as objects by performing boxing and unboxing operations.

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 an object reference 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.

Statements

Mostly the same as C++, with a few special statements:

  • foreach
  • yield for coroutine
  • try catch finally
  • checked/unchecked, integer overflow
  • lock
  • using
  • property, get/set
  • indexer, T this[int index]
  • event, +=, -=, add/remove, Changed?.Invoke(this, EventArgs.Empty);
  • finalizer, The using statement provides a better approach to object destruction
    public int Capacity 
    {
        get { return items.Length; }
        set 
        {
            if (value < count) value = count;
            if (value != items.Length) 
            {
                T[] newItems = new T[value];
                Array.Copy(items, 0, newItems, 0, count);
                items = newItems;
            }
        }
    }

CSC

  • csc, csharp compiler, The csc command compiles for the full framework, and may not be available on all platforms.
    • csc /t:library xx.cs
    • csc /r:acme.dll example.cs
  • The .NET Core ecosystem uses the dotnet CLI to manage command line builds. See this tutorial for a full description of those tools on the platforms supported by .NET Core.

其他