❔ 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
Because value typed have an implicit default value
Reference types don't, they default to null
But don't reference types just hold values, like in a array couldn't you look at the values inside and see the type
Also, structs are value types and also need to be
new
ed upOh, you are talking specifically about literal values
Yeah
What is a struct
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
So literal values are structs
Class on heap
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 limitationsOk
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 sameOk
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 directlySo is array a class or does each data type have an array
array is something else. its a reference type thou.
but its not a class
int[]
is not the same as Array
I thought int[] ,was an reference type
it is
its an array.
something can be a reference type without being a class
But it's also a class?
Ok
I literally just said twice that its not a class
I thought you meant array not int[]
int[]
IS an array. its just not an Array
Ok. And int[] is. Struct
no its an array.
its its own thing
I thought you said new makes classes and structs
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 defaultFor note here, the static class
Array
is not []
Though the []
is a reference type which its content's point to something in memorythats what I've been trying to say 🙂
And the constructor makes the space for those 5 ints
yeah. it couldnt do that before, as it didnt know what size of array you wanted
so basically arrays hold variables
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
this is valid, and just sets the value of the 8th integer to 555Ok thank you for the help
yw
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
they dont
Oh?
Angius
REPL Result: Failure
Exception: CompilationErrorException
Compile: 403.426ms | Execution: 0.000ms | React with ❌ to remove this embed.
not like that
just
S s;
Ah, you mean
S s
itself won't be null
yeah
You're rightyup
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
Aaron
REPL Result: Success
Result: bool
Quoted by
<@270457259065081856> from #void-spam (click here)
Compile: 313.457ms | Execution: 74.367ms | React with ❌ to remove this embed.
arrays are as much of a class as any other class
wdym
Definite assignment applies to structs just as much as it applies to classes
Cyberrex
REPL Result: Success
Compile: 502.304ms | Execution: 31.011ms | React with ❌ to remove this embed.
Remember that the repl isn't doing what you think it is
huh
it essentially makes s a field
to see the actual rules, you have to make a method inside the repl
Aaron
REPL Result: Success
Compile: 526.570ms | Execution: 34.776ms | React with ❌ to remove this embed.
this compiles in a normal app as well
333fred
REPL Result: Failure
Exception: CompilationErrorException
Compile: 529.253ms | Execution: 0.000ms | React with ❌ to remove this embed.
it even makes a suggestion if you new it
You need to definitely assign before you can use it
oh before you read yeah
I didn't know the definite assignment rules treated struct fields independently
This suggestion is
s = new() { i = 5 };
oh right lol
nevermind that
I suppose it makes sense to do so
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
yeah so one field can be uninitialized while another can be initialized
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.