C# basics

Posted on 2013-12-05 00:11  chayu3  阅读(192)  评论(0编辑  收藏  举报

在C# language下写单元测试要用NUnit framework,N stands for .NET,包含测试方法的类要加上[TestFixture] annotation.

建立线程用Thread类,instantiate一个thread要用new Thread(new ThreadStart(Work.DoWork)); 运行一个线程要用.start()方法。

Nullable types have the following characteristics:

  • Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)

  • The syntax T? is shorthand for Nullable<T>, where T is a value type. The two forms are interchangeable.

  • Assign a value to a nullable type just as you would for an ordinary value type, for example int? x = 10; ordouble? d = 4.108. A nullable type can also be assigned the value null: int? x = null.

  • Use the Nullable<T>.GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault();

  • Use the HasValue and Value read-only properties to test for null and retrieve the value, as shown in the following example: if(x.HasValue) j = x.Value;

    • The HasValue property returns true if the variable contains a value, or false if it is null.

    • The Value property returns a value if one is assigned. Otherwise, a System.InvalidOperationException is thrown.

    • The default value for HasValue is false. The Value property has no default value.

    • You can also use the == and != operators with a nullable type, as shown in the following example: if (x != null) y = x;

 

readonly keyword:

readonly 关键字与 const 关键字不同。const 字段只能在该字段的声明中初始化。readonly 字段可以在声明或构造函数中初始化。因此,根据所使用的构造函数,readonly字段可能具有不同的值。另外,const 字段为编译时常数,而 readonly 字段可用于运行时常数.

与Java里的final比较:

==================Quote starts===================

The final keyword has several usages in Java. It corresponds to both the sealed and readonlykeywords in C#, depending on the context in which it is used.

Classes

To prevent subclassing (inheritance from the defined class):

Java

publicfinalclassMyFinalClass{...}

C#

publicsealedclassMyFinalClass{...}

Methods

Prevent overriding of a virtual method.

Java

publicclassMyClass{publicfinalvoid myFinalMethod(){...}}

C#

publicclassMyClass:MyBaseClass{publicsealedoverridevoidMyFinalMethod(){...}}

As Joachim Sauer points out, a notable difference between the two languages here is that Java by default marks all non-static methods as virtual, whereas C# marks them as sealed. Hence, you only need to use the sealed keyword in C# if you want to stop further overriding of a method that has been explicitly marked virtual in the base class.

Variables

To only allow a variable to be assigned once:

Java

publicfinaldouble pi =3.14;// essentially a constant

C#

publicreadonlydouble pi =3.14;// essentially a constant

As a side note, the effect of the readonly keyword differs from that of the const keyword in that the expression is evaluated at runtime rather than compile-time, hence allowing arbitrary expressions.

==================Quote ends===================