See Also: NSDictionary Members
This is an immutable dictionary, to create mutable ones, use the MonoTouch.Foundation.NSMutableDictionary type.
There is a convenient constructor that takes at least one key and one value, with optional values that will create the dictionary by pairing each key with a value. If you use .NET objects, the keys and values will first be boxed into NSObjects using NSObject.FromObject.
c# Example
// // Using C# objects, strings and ints, produces // a dictionary with 2 NSString keys, "key1" and "key2" // and two NSNumbers with the values 1 and 2 // var dict = new NSDictionary ("key1", 1, "key2", 2); // // Using NSObjects directly, gives you fine control // var key1 = new NSString ("key1"); vae value1 = new NSNumber ((byte) 1); var key2 = new NSString ("key2"); vae value2 = new NSNumber ((byte) 2); var dict2 = new NSDictionary (key1, value1, key2, value2);
Sometimes, you might have already an array of keys and values, so you can use the following factory methods:
c# Example
var keys = new object [] { "key1", "key2" }; var values = new object [] { "value1", "value2" }: var dict = NSDictionary.FromObjectsAndKeys (values, keys);
The NSDictionary type implements the ICollection, IDictionary and the generic ICollection and IDictionary interfaces, which makes it convenient to use with existing .NET APIs and also provides indexers that take an NSObject, an NSString or a C# string as well as an enumerator.
In some cases, where you might be iterating over a loop, or you have not surfaced a bound type, but you have the handle to the key, you can use the NSDictionary.LowlevelObjectForKey which takes a handle for the key and returns a handle for the returned object.