中の技術日誌ブログ

C#とC++/CLIと
VBと.NETとWindowsで戯れる
 

目次

Blog 利用状況

ニュース

自己紹介

東京でソフトウェアエンジニアをやっています。
お仕事大募集中です。
記事執筆や、講師依頼とかでも何でもどうぞ(*^_^*)
似顔絵 MSMVPロゴ
MSMVP Visual C# Since 2004/04-2013/03

記事カテゴリ

書庫

日記カテゴリ

00-整理

01-MSMVP

第5回 Dictionary

第5回 Dictionary<K, V>

あるキーと値の組み合わせを1.xではHashtableや、IDictionaryで表現してきた。

1.x C#
Hashtable ht = new Hashtable();
ht.Add(3, "A");
ht.Add(1, "B");
ht.Add(2, "C");
MessageBox.Show((string)ht[2]);
1.x VB
Dim ht As Hashtable = New Hashtable
ht.Add(3, "A")
ht.Add(1, "B")
ht.Add(2, "C")
MessageBox.Show(CStr(ht(2)))
1.x MC++
Hashtable __gc *ht = new Hashtable();
ht->Add(__box(3), S"A");
ht->Add(__box(1), S"B");
ht->Add(__box(2), S"C");
MessageBox::Show(static_cast<String*>(ht->get_Item(__box(2))));

型を宣言時に固定化できるということは、すべてのキャストが不要になるということだ。

特にDictionaryでは2値を扱うためキャストが増大傾向にある。

2.0 C#
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(3, "A");
dic.Add(1, "B");
dic.Add(2, "C");
MessageBox.Show(dic[2]);
2.0 VB
Dim dic As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
dic.Add(3, "A")
dic.Add(1, "B")
dic.Add(2, "C")
MessageBox.Show(dic(2))
2.0 C++/CLI
Dictionary<int, String^>^ dic = gcnew Dictionary<int, String^>();
dic->Add(3, "A");
dic->Add(1, "B");
dic->Add(2, "C");
MessageBox::Show(dic->Item[2]);

どうだろう。

今回のソースなどまさしくどの言語も同じ単純明快。

これこそが.Netな世界だといえると思いませんか?

連載をまとめてあります。 http://blogs.users.gr.jp/naka/category/285.aspx

 

投稿日時 : 2004年12月9日 13:03

コメントを追加

# re: 第5回 Dictionary<K, V> 2004/12/09 14:23 河端善博

いい感じですね
はやく、C# 2.0 を業務利用したいです

# KBZHiDhmyQ 2011/12/13 17:36 http://www.birthcontrolremedy.com/birth-control/ya

Left on my site a link to this post. I think many people will be interested in it..!

# gRrDnFmmJqrcoDPcF 2011/12/22 21:48 http://www.discreetpharmacist.com/

uePPv4 Well, actually, a lot of what you write is not quite true !... well, okay, it does not matter:D

# LBZrrXvqoBQKQ 2012/01/04 6:53 http://www.kosherbeefjerky.net/

fxVzcV I decided to help and sent a post to the social bookmarks. I hope to raise it in popularity!!...

タイトル
名前
URL
コメント