C#中for和foreach循环的性能
摘要:大家先来看看如下三个循环: int[] foo = new int[100]; 1, foreach (int i in foo) Console.WriteLine(i.ToString()); 2,for(int index=0;index<foo.Length;index++) Console.WriteLine(foo[index].ToString()); 3,int len=fo...
阅读全文
posted @
2009-06-20 15:19
周雪峰
阅读(3055)
推荐(2) 编辑
关于DefaultValueAttribute类的一些误解!
摘要:这个DefaultValueAttributte特性时常令大家比较疑惑,特别是看了MSDN文档中关于这个类的说明,大家可能就更加的疑惑了,文档中关于DefaultValueAttribute特性的说明如下:指定属性 (Property) 的默认值。 所以大家可能都以为通过应用这个特性就会设置属性的默认值,于是大家可能编写了类似的代码: public class Child { private in...
阅读全文
posted @
2009-06-19 17:00
周雪峰
阅读(319)
推荐(0) 编辑
const,readonly字段的取舍!
摘要:如果想在类中定义某些常量字段,一般有两种选择,可以定义成const,也可以定义成readonly,这两种方式如何取舍呢?在绝大多数情况下,readonly是要优于const的,因为readonly可以带来更大的灵活性!一般我们可以认为const是编译时常量,也就是说你引用const字段,在编译的时候就被替换成相应的常量值了!例如:public class MyClass{ public const...
阅读全文
posted @
2009-06-17 18:44
周雪峰
阅读(376)
推荐(0) 编辑
公有字段和属性的选择!
摘要:一般在C#中,想“暴露”类中的某些数据给用户可以使用两种方式来实现,一种是使用公有字段,另外一种是使用属性来实现,下面我列举一些代码来比较这两种实现:1,使用公有字段:public class Student{ public string Name;} 2,使用属性: public class Student { private string _name; public ...
阅读全文
posted @
2009-06-15 15:08
周雪峰
阅读(486)
推荐(0) 编辑