Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the C# equivalent of NSMutableArray and NSArray?

What is the C# equivalent of NSMutableArray and NSArray?

Does C# have a mutable and non-mutable? I noticed that string appears to be mutable by default.

As a bonus, how do I quickly populate the array in C#?

like image 417
Ethan Allen Avatar asked Dec 11 '11 03:12

Ethan Allen


People also ask

What is C used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language in simple words?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is -= in C?

-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A.


1 Answers

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.

like image 86
BoltClock Avatar answered Oct 14 '22 09:10

BoltClock