第一组:刘聪 Const、readOnly、Static readOnly
Const 定义的是静态常在对象初始化的时候赋值.以后不能改变它的值.属于编译时常量。不能用new初始化。
Readonly 是只读变量.属于运行时变量.可以在类constructor里改变它的值.不能作用于局部变量。
const 和 static 不能在一起用,它已经是静态的了。
我们都知道,const和static readonly的确非常像:通过类名而不是对象名进行访问,在程式中只读等等。在多数情况下能混用。
二者本质的差别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值。
而static readonly,在程式中只读, 不过它是在运行时计算出其值的,所以还能通过静态构造函数来对它赋值,
readonly只能用来修饰类的field,不能修饰局部变量,也不能修饰property等其他类成员.
明白了这个本质差别,我们就不难看出下面的语句中static readonly和const能否互换了:
1. static readonly MyClass myins = new MyClass();
2. static readonly MyClass myins = null;
3. static readonly A = B * 20;
static readonly B = 10;
4. static readonly int [] constIntArray = new int[] {1, 2, 3};
5. void SomeFunction()
{
const int a = 10;
...
}
1:不能换成const。new操作符是需要执行构造函数的,所以无法在编译期间确定
2:能换成const。我们也看到,Reference类型的常量(除了String)只能是Null。
3:能换成const。我们能在编译期间非常明确的说,A等于200。
4:不能换成const。道理和1是相同的,虽然看起来1,2,3的数组的确就是个常量。
5:不能换成readonly,readonly只能用来修饰类的field,不能修饰局部变量,也不能修饰property等其他类成员。
因此,对于那些本质上应该是常量,不过却无法使用const来声明的地方,能使用static readonly。例如C#规范中给出的例子:
public class Color
{
public static readonly Color Black = new Color(0, 0, 0);
public static readonly Color White = new Color(255, 255, 255);
public static readonly Color Red = new Color(255, 0, 0);
public static readonly Color Green = new Color(0, 255, 0);
public static readonly Color Blue = new Color(0, 0, 255);
private byte red, green, blue;
public Color(byte r, byte g, byte b)
{
red = r;
green = g;
blue = b;
}
}
static readonly需要注意的一个问题是,对于一个static readonly的Reference类型,只是被限定不能进行赋值(写)操作而已。而对其成员的读写仍然是不受限制的。
public static readonly MyClass myins = new MyClass();
…
myins.SomeProperty = 10; //正常
myins = new MyClass(); //出错,该对象是只读的
不过,如果上例中的MyClass不是个class而是个struct,那么后面的两个语句就都会出错。
第二组:赵彩凤 带有Sheet名导出
public static MemoryStream CreateExcel(string[] titles, string[] fields, T[] datas, string format, string sheetName = null)
{
if (titles == null || titles.Length == 0)
throw new Common.DB.DBException("表头不能为空");
if (fields == null || fields.Length == 0)
throw new Common.DB.DBException("字段不能为空");
if (titles.Length != fields.Length)
throw new Common.DB.DBException("表头个数和字段个数长度不一致");
HSSFWorkbook book = new HSSFWorkbook();
ISheet sheet = book.CreateSheet(sheetName == null ? "Sheet" : sheetName);
IRow header = sheet.CreateRow(0);
for (int i = 0; i < titles.Length; i++)
{
header.CreateCell(i).SetCellValue(titles[i]);
}
Type type = typeof(T);
for (int i = 0; i < datas.Length; i++)
{
IRow row = sheet.CreateRow(i + 1);
for (int j = 0; j < fields.Length; j++)
{
string cellValue = "";
System.Reflection.PropertyInfo property = type.GetProperty(fields[j]);
if (property != null)
{
object o = property.GetValue(datas[i], null);
if (o != null)
{
if (property.PropertyType == typeof(DateTime))
{
if (Convert.ToDateTime(o) != DateTime.MinValue)
{
cellValue = Convert.ToDateTime(o).ToString(format);
}
}
else
{
cellValue = o.ToString();
}
}
}
row.CreateCell(j).SetCellValue(cellValue);
}
}
System.IO.MemoryStream bookms = new System.IO.MemoryStream();
book.Write(bookms);
return bookms;
}
第三组:吴景霞 调整下拉框空白
下拉框初始化无默认值,或者有空白选项,影响美观,可通过以下方法调整:
ng-switch 指令根据下拉菜单的选择结果显示或隐藏 HTML 区域。
- 给定初始化信息(ng-init)
- 隐藏空白选项(ng-show="false")
Dogs
Welcome to a world of dogs.
Tutorials
Learn from examples.
Cars
Read about cars.
第四组:傅云 2.14最简单的代码
马上就2.14了送大家最简单的代码
static void Main(string[] args)
{
int i = 1;
while(i<=10000)
{
Console.WriteLine("第" + i + "遍说:我爱你");
i++;
}
Console.WriteLine();
}
}