What is the C# equivalent of NSMutableArray and NSArray?
That would be ArrayList
and object[]
respectively, if you take the weak typing nature of NSMutableArray
and NSArray
into account.
Arrays and lists in C# (at least for .NET 2.0 and above) can also be strongly-typed, so depending on what kind of object you're storing you can specify that type. For example if you only have NSString
objects in an NSMutableArray
in your Objective-C code, you'd use List<string>
, and if you have them in an NSArray
, you'd use string[]
with a fixed size instead.
To quickly initialize and populate a list or an array in C#, you can use what's known as a collection initializer:
List<string> list = new List<string> { "foo", "bar", "baz" };
string[] array = { "foo", "bar", "baz" }; // Shortcut syntax for arrays
string
in C# is immutable, just like NSString
in the Foundation framework. Every time you assign a new string to a variable, you simply point or refer the variable to a different object.
List<String> stringList = new List<String>();
stringList.Add("Test");
String in C# is immutable. The equivalent of NSMutableString in C# is StringBuilder. C#, unlike Objective-C doesn't represent the method intention clearly on the method name. For example, both classes String and StringBuilder has a method called Replace that you might think, replaces characters. String.Replace replaces and returns the new string whereas StringBuilder.Replace does a in-place replace.
Most methods in most classes in C# (and Java too) work like that.
In Obj-C, naming conventions are clear, it's either
[NSString stringByReplacing...]
[NSMutableString replace...]