Coding Tutorial: In .NET, there is a fundamental division between value types and reference types. All is explained here.
(Yes, I know I wrote 'CreateOjects' not 'CreateObjects'.)
Source code available at github.com/JasperKent/Val-and...
Coding Tutorial: In .NET, there is a fundamental division between value types and reference types. All is explained here.
(Yes, I know I wrote 'CreateOjects' not 'CreateObjects'.)
Source code available at github.com/JasperKent/Val-and...
Anything other fundamental features of C# you're interested in? Let me know. Source code at: github.com/JasperKent/Val-and-Ref-Types Don't forget to subscribe at kzhead.info/tools/qWQzlUDdllnLmtgfSgYTCA.html And if you liked it, click the 👍.
If we have a structure with a string type (or class ) as one of its property, how are they handled, are they still placed on Stack , or just string property is explicitly placed on heap ?
Excellent excellent excellent! Learning programming becomes simple when you are lucky enough to discover such a superb explanation!! Thank you very much!!
This is the best explanation i've heard on this topic.
I like to think of passing by reference as passing by value. Instead of passing a copy of a normal value type, we pass a copy of a memory address on the heap.
Wow, the best explanation I have seen so far. Thx for this!
These are brilliant - keep up the great work and thanks for your time!
Glad you like them!
Great explanation!
Well explained, thank you.
Thanks a lot! That is really helpful, your explanation is clear and easy to understand!
Great video. The best explanation i have ever seen on KZhead
Glad it was helpful!
There is a good article that you can find on real world performance that you can google. "Stack allocation vs heap allocation - performance benchmark". But any way on 1 million loops with 10 allocation+init+free attempts, we see that difference is about 8% at 60KB total processing size. Which also also not a big show stopper.
Excelent tutorial, thanks.
awesome video, thanks!
Amazing!!
Love your videos. You should do a video about correctly implementing the Clone method/interface.
I'll put it on the list.
@@CodingTutorialsAreGo Thanks. I look forward to it.
lul c# is really funny when one comes from c++ background... I was so lost.
Another great explanation! Thank you! Also... what program did you use to create your sack pointer animation on the right side at the first half of the video?
All done in Adobe Premiere Pro.
@@CodingTutorialsAreGo I bet That took a fair amount of extra time. Well worth it though! It really made things clear. Thank you!
Yeah, it takes a while. When I'm teaching live, I just use a virtual whiteboard, which does it in real time. But it's such a mess, I don't want it recorded for posterity.
Very good! And how are the p1 p2 fred (names of objects) stored? In which part of the memory?
On the stack, as shown.
@@CodingTutorialsAreGo I meant the names itself. For example address 2 in stack memory contains a reference/address of where an object on the heap is stored. Where is the name p1 stored?
@@nononnomonohjghdgdshrsrhsjgd It's not. Local variable names are removed during compilation leaving only the positions on the stack.
12:56
What will happen in Stack if we have: var a = 5; a = 6; Will the compiler overrite the value of the existing variable "a" (5 to 6) OR It will create new object "a" with value of 6? Same question for string? var a = "aa"; a = "bb"; Will the compiler override the reference of "a" in the Stack OR Will create new object "a" with the newer reference?
In the case of ints, a is an int and therefore a value type, so only one block of stack storage is created with a value 5 which is then overwritten to 6. (Actually, in this simple case the compiler might spot the optimisation and just set a directly to 6, but in general it overwrites.) In the string case, there will be one reference block allocated on the stack for a and two blocks on the heap, for "aa" and "bb". a will initially contain the reference to "aa" but will then be overwritten with the reference to "bb". (Again, assuming no compiler optimisations.)
@@CodingTutorialsAreGo Perfect! Thank you very much! :)
In Java we have Wrapper Classes to make primitive types to reference types.However in C# int is inherited from Object class.How can it is possible to both inherited and being stored in stack.What happens if I call toString() method on a int variable is it will be allocated to the heap?.I dont understand how it is possible to inherited from object if it is stored in stack it is the opposite of java.
The fundamental thing that is required to allocate an object on the stack is that the compiler knows its size. It doesn't matter if it's inherited as long as the compiler knows the size of the overall object. In C++, for example, you can store anything on the stack, whatever the depth of the inheritance. What you can't do in C# is have a base class reference to a derived class object stored on the stack. C# cheats this using boxing. A C# box is the same concept as the Java wrapper, except that it is automatically created by the compiler, whereas in Java they are predefined (or they were last time I used Java - it may have moved on). See my video on boxing: kzhead.info/sun/hZVukcmOkImce30/bejne.html
@@CodingTutorialsAreGo Thanks for high quality videos and explanation.As I understand , when we call toString() method for example in a value type still it is stored in stack which is a great advantage if we compare the design preference with Java.Because there is no need boxing nor wrapper classes to use methods like toString if we want to use them.So, when I call an toString method in int value is c# compiler does autoboxing ?
@@mert.pinarbasi When you call ToString on a value type (e.g. int) then the value type remains on the stack. There is no boxing. The int remains on the stack. The returned string, obviously, must be on the heap.