C# 方法使用匯總(GetEnumerator用法,??用法等)

來源:互聯網
上載者:User

目錄:

1、??運算子使用

2、GetEnumerator方法

3、ResourceManager.GetString方法獲得Resources的字元。

4、獲得Settings檔案的字元。

一、??可能是一個被遺忘的運算子,很少看到有人用它,它的用法很簡單卻很實用:
variable ?? defaultValue
相當於
variable == null ? defaultValue : variable
有了它,一行便能搞定Lazy Evaluation了:
使用??之前:

public UserAccess Users
{
get
{
if (_users == null)
{
_users = Proxy.GetQueryObject<UserAccess>();
}
return _users;
}
}

之後:

public UserAccess 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 void Test2()        {            Stopwatch watch = new Stopwatch();          Hashtable hastable = new Hashtable();                      for (int i = 0; i < 10000; i++)            {                hastable.Add(i, i.ToString() + "值");            }            //測試GetEnumerator            watch.Start();            for (int i = 0; i < times; i++)            {                IDictionaryEnumerator enumerator = hastable.GetEnumerator();                while (enumerator.MoveNext())                {                    string key = enumerator.Key.ToString();                    string value = enumerator.Value.ToString();                }            }            watch.Stop();            Console.WriteLine("Hashtable GetEnumerator耗時" + watch.ElapsedMilliseconds);            Console.WriteLine("---------------");            watch.Reset();            //測試ForEach            watch.Start();            for (int i = 0; i < times; i++)            {                foreach (object item in hastable.Keys)                {                    string key = item.ToString();                    string value = hastable[item].ToString();                }            }            watch.Stop();            Console.WriteLine("Hashtable ForEach耗時" + watch.ElapsedMilliseconds);            Console.WriteLine("---------------");            watch.Reset();            Dictionary<int, string> dictionary = new Dictionary<int, string>();            for (int i = 0; i < 10000; i++)            {                dictionary.Add(i, i.ToString() + "值");            }            watch.Start();            for (int i = 0; i < times; i++)            {                                Dictionary<int,string>.Enumerator enumerator = dictionary.GetEnumerator();                while (enumerator.MoveNext())                {                    int key = enumerator.Current.Key;                    string value = enumerator.Current.Value;                }            }            watch.Stop();            Console.WriteLine("Dictionary GetEnumerator耗時" + watch.ElapsedMilliseconds);            Console.WriteLine("---------------");            watch.Reset();            //測試ForEach            watch.Start();            for (int i = 0; i < times; i++)            {                foreach (int item in dictionary.Keys)                {                    int key = item;                    string value = dictionary[item];                }            }            watch.Stop();            Console.WriteLine("Dictionary ForEach耗時" + watch.ElapsedMilliseconds);            Console.WriteLine("---------------");            Console.ReadKey();        }    }

  原文:http://www.cnblogs.com/scottckt/archive/2011/05/16/2048243.html

 三、獲得Resources的字元。通過ResourceManager.GetString方法獲得定義在Properties.Resources的String字元。 WpfApplicationSomeMethodTest.Properties.Resources.ResourceManager.GetString("TestString");

四、獲得Settings檔案的字元。WpfApplicationSomeMethodTest.Properties.Settings.Default.DefaultFolder;

 

 


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.