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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Var Scope

Var Scope

Scheduled Pinned Locked Moved C#
questioncsharp
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.
  • A Offline
    A Offline
    Ashraf zia
    wrote on last edited by
    #1

    Hello there to all: I m new to C#. I have a question that what's the difference between a variable scope and a variable life time? Plz give me a detailed answer to clear my concept. Hope to hear!

    R G 2 Replies Last reply
    0
    • A Ashraf zia

      Hello there to all: I m new to C#. I have a question that what's the difference between a variable scope and a variable life time? Plz give me a detailed answer to clear my concept. Hope to hear!

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Pretty much the same thing. The variable's lifetime is from when it is created (strictly I believe its when the block containing the variable definition is opened) to when it goes 'out of scope'. When this happens, the stack is unwound and all the variables and references get wiped out. The scope is usually the area within the innermost set of braces { } which contain the variable. Regards, Rob Philpott.

      1 Reply Last reply
      0
      • A Ashraf zia

        Hello there to all: I m new to C#. I have a question that what's the difference between a variable scope and a variable life time? Plz give me a detailed answer to clear my concept. Hope to hear!

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        For value types the lifetime is the entire method where it's declared, but the scope might be smaller, for instance inside a loop. Outside the scope, the room allocated on the stack for the variable is simply unused. For reference types the lifetime is from when the object is created until it's garbage collected. When all references to the object go out of scope (or rather the lifetime ends, I believe), it can be garbage collected. --- b { font-weight: normal; }

        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