C
C#9mo ago
Emelie

❔ How do I initialize a static list with objects? (Quidditch app)

I am feeling completely lost on this. I have a playermanager which contains a static list of Player objects. but no matter what I do, I get errors when I try to add demo players to the list... Why is this? I have attached an image of the static list and the player class itself.
No description
No description
7 Replies
cap5lut
cap5lut9mo ago
there is a list initializer, so u can do it like this:
... = new()
{
new Player(...),
new Player(...)
};
... = new()
{
new Player(...),
new Player(...)
};
there r a lot helpful initializers u might want to read through: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers#collection-initializers
Object and Collection Initializers - C# Programming Guide - C#
Object initializers in C# assign values to accessible fields or properties of an object at creation after invoking a constructor.
Emelie
Emelie9mo ago
perfect, just what I was looking for! thank you
cap5lut
cap5lut9mo ago
and if u would have more complex logic going on, u would use static constructors https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
Static Constructors - C# Programming Guide - C#
A static constructor in C# initializes static data or performs an action done only once. It runs before the first instance is created or static members are referenced.
cap5lut
cap5lut9mo ago
the shown way is just syntax sugar, under the hood a static constructor is generated for that anyway. u can observe this here: https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQAwAIpwCwG5nJQDMmM6AwsgN7Lp3oAOATgJYBuAhgC4CmmcANnQAZFgGcuAHhYA7LgD50MgK4BbAEY8mY9AF4lPAO4AKAJToq6OABp0ZAL74k9oA==
SharpLab
C#/VB/F# compiler playground.
cap5lut
cap5lut9mo ago
if this answered ur questions please $close the thread
MODiX
MODiX9mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ Transparent BackgroundI'm trying to use AvaloniaUI in my project (linux, mac, and windows only) but I'm not sure how to cr✅ EF Core long running query causing concurrency issuesHello. I have a .NET application that uses a DbContext with PostgreSQL. To test the performance of m❔ (Beginner) Seeking Advice on Distributing Items Equally in C# ProgramHey everyone! I hope you're doing well. I'm still pretty new around here and not exactly a pro in t❔ ENV var dump collection not working on some machinesI'm using these env vars to collect dumps from my .net core macOS app (docs: https://learn.microsoft❔ How to play music files from resources/relative paths in wpf c# ?I have spend good 2 to 3 hours trying to get a sound file played from relative path, it just doesnt ❔ Simple code that I dont understand why it wont work.Hello, I am an IT specialized class student and we started learning C# in class, I am trying out somEmulating Windows FeaturesHello, I'm trying to figure out a couple of things before I start a new project. The first of which ❔ IEnumerable to ObservableCollectionI am not sure if im doing this correctly but I am trying to create a sqlite table with some data in ❔ EF Core optional where extension methodI have a lot of optional parameters for queries. Filter by this, filter by that etc. I can do it lik❔ Need help on UI Design and tips for implementation - .Net Mauithree questions: 1. How can I edit the blue bar on the top? I'm not sure where its coming from, I o