C# Value Types and Reference Types

2021 ж. 14 Қаң.
4 494 Рет қаралды

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 👍.

    @CodingTutorialsAreGo@CodingTutorialsAreGo3 жыл бұрын
    • 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 ?

      @finwwwfinwww4669@finwwwfinwww46692 ай бұрын
  • Excellent excellent excellent! Learning programming becomes simple when you are lucky enough to discover such a superb explanation!! Thank you very much!!

    @bioanu@bioanu2 жыл бұрын
  • This is the best explanation i've heard on this topic.

    @alexpajp123@alexpajp1232 жыл бұрын
  • 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.

    @Pluvo2for1@Pluvo2for1Ай бұрын
  • Wow, the best explanation I have seen so far. Thx for this!

    @dolusdirectu@dolusdirectu3 жыл бұрын
  • These are brilliant - keep up the great work and thanks for your time!

    @zachgh6111@zachgh61113 жыл бұрын
    • Glad you like them!

      @CodingTutorialsAreGo@CodingTutorialsAreGo3 жыл бұрын
  • Great explanation!

    @Fernando-mh7st@Fernando-mh7st Жыл бұрын
  • Well explained, thank you.

    @jonlicht4514@jonlicht4514 Жыл бұрын
  • Thanks a lot! That is really helpful, your explanation is clear and easy to understand!

    @yegorkatrechko1736@yegorkatrechko173610 ай бұрын
  • Great video. The best explanation i have ever seen on KZhead

    @user-yi4zd6gh5h@user-yi4zd6gh5h3 жыл бұрын
    • Glad it was helpful!

      @CodingTutorialsAreGo@CodingTutorialsAreGo3 жыл бұрын
  • 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.

    @davidwhite2011@davidwhite20113 жыл бұрын
  • Excelent tutorial, thanks.

    @kiPROBRos@kiPROBRos3 жыл бұрын
  • awesome video, thanks!

    @John_Macaroni@John_Macaroni Жыл бұрын
  • Amazing!!

    @davidmoshkowitz4043@davidmoshkowitz40432 жыл бұрын
  • Love your videos. You should do a video about correctly implementing the Clone method/interface.

    @CuriousCyclist@CuriousCyclist2 жыл бұрын
    • I'll put it on the list.

      @CodingTutorialsAreGo@CodingTutorialsAreGo2 жыл бұрын
    • @@CodingTutorialsAreGo Thanks. I look forward to it.

      @CuriousCyclist@CuriousCyclist2 жыл бұрын
  • lul c# is really funny when one comes from c++ background... I was so lost.

    @zanagi@zanagiАй бұрын
  • 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?

    @DedicatedManagers@DedicatedManagers3 жыл бұрын
    • All done in Adobe Premiere Pro.

      @CodingTutorialsAreGo@CodingTutorialsAreGo3 жыл бұрын
    • @@CodingTutorialsAreGo I bet That took a fair amount of extra time. Well worth it though! It really made things clear. Thank you!

      @DedicatedManagers@DedicatedManagers3 жыл бұрын
    • 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.

      @CodingTutorialsAreGo@CodingTutorialsAreGo3 жыл бұрын
  • Very good! And how are the p1 p2 fred (names of objects) stored? In which part of the memory?

    @nononnomonohjghdgdshrsrhsjgd@nononnomonohjghdgdshrsrhsjgd11 ай бұрын
    • On the stack, as shown.

      @CodingTutorialsAreGo@CodingTutorialsAreGo11 ай бұрын
    • @@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@nononnomonohjghdgdshrsrhsjgd11 ай бұрын
    • @@nononnomonohjghdgdshrsrhsjgd It's not. Local variable names are removed during compilation leaving only the positions on the stack.

      @CodingTutorialsAreGo@CodingTutorialsAreGo11 ай бұрын
  • 12:56

    @kiPROBRos@kiPROBRos3 жыл бұрын
  • 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?

    @hristoiliev7767@hristoiliev77672 жыл бұрын
    • 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@CodingTutorialsAreGo2 жыл бұрын
    • @@CodingTutorialsAreGo Perfect! Thank you very much! :)

      @hristoiliev7767@hristoiliev77672 жыл бұрын
  • 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.

    @mert.pinarbasi@mert.pinarbasi2 жыл бұрын
    • 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@CodingTutorialsAreGo2 жыл бұрын
    • @@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@mert.pinarbasi2 жыл бұрын
    • @@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.

      @CodingTutorialsAreGo@CodingTutorialsAreGo2 жыл бұрын
KZhead