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