C
C#15mo ago
Figure

❔ why new is needed for reference types

Can someone explain why the new operator is used for references types but not value types. When I assign an variable x of type int to 10 is 10 just binary in the computer or an object of the int class. And what does it mean for to store references to your data and how is that the same as an object
64 Replies
Pobiega
Pobiega15mo ago
Because value typed have an implicit default value Reference types don't, they default to null
Figure
FigureOP15mo ago
But don't reference types just hold values, like in a array couldn't you look at the values inside and see the type
Angius
Angius15mo ago
Also, structs are value types and also need to be newed up
Pobiega
Pobiega15mo ago
Oh, you are talking specifically about literal values
Figure
FigureOP15mo ago
Yeah What is a struct
Angius
Angius15mo ago
I'd go more along the lines of "primitives don't need to be newed up" While complex types that consist of those primitives, do It's like a class but lives on the... uh... stack? Can never remember what lives in the stack and what on the heap lol
Figure
FigureOP15mo ago
So literal values are structs
Pobiega
Pobiega15mo ago
Class on heap
Angius
Angius15mo ago
And struct on stack, so I was correct, nice Technically not all of them, since System.String is a class due to struct's size limitations
Figure
FigureOP15mo ago
Ok
Pobiega
Pobiega15mo ago
When you do int x = 10; a place in the stack (assuming this is a local variable) will be reserved to hold an integer, currently with the value 10 you can later do x = 123456; and the value stored there changes, but the size and location of the reservation in the stack remains the same
Figure
FigureOP15mo ago
Ok
Pobiega
Pobiega15mo ago
new is how we call the constructor for a class/struct, and thats what that keyword does int doesnt need a constructor, as its values are always stored directly
Figure
FigureOP15mo ago
So is array a class or does each data type have an array
Pobiega
Pobiega15mo ago
array is something else. its a reference type thou. but its not a class int[] is not the same as Array
Figure
FigureOP15mo ago
I thought int[] ,was an reference type
Pobiega
Pobiega15mo ago
it is its an array. something can be a reference type without being a class
Figure
FigureOP15mo ago
But it's also a class? Ok
Pobiega
Pobiega15mo ago
I literally just said twice that its not a class
Figure
FigureOP15mo ago
I thought you meant array not int[]
Pobiega
Pobiega15mo ago
int[] IS an array. its just not an Array
Figure
FigureOP15mo ago
Ok. And int[] is. Struct
Pobiega
Pobiega15mo ago
no its an array. its its own thing
Figure
FigureOP15mo ago
I thought you said new makes classes and structs
Pobiega
Pobiega15mo ago
int[] is a reference to a piece of memory new int[5] is the space required for 5 ints, consecutively all initialized to 0, by default
arion
arion15mo ago
For note here, the static class Array is not [] Though the [] is a reference type which its content's point to something in memory
Pobiega
Pobiega15mo ago
thats what I've been trying to say 🙂
Figure
FigureOP15mo ago
And the constructor makes the space for those 5 ints
Pobiega
Pobiega15mo ago
yeah. it couldnt do that before, as it didnt know what size of array you wanted
Figure
FigureOP15mo ago
so basically arrays hold variables
Pobiega
Pobiega15mo ago
int[] is the type of the variable, much like string or List<String> etc an array with length 5 and one with length 10000 have the same type, int[] I wouldn't say that, its one variable holding Length amount of items
int[] xs = new int[10];
xs[7] = 555;
int[] xs = new int[10];
xs[7] = 555;
this is valid, and just sets the value of the 8th integer to 555
Figure
FigureOP15mo ago
Ok thank you for the help
Pobiega
Pobiega15mo ago
yw
333fred
333fred15mo ago
More correctly, structs live where their containers live. A struct field of a class is on the heap, because that's where its container is Arrays are classes, but they're very special. Both C# and the runtime have special instructions just for them It's perfectly fine to treat them as if they're their own special category of thing
sibber
sibber15mo ago
they dont
Angius
Angius15mo ago
Oh?
MODiX
MODiX15mo ago
Angius
REPL Result: Failure
struct S {}
S s = S();
struct S {}
S s = S();
Exception: CompilationErrorException
- Non-invocable member 'S' cannot be used like a method.
- Non-invocable member 'S' cannot be used like a method.
Compile: 403.426ms | Execution: 0.000ms | React with ❌ to remove this embed.
sibber
sibber15mo ago
not like that just S s;
Angius
Angius15mo ago
Ah, you mean S s itself won't be null yeah You're right
sibber
sibber15mo ago
yup
333fred
333fred15mo ago
They do You need to assign some value to the local or it won't allow you to access it Most commonly, this is with new
MODiX
MODiX15mo ago
Aaron
REPL Result: Success
new int[5] is Array
new int[5] is Array
Result: bool
True
True
Quoted by
<@270457259065081856> from #void-spam (click here)
Compile: 313.457ms | Execution: 74.367ms | React with ❌ to remove this embed.
Aaron
Aaron15mo ago
arrays are as much of a class as any other class
sibber
sibber15mo ago
wdym
333fred
333fred15mo ago
Definite assignment applies to structs just as much as it applies to classes
MODiX
MODiX15mo ago
Cyberrex
REPL Result: Success
S s;
s.i = 5;
struct S
{
public int i;
}
S s;
s.i = 5;
struct S
{
public int i;
}
Compile: 502.304ms | Execution: 31.011ms | React with ❌ to remove this embed.
333fred
333fred15mo ago
Remember that the repl isn't doing what you think it is
sibber
sibber15mo ago
huh
Aaron
Aaron15mo ago
it essentially makes s a field to see the actual rules, you have to make a method inside the repl
MODiX
MODiX15mo ago
Aaron
REPL Result: Success
void M() {
S s;
s.i = 5;
}
M();
struct S
{
public int i;
}
void M() {
S s;
s.i = 5;
}
M();
struct S
{
public int i;
}
Compile: 526.570ms | Execution: 34.776ms | React with ❌ to remove this embed.
sibber
sibber15mo ago
this compiles in a normal app as well
Aaron
Aaron15mo ago
spinthink
MODiX
MODiX15mo ago
333fred
REPL Result: Failure
void M() {
S s;
_ = s.i;
}
M();
struct S
{
public int i;
}
void M() {
S s;
_ = s.i;
}
M();
struct S
{
public int i;
}
Exception: CompilationErrorException
- Use of possibly unassigned field 'i'
- Use of possibly unassigned field 'i'
Compile: 529.253ms | Execution: 0.000ms | React with ❌ to remove this embed.
sibber
sibber15mo ago
it even makes a suggestion if you new it
No description
333fred
333fred15mo ago
You need to definitely assign before you can use it
sibber
sibber15mo ago
oh before you read yeah
Aaron
Aaron15mo ago
I didn't know the definite assignment rules treated struct fields independently
333fred
333fred15mo ago
This suggestion is s = new() { i = 5 };
sibber
sibber15mo ago
oh right lol nevermind that
Aaron
Aaron15mo ago
I suppose it makes sense to do so
333fred
333fred15mo ago
Anyway, this is all way above the head of the original question. For simplicity's sake: you need to assign some value to any variable before you can use it
sibber
sibber15mo ago
yeah so one field can be uninitialized while another can be initialized
Accord
Accord15mo 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