✅ How do I set a struct attribute to its parameter name?

Doing this doesn't work.
public struct Example
{
public int id;
public string name;

public Example(int id, string name)
{
id = id;
name = name;
}
}
public struct Example
{
public int id;
public string name;

public Example(int id, string name)
{
id = id;
name = name;
}
}
I'm looking for something like a dataclass that just holds attributes I set once and read off continuously. Looking for something like this:
from dataclasses import dataclass

@dataclass
class Example:
id: int
name: str
from dataclasses import dataclass

@dataclass
class Example:
id: int
name: str
And then to be able to add a bunch to an array:
Example[] ArrayOfExamples = [
Example(id: 1, name: "hi"),
...
]
Example[] ArrayOfExamples = [
Example(id: 1, name: "hi"),
...
]
20 Replies
Pobiega
Pobiega4mo ago
this.id = id;
it’s raining outside
that it?
Pobiega
Pobiega4mo ago
Yeah, assuming your example is correct Oh uh
it’s raining outside
what about for the array
Pobiega
Pobiega4mo ago
Data class would be record
it’s raining outside
ooh whats that and is struct fine or should i use record
Pobiega
Pobiega4mo ago
Your struct is mutable
it’s raining outside
i literally just want something to hold a group of attributes that i can access later on, thats it
Pobiega
Pobiega4mo ago
Records are not Then record should be your default
it’s raining outside
so records are set once, cant set again?
Pobiega
Pobiega4mo ago
Yes
it’s raining outside
awesome also how do you close threads?
Pobiega
Pobiega4mo ago
$close
MODiX
MODiX4mo ago
If you have no further questions, please use /close to mark the forum thread as answered
it’s raining outside
cheers oh wait
Pobiega
Pobiega4mo ago
$records
MODiX
MODiX4mo ago
Use record types tutorial - C#
Learn about how to use record types, build hierarchies of records, and when to choose records over classes.
Pobiega
Pobiega4mo ago
In case you wanna read a out it
it’s raining outside
:thumbsup:
Pobiega
Pobiega4mo ago
And for the array... Exanple[] x = [new(1, "hello")];
Want results from more Discord servers?
Add your server