C#一些實用的,容易被遺忘的特性,經驗和技巧

來源:互聯網
上載者:User

  突然想到,想要通過名字來獲得枚舉的值,看了不少的資料,發現上面都只是解釋枚舉的文法而已,對於它的實際應用,一點也不提及,難道只能用switch來判斷?不好吧,這樣也未免太土了點。後來發現了枚舉還有這樣的用法,真的很使用,看代碼:

 

Code
namespace EnumTest
{
enum date { sun, mon, tue, wes, thu, fri, sat }

class Program
{
static void Main(string[] args)
{
Console.WriteLine("輸入星期的名稱:");
string name = Console.ReadLine();

//Type參數表示要轉換成的枚舉的類型,true指示忽略大小寫
object obj = Enum.Parse(typeof(date), name, true);
Console.WriteLine("輸出星期的數字:");
Console.WriteLine(obj + ":" + (int)obj);
}
}
}

 

再來看一下運行結果圖:

  怎麼樣,是不是覺得很實用啊。

 

  接下來要介紹的是委託(delegate),有對delegate不太瞭解的人可以看一下我以前寫的一個隨筆什麼是委託(delegate) 。我們平時使用委託都是單個的指定委託的方法,但是如果我們需要通過參數動態指定呢?難道也是用switch?這樣做太麻煩了,還有更好的方法,看代碼:

 

Code
namespace DelegateTest
{
class Person
{
public void FirstMethod()
{
Console.WriteLine("這是第一個方法!");
}
public void SecondMethod()
{
Console.WriteLine("這是第二個方法!");
}
}

delegate void dele();

class Program
{
static void Main(string[] args)
{
Console.WriteLine("輸入方法的名字:");
string name = Console.ReadLine();
Person p = new Person();

//Type參數是要轉換的委託的類型,p是要調用的委託的執行個體,true指示忽略大小寫
dele d = Delegate.CreateDelegate(typeof(dele), p, name + "Method", true) as dele;
d.Invoke();
Console.WriteLine("輸入方法的名字:");
name = Console.ReadLine();
d = Delegate.CreateDelegate(typeof(dele), p, name + "method", true) as dele;
d.Invoke();
}
}
}

 

下面是啟動並執行結果圖:

 

  接下來是一個C#經常被忽略的特性,匿名方法。雖然匿名方法很少用到,但是知道總比不知道的好。以delegate為例,我們平時都是賦予delegate具體的方法,但是有時候有一個方法要委託,但是它要執行的功能實在是很簡單,而且可能也就只用一次而已,但是要給它寫一個具體的方法實在是麻煩。而C#2.0為我們提供了匿名方法的特性,代碼如下:

 

Code
namespace Anonymity
{
delegate void dele();
class Program
{
static void Main(string[] args)
{
dele d = delegate() { Console.WriteLine("這是一個匿名方法!"); };
d.Invoke();
}
}
}

 

運行結果如:

 

  接下來是索引器(Indexer)了,寫技術文章不像寫小說,真的挺累的。要構思,要描述,要寫代碼,要調試,更重要的是,要敲鍵盤。好了,抱怨到些為止。索引器(Indexer)能讓我們像訪問數組一樣訪問對象,至於它有什麼作用呢?只有用到了才知道,看代碼:

 

Code
namespace IndexerTest
{
class PersonNames
{
List<string> names = new List<string>();
/// <summary>
/// 為對象建立索引器,注意它和屬性的get/set的不同
/// </summary>
/// <param name="index">索引值</param>
/// <returns></returns>
public string this[int index]
{
get { return names[index]; }
set { names.Add(value); }
}
}

class Program
{
static void Main(string[] args)
{
PersonNames names = new PersonNames();
names[0] = "微軟";
names[1] = "Google";
names[2] = "百度";
for (int i = 0; i < 3; i++)
{
Console.WriteLine(names[i]);
}
}
}
}

 

  遺憾的是,它不支援foreach送代。要實現送代的功能,可以實現IEnumerable介面。

 

  接下來是。。。?還有接下來?是不是看得很累了,其實我也寫得很累了。接下來是雜湊表(HashTable),HashTable大家都知道很好用,大家可能會發現,它好像不能使用foreach來送代。其實它是可以送代的,只是稍微有點不同而已,看代碼:

 

Code
namespace HashTableTest
{
class Program
{
static void Main(string[] args)
{
Hashtable table = new Hashtable();
table["name1"] = "微軟";
table.Add("name2", "Google");
table["name3"] = "百度";
//注意name的類型名
foreach (DictionaryEntry name in table)
{
Console.WriteLine(name.Key + ":" + name.Value);
}
}
}
}

 

運行結果如:

  大家仔細看一下,它送代返回的類型是DictionaryEntry而不是我們預料中的string。為什麼是這樣的呢?HashTable不同於其它的Collection,它儲存的不僅是值(value),同時還儲存著鍵(key),它們在HashTable中是以DictionaryEntry類型儲存著的。再回顧一下上面的索引器(Indexer),我們就能推斷出,HashTable可能是一個實現了索引器功能的類(不是別罵我,我也是猜的)。

  好了,終於寫完了,希望對大家有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.