Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stack and Heap

Stack and Heap

Scheduled Pinned Locked Moved C#
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11127370
    wrote on last edited by
    #1

    HI, My question is,What is Stack and Heap in C#??? As i search in net also,but i didn't understand it clearly? So,can anyone explain me with simple example? Thanks...

    L OriginalGriffO 2 Replies Last reply
    0
    • U User 11127370

      HI, My question is,What is Stack and Heap in C#??? As i search in net also,but i didn't understand it clearly? So,can anyone explain me with simple example? Thanks...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Member 11161625 wrote:

      What is Stack and Heap in C#?

      The same as in any other language; see Differences between Stack and Heap[^].

      1 Reply Last reply
      0
      • U User 11127370

        HI, My question is,What is Stack and Heap in C#??? As i search in net also,but i didn't understand it clearly? So,can anyone explain me with simple example? Thanks...

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Not really, no... it's a complicated subject. The first thing you need to get sorted is the difference between a variable, a reference, and an instance - because without that, none of the rest of this will make any sense. An instance is an example of a class. Thinking about cars, an instance is a car you can physically get into and drive - it is distinct from all other cars in that it will have a different registration / licence number, may be a different colour, may have a different owner. Each physical car is a different instance of a generic subject "A Car". A reference is a "pointer" to an instance: "my car" is a reference to a physical vehicle, "your car" is a reference to a different one. "This car" could refer to the same vehicle as "my car" now, and to "your car" in a moment as we physically move from one to another. Clearly, you can have many references to the same instance! A variable is a named "place" that holds a value - this could be a number like 12 or 14, or a reference to an instance. So when we say:

        Car myCar = new Car("Mercedes", "A160", Color.Red);

        We declare a variable called myCar which holds a reference to an instance which is created via the new keyword. A variable doesn't have to hold a reference: it can hold a value instead:

        int i = 666;

        We create a variable called i which contains the value 666 This is important, because we have different names for each type of variable: we call the first one a reference type and the second a value type and how they behave is different. -- We'll get to stack and heap soon, honest! -- When you copy a variable, it's important to know if it's a reference type or value type, because they seem to behave very differently:

        int i = 666;
        int j = i;
        i = i + 1;
        Console.WriteLine("{0}:{1}", i, j);

        You expect to get "667:666" because if you didn't, then doing any kind of math would get very difficult! And you do: i and j are value types, so the value in i is copied into j, and then incremented - that doesn't affect the value in j because it's a copy of the value. If you think about variables as "pockets" it makes sense: you have five pennies in your right trouser pocket, and you copy the value to your left trouser pocket - you now have five coins in each pock

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups