I am a Java programmer, I have used a Java ArrayList
before and now I want to have something like that in C#. Some of options I need are in this Java code:
String[] strs = new String[]{"str1" , "str2" , "str3" , "str4"}; ArrayList arrayList = new ArrayList(35); arrayList.add(strs[0]); arrayList.add(strs[1]); arrayList.remove(0); arrayList.set(0, strs[2]); String s = (String) arrayList.get(1);
I used C# ArrayList
and LinkedList
, but they don't have these simple options that I need. Is there another option in C# supporting accessing objects with indexes, inserting and removing from certain index?
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 ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? 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.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
use List <T>
String[] strs = new String[]{"str1" , "str2" , "str3" , "str4"}; List<string> stringList = new List<string>(); stringList.add(strs[0]); stringList.add(strs[1]); stringList.RemoveAt(indexYouWantToDelete) String s = stringList[0];
ArrayLists in c# come from the pre-generic era tho. Since C# 2.0 we have generic collections, List <T>
being one example of that. As the comment on this answer says, if you use an ArrayList, the elements that you put into the arraylist will have to be boxed (to Object
, because thats the only thing an ArrayList takes as input). If you want to access them after that, they will have to be explicitly unboxed, like what you did in your question. ( --> String s = (String) arrayList.get(1);
)
using generic collections (like List <T>
), there is no boxing anymore, as the compiler knows what datatype the list will consist of. In this case, Strings. You could also have a List<int>
, List<char>
, or List<whatever>
, and you can use the same indexing functionality on them.
Use List<T>
...........................................
which has Add
Remove
, RemoveAt
indexers like list[i]
etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With