Ironpython使用C#對象的筆記

1. 
#List類型的定義
from System.Collections.Generic import List, Dictionary
int_list = List[int]()

#同樣Byte類型的值的定義
from System import Byte
b = Byte(1)   #


2.
C# lambada
>>> from System.Collections.Generic import IEnumerable, List
>>> list = List[int]([1, 2, 3])
>>> import clr
>>> clr.AddReference("System.Core")
>>> from System.Linq import Enumerable
>>> Enumerable.Any(list,lambda x:x<2)
True
>>> Enumerable.Any(list,lambda x:x>2)
True
>>> Enumerable.Any(list,lambda x:x>4)
False

3. C#中的byte[]數組,在Ironpython中這樣表示
from System import Array,Byte
bbyte = Array[Byte]  

4. ref 和 out
>>> from System.Collections.Generic import Dictionary
>>> d = { "a":100.1, "b":200.2, "c":300.3 }
>>> d = Dictionary[str, float](d)
>>> d.TryGetValue("b")
(True, 200.2)
>>> import clr
>>> r = clr.Reference[float]()
>>> d.TryGetValue("b", r)
True
>>> r.Value
200.2
>>>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章