✅ 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
Pobiega6mo ago
this.id = id;
slowly losing it
slowly losing itOP6mo ago
that it?
Pobiega
Pobiega6mo ago
Yeah, assuming your example is correct Oh uh
slowly losing it
slowly losing itOP6mo ago
what about for the array
Pobiega
Pobiega6mo ago
Data class would be record
slowly losing it
slowly losing itOP6mo ago
ooh whats that and is struct fine or should i use record
Pobiega
Pobiega6mo ago
Your struct is mutable
slowly losing it
slowly losing itOP6mo ago
i literally just want something to hold a group of attributes that i can access later on, thats it
Pobiega
Pobiega6mo ago
Records are not Then record should be your default
slowly losing it
slowly losing itOP6mo ago
so records are set once, cant set again?
Pobiega
Pobiega6mo ago
Yes
slowly losing it
slowly losing itOP6mo ago
awesome also how do you close threads?
Pobiega
Pobiega6mo ago
$close
MODiX
MODiX6mo ago
If you have no further questions, please use /close to mark the forum thread as answered
slowly losing it
slowly losing itOP6mo ago
cheers oh wait
Pobiega
Pobiega6mo ago
$records
MODiX
MODiX6mo 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
Pobiega6mo ago
In case you wanna read a out it
slowly losing it
slowly losing itOP6mo ago
:thumbsup:
Pobiega
Pobiega6mo ago
And for the array... Exanple[] x = [new(1, "hello")];

Did you find this page helpful?