C
C#3y 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
Angius3y 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
Servator3y ago
Also dont use more than 1 Random
mindhardt
mindhardt3y 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
OliOP3y ago
what is the proper name i have a string and intergers
Angius
Angius3y ago
Well, the proper type for a variable is... what you want to store there int, string, Customer, List<Animal>, or whatever else
Oli
OliOP3y ago
so can i do int and string
mindhardt
mindhardt3y ago
That object is not used btw Its name is shadowed in the method
Angius
Angius3y ago
Yeah, the field isn't used
Oli
OliOP3y 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
mindhardt3y ago
What does it say?
Oli
OliOP3y ago
mindhardt
mindhardt3y ago
Show me your code
Oli
OliOP3y ago
MODiX
MODiX3y ago
Sorry @haakonsen your message contained blocked content and has been removed!
Oli
OliOP3y ago
im stupid hold on
mindhardt
mindhardt3y ago
No new Just Random.Shared.Next Shared is a property, like a variable with a random object
Oli
OliOP3y ago
so i dont need the random rnd = new random
mindhardt
mindhardt3y 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
OliOP3y ago
mindhardt
mindhardt3y ago
Ok, you use older .Net Then create your own random object And use it
Oli
OliOP3y ago
can i just reinstall it or smth
mindhardt
mindhardt3y 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
OliOP3y ago
ok how do i use List<T>
mindhardt
mindhardt3y 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
OliOP3y ago
well im making multiple players than create a team which im going to try loop again cause i need 2 teams
Oli
OliOP3y ago
i tried doing that earlier today and it still doesnt work
Oli
OliOP3y ago
is it cause im using an older version
Angius
Angius3y ago
Well, string.Join() takes two parameters First one is the glue to use for joining, second one is the list
Oli
OliOP3y ago
oh
Oli
OliOP3y ago
it just prints this
Oli
OliOP3y ago
Angius
Angius3y 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
OliOP3y ago
how do i do that thanks btw
Angius
Angius3y 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
OliOP3y ago
cheers all worked thanks @Angius and @Hin
Angius
Angius3y ago
Nice
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mindhardt
mindhardt3y ago
Just use /close I guess associates can do so as well
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server