目录:
1、??运算符使用
2、GetEnumerator方法
3、ResourceManager.GetString方法获得Resources的字符。
4、获得Settings文件的字符。
一、??可能是一个被遗忘的运算符,很少看到有人用它,它的用法很简单却很实用:
variable ?? defaultValue
相当于
variable == null ? defaultValue : variable
有了它,一行便能搞定Lazy Evaluation了:
使用??之前:
{
get
{
if (_users == null)
{
_users = Proxy.GetQueryObject<UserAccess>();
}
return _users;
}
}
之后:
{
get
{
return _users ?? (_users = Proxy.GetQueryObject<UserAccess>());
}
}
注:这个运算符只支持引用类型和Nullable类型。
int?就是Nullable<int>,Nullable类型也支持的。
原文:http://www.cnblogs.com/Dah/archive/2007/09/29/910479.html
二.GetEnumerator
下面的示例说明 GetEnumerator 方法的用法。包括在枚举数为活动的情况下从基础 DataTable 中删除行时枚举数的行为。
view plaincopy to clipboardprint?
public static void Main()
{
try
{
DataTable userTable = new DataTable("peopleTable");
userTable.Columns.Add("Id", typeof(int));
userTable.Columns.Add("Name", typeof(string));
// Note that even if you create the DataTableReader
// before adding the rows, the enumerator can still
// visit all the rows.
DataTableReader reader = userTable.CreateDataReader();
userTable.Rows.Add(new object[] { 1, "Peter" });
userTable.Rows.Add(new object[] { 2, "Mary" });
userTable.Rows.Add(new object[] { 3, "Andy" });
userTable.Rows.Add(new object[] { 4, "Russ" });
IEnumerator enumerator = reader.GetEnumerator();
// Keep track of whether the row to be deleted
// has actually been deleted yet. This allows
// this sample to demonstrate that the enumerator
// is able to survive row deletion.
bool isRowDeleted = false;
while (enumerator.MoveNext())
{
DbDataRecord dataRecord = (DbDataRecord)enumerator.Current;
// While the enumerator is active, delete a row.
// This doesn't affect the behavior of the enumerator.
if (!isRowDeleted)
{
isRowDeleted = true;
userTable.Rows[2].Delete();
}
Console.WriteLine(dataRecord.GetString(1));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
原文:http://www.cnblogs.com/suiqirui19872005/archive/2007/08/11/851752.html
2.2第二种用法
const
int
times =1000;
public
static
|