❔ Fix sized array/list

I'm quite new to C#, evidently, and I'm trying to initialise a list (or an array) in a class that shall be defined when an instance of this class is made. Something like:
class Testing {
Private int[] _cords = new int[2];

Public Testing(int num1, int num2) {
_cords = {num1, num2};
}
}
class Testing {
Private int[] _cords = new int[2];

Public Testing(int num1, int num2) {
_cords = {num1, num2};
}
}
this code is ofcourse wrong, but I believe it should kinda explain what im trying to do.
20 Replies
Rotaerk
Rotaerk2y ago
so when you do:
class Foo
{
T fieldName = initialValue;
}
class Foo
{
T fieldName = initialValue;
}
when the code is compiled, that initialization is actually put into the constructor ... so that's basically the same as:
class Foo
{
T fieldName;
public Foo()
{
fieldName = initialValue;
}
}
class Foo
{
T fieldName;
public Foo()
{
fieldName = initialValue;
}
}
performing this transformation on your code, you essentially have this:
class Testing
{
int[] _cords;
public Testing(int num1, int num2)
{
_cords = new int[2];
_cords = {num1, num2};
}
}
class Testing
{
int[] _cords;
public Testing(int num1, int num2)
{
_cords = new int[2];
_cords = {num1, num2};
}
}
so the first problem here is that you're initializing the thing twice it's redundant ... only the second one will be kept, and the first is a waste of time second, the syntax itself is wrong in the second case
ACiDCA7
ACiDCA72y ago
well the first problem is actually that access modifiers shouldnt be pascalcase^^
Rotaerk
Rotaerk2y ago
you can do new int[] { num1, num2 } lol yeah I just silently fixed that I haven't used C# version higher than 7, so not sure what syntaxes they've added...
ACiDCA7
ACiDCA72y ago
for the other points i fully agree what you are saying ;)
Rotaerk
Rotaerk2y ago
but I think the most concise way to initialize that would be _cords = new [] { num1, num2 }; unless there's something newer
ACiDCA7
ACiDCA72y ago
_cords = new[] {num1, num2};
_cords = new[] {num1, num2};
should work in newer versions i thought in your example there would still be an int in there but after rereading you already omited it... then ignore what i wrote
SolwayFirth
SolwayFirthOP2y ago
okay okay thank you goodpeople ✌️
Rotaerk
Rotaerk2y ago
the explicit way is new int[2] { num1, num2 } but the above syntax omits the type and size because they can be inferred also, if these are coordinates, then maybe you should add a second o to the name of your field 😛
SolwayFirth
SolwayFirthOP2y ago
whoops
Anton
Anton2y ago
do you want an inline buffer or a heap allocated one? because you can technically do any
SolwayFirth
SolwayFirthOP2y ago
i just want to use the least amount of memory that i can
Aaron
Aaron2y ago
even just = { num1, num2 } works
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
int[] a = { 1, 2, 3 };
a
int[] a = { 1, 2, 3 };
a
Result: int[]
[
1,
2,
3
]
[
1,
2,
3
]
Compile: 411.493ms | Execution: 66.299ms | React with ❌ to remove this embed.
Anton
Anton2y ago
the least possible would be fixed-size arrays, but you need unsafe code for those there's the unsafe keyword for types and the fixed keyword for arrays
Aaron
Aaron2y ago
uh? no you don't if you want to use little memory here, that's easy
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
class Testing
{
private (int x, int y) _cords;

public Testing(int num1, int num2)
{
_cords = (num1, num2);
}
}
class Testing
{
private (int x, int y) _cords;

public Testing(int num1, int num2)
{
_cords = (num1, num2);
}
}
Compile: 527.634ms | Execution: 42.235ms | React with ❌ to remove this embed.
Anton
Anton2y ago
yeah this too, but this one's not an array not that it matters though
SolwayFirth
SolwayFirthOP2y ago
okay thats what im gonna implement
Anton
Anton2y ago
if you had 10 items, a tuple would be less viable
Accord
Accord2y 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