✅ 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?
TrialObjects testObj1 = new TrialObjects("some string argument1");
TrialObjects testObj2 = new TrialObjects("some string argument2");
TrialObjects testObj3 = testObj1;
TrialObjects testObj1 = new TrialObjects("some string argument1");
TrialObjects testObj2 = new TrialObjects("some string argument2");
TrialObjects testObj3 = testObj1;
13 Replies
Thinker
Thinker2y ago
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.
SolwayFirth
SolwayFirthOP2y ago
and does this have to do anything with stack/heap
sibber
sibber2y ago
if the thing youre newing is a reference type, itll be allocated on the heap otherwise its on the stack
SolwayFirth
SolwayFirthOP2y ago
so in this example, would the testObject1 be on the stack?
sibber
sibber2y ago
if its a reference type classes are reference types
Thinker
Thinker2y ago
reference type (class, interface, delegate) -> heap value type (struct) -> stack
SolwayFirth
SolwayFirthOP2y ago
noted cheers
qqdev
qqdev2y ago
Often times but not always yep. Even structs can land on the heap
sibber
sibber2y ago
wdym not always? when does newing a struct allocate it on the heap
qqdev
qqdev2y ago
This is a good read
qqdev
qqdev2y ago
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...
sibber
sibber2y ago
oh when theyre a member of a class yeah of course or an array i was thinking in the context of a function $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server