[1]子类调用父类构造函数

Code
using System;
using System.Collections.Generic;
public class Root
{
public Root(int i)
{
Console.WriteLine("root have not default construction ");
}
}
public class Stock : Root
{
/*
如果子类只提供这个构造函数,很容易理解,一定会出错的。
public Stock()
{


}
*/
public Stock(int i, int j) : base(i)
{
Console.WriteLine("Stock is subtype of Root");
}
}
public class Leaf : Stock
{
public Leaf(int i, int j, int k) : base(i, j) //调用它的父类,不调用它的祖父类
{
Console.WriteLine("Leaf is subtype of Stock");
}
public static void Main()
{
Leaf lf = new Leaf(1,2,3);
}
}
[2]十进制数转化为十六进制对应的字符串
uint i = 123654;
string i = i.ToString("X");
[3] 字符数组转化成字符串
string strSN = "TJEM3FJHIJKLMNOPQRSTUV";
char[] arrSN = strSN.ToCharArray();
string _strGUID = new string(arrSN);
[4]保留小数点后指定位数
using System;
using System.Collections.Generic;
public class Num
{
public static void Main()
{
double pi = Math.Round(3.1415926, 3);//保留3位小数,并四舍五入
Console.WriteLine(pi);
}
}
[5]虚函数的使用

Code
1 using System;
2 using System.Collections.Generic;
3 public class Level1
4 {
5 public virtual void print()
6 {
7 Console.WriteLine("level1");
8 }
9 }
10 public class Level2 : Level1
11 {
12 }
13 public class Level3 : Level2
14 {
15 }
16 public class Level4 : Level3
17 {
18 public override void print()
19 {
20 Console.WriteLine("Level4");
21 }
22 public static void Main()
23 {
24 Level1 level = new Level4();
25 level.print();
26 }
27 }
result:
Level4
[6]
DateTime

CodeDateTime
using System;
using System.Collections.Generic;
public class DataTest
{
public static void Main()
{
DateTime date1 = new DateTime(2008, 5, 1, 8, 30, 52);
Console.WriteLine(date1.ToString());
DateTime date2 = DateTime.Now; // get current time
Console.WriteLine(date2.ToString());
}
}
result:
2008-5-1 8:30:52
2009-5-22 14:32:13
[7]枚举到字符串

Code
1 using System;
2 using System.Collections.Generic;
3
4 public enum TimeOfDay
5 {
6 Morning = 0,
7 Afternoon = 1,
8 Evening = 2
9 }
10 public class TestEnum
11 {
12 public static void Main()
13 {
14 TimeOfDay time = TimeOfDay.Afternoon;
15 Console.WriteLine("time's value ={0}", (int)time);
16 Console.WriteLine("time's string = {0}",time.ToString());
17 }
18 }
19
20 [8]C#Dictionary
"Dictionary 泛型类提供了从一组键到一组值的映射。字典中的每个添加项都由一个值及其相关联的键组成。
通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary 类是作为一个哈希表来实现的。
检索速度取决于为 TKey 指定的类型的哈希算法的质量。
只要对象用作 Dictionary 中的键,它就不能以任何影响其哈希值的方式更改。使用字典的相等比较器比较时,
Dictionary 中的任何键都必须是唯一的。键不能为 空引用(在 Visual Basic 中为 Nothing),但是如果值类型
TValue 为引用类型,该值则可以为空。
Dictionary 需要一个相等实现来确定键是否相等。可以使用一个接受 comparer 参数的构造函数来指定 IEqualityComparer
泛型接口的实现;如果不指定实现,则使用默认的泛型相等比较器 EqualityComparer.Default。如果类型 TKey
实现 System.IEquatable 泛型接口,则默认相等比较器会使用该实现。"
me:需要重载以下两个函数
public override int GetHashCode();
public override bool Equals(object obj);
[9]vs 2005 单元测试
(1)测试->新建测试->单元测试->[TestMethod]
(2)双击*.vsmdi启动单元测试界面
[10]