✅ variables declared with/without the “new” keyword.
What difference does using the word
new
make in the code? even in terms of memory allocations n stuff.
also in this code how many TrialObjects
objects were created?
13 Replies
two objects are created here
In general,
new
means a new object is created.
The variables here aren't declared with the new
keyword, they're just assigned a new object.and does this have to do anything with stack/heap
if the thing youre newing is a reference type, itll be allocated on the heap
otherwise its on the stack
so in this example, would the testObject1 be on the stack?
if its a reference type
classes are reference types
reference type (class, interface, delegate) -> heap
value type (struct) -> stack
noted
cheers
Often times but not always yep. Even structs can land on the heap
wdym not always?
when does newing a struct allocate it on the heap
This is a good read
Stack Overflow
Are Structs always stack allocated or sometimes heap allocated?
I was of the impression that in C#, struct elements are allocated on the stack and thus disappear when returning from a method in which they were created. But what happens if I place the struct-val...
oh when theyre a member of a class yeah of course
or an array
i was thinking in the context of a function
$close
Use the
/close
command to mark a forum thread as answered