C
C#2y ago
Oli

how do i print an arraylist [Answered]

im new to c and ive been trying to print an arraylist and im still not too sure now to
39 Replies
Angius
Angius2y ago
Step 1 would be not using ArrayList. It's a relic of a pre-generics era that should've been removed from the language decades ago. Use List<T> instead I also see you use object. Don't. Use the proper type name Far as printing lists goes, though, string.Join() is the most common way
Servator
Servator2y ago
Also dont use more than 1 Random
mindhardt
mindhardt2y ago
No need to create new Random objects, there is a Singleton since I guess .NET 5. new Random.Next() -> Random.Shared.Next() It is thread-safe and saves memory And also, c# naming convention uses PascalCase for classes and camelCase for local variables, no snake_case
Oli
Oli2y ago
what is the proper name i have a string and intergers
Angius
Angius2y ago
Well, the proper type for a variable is... what you want to store there int, string, Customer, List<Animal>, or whatever else
Oli
Oli2y ago
so can i do int and string
mindhardt
mindhardt2y ago
That object is not used btw Its name is shadowed in the method
Angius
Angius2y ago
Yeah, the field isn't used
Oli
Oli2y ago
so how would i make a list with strings and intergers it doesnt let me do shared im fairly use i used net 5 for this but idk at this point
mindhardt
mindhardt2y ago
What does it say?
Oli
Oli2y ago
mindhardt
mindhardt2y ago
Show me your code
Oli
Oli2y ago
MODiX
MODiX2y ago
Sorry @haakonsen your message contained blocked content and has been removed!
Oli
Oli2y ago
im stupid hold on
mindhardt
mindhardt2y ago
No new Just Random.Shared.Next Shared is a property, like a variable with a random object
Oli
Oli2y ago
so i dont need the random rnd = new random
mindhardt
mindhardt2y ago
No Ok, you see, this line creates a Random object for you to use You can do this, sure, or you can use Random.Shared Both are fine
Oli
Oli2y ago
mindhardt
mindhardt2y ago
Ok, you use older .Net Then create your own random object And use it
Oli
Oli2y ago
can i just reinstall it or smth
mindhardt
mindhardt2y ago
Too much hustle, why would you? I mean creating Random objects is what people did for decades There is nothing wrong with it
Oli
Oli2y ago
ok how do i use List<T>
mindhardt
mindhardt2y ago
T is a type you need In your case I guess it is List<team_create> That name is bad btw Change it to something that makes sense, like Player
Oli
Oli2y ago
well im making multiple players than create a team which im going to try loop again cause i need 2 teams
Oli
Oli2y ago
i tried doing that earlier today and it still doesnt work
Oli
Oli2y ago
is it cause im using an older version
Angius
Angius2y ago
Well, string.Join() takes two parameters First one is the glue to use for joining, second one is the list
Oli
Oli2y ago
oh
Oli
Oli2y ago
it just prints this
Oli
Oli2y ago
Angius
Angius2y ago
Because, for each element, it uses the default .ToString() method of base class object. You can override it for your class to return what you want to return
Oli
Oli2y ago
how do i do that thanks btw
Angius
Angius2y ago
How to override the ToString method - C# Programming Guide
Learn how to override the ToString method in C#. Every class or struct inherits Object and gets ToString, which returns a string representation of that object.
Oli
Oli2y ago
cheers all worked thanks @Angius and @Hin
Angius
Angius2y ago
Nice
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mindhardt
mindhardt2y ago
Just use /close I guess associates can do so as well
Accord
Accord2y ago
✅ This post has been marked as answered!