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. Reference as a 'pointer' to a value type

Reference as a 'pointer' to a value type

Scheduled Pinned Locked Moved C#
csharpperformancequestionlearning
7 Posts 4 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.
  • K Offline
    K Offline
    kildareflare
    wrote on last edited by
    #1

    Hello, im a C# newbie and im having trouble with the transition from C. In particular with using references in place of pointers. I have a book called Professional C# but it does not seem to cover what I want to know. Namely, I want to assign a reference to a bool value type, such that I can use it to change the value; or so that I can pass the memory location as a parameter to a method, such that the method will be working on the value the reference points to. My current understanding is that: //creates bool value type and assigns value bool symbol = true; //creates reference to bool type bool pointer; pointer = symbol; //assign reference to symbol? Thanks Rich

    B L 2 Replies Last reply
    0
    • K kildareflare

      Hello, im a C# newbie and im having trouble with the transition from C. In particular with using references in place of pointers. I have a book called Professional C# but it does not seem to cover what I want to know. Namely, I want to assign a reference to a bool value type, such that I can use it to change the value; or so that I can pass the memory location as a parameter to a method, such that the method will be working on the value the reference points to. My current understanding is that: //creates bool value type and assigns value bool symbol = true; //creates reference to bool type bool pointer; pointer = symbol; //assign reference to symbol? Thanks Rich

      B Offline
      B Offline
      Bert delaVega
      wrote on last edited by
      #2

      Take a look at "unsafe" code is C#. You have to use the unsafe keyword for the code block, method or class where you manipulate your pointers. Also, the project needs to have the unsafe flag set in the project properties or command line compile.

      1 Reply Last reply
      0
      • K kildareflare

        Hello, im a C# newbie and im having trouble with the transition from C. In particular with using references in place of pointers. I have a book called Professional C# but it does not seem to cover what I want to know. Namely, I want to assign a reference to a bool value type, such that I can use it to change the value; or so that I can pass the memory location as a parameter to a method, such that the method will be working on the value the reference points to. My current understanding is that: //creates bool value type and assigns value bool symbol = true; //creates reference to bool type bool pointer; pointer = symbol; //assign reference to symbol? Thanks Rich

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, first of all, be careful, not all replies are correct or even to-the-point your example does not contain references or pointers; calling a variable "pointer" does not turn it into a pointer ! The CLR (that is the system underneath several languages including C#) lets you work with "value types" (such as int and bool, but also struct) and "reference types" (such as Form and Button). A local value type (one declared inside a method) is stored on the stack. If you pass it as a parameter to another method, it gets copied (or at least behaves as if it were copied), so that method cannot modify your variable. Things change when you add the "ref" keyword to your parameter list, both for caller and callee. Now you are really passing a pointer, and the callee can modify the caller's variable. If you're familiar with C, it is like adding a * at the caller, and a & at the callee (but then all the code of the callee needs additional *, not so in C#). A reference type is different, it IS a pointer to an object, so when you pass it to some method, that method can do whatever it chooses to do to your object. Conclusion: if you have bool symbol=true; and you want to call a method such that it could change symbol, then do: someMethod(ref symbol); May I suggest you buy a book on C# and work your way through it. I am convinced you need to have a reference book at hand at all times when starting to use a new language. Good luck!

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        K 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, first of all, be careful, not all replies are correct or even to-the-point your example does not contain references or pointers; calling a variable "pointer" does not turn it into a pointer ! The CLR (that is the system underneath several languages including C#) lets you work with "value types" (such as int and bool, but also struct) and "reference types" (such as Form and Button). A local value type (one declared inside a method) is stored on the stack. If you pass it as a parameter to another method, it gets copied (or at least behaves as if it were copied), so that method cannot modify your variable. Things change when you add the "ref" keyword to your parameter list, both for caller and callee. Now you are really passing a pointer, and the callee can modify the caller's variable. If you're familiar with C, it is like adding a * at the caller, and a & at the callee (but then all the code of the callee needs additional *, not so in C#). A reference type is different, it IS a pointer to an object, so when you pass it to some method, that method can do whatever it chooses to do to your object. Conclusion: if you have bool symbol=true; and you want to call a method such that it could change symbol, then do: someMethod(ref symbol); May I suggest you buy a book on C# and work your way through it. I am convinced you need to have a reference book at hand at all times when starting to use a new language. Good luck!

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          K Offline
          K Offline
          kildareflare
          wrote on last edited by
          #4

          Luc, Most helpful, I do have a book on C# and had come across the ref keyword. However I was only using it in the member function declaration and not when I invoked it. I think it did not help that i've jumped in at the deep end with my first app - could have chosen something a bit easier, I am learning quite quickly though! Cheers all Rich.

          L 1 Reply Last reply
          0
          • K kildareflare

            Luc, Most helpful, I do have a book on C# and had come across the ref keyword. However I was only using it in the member function declaration and not when I invoked it. I think it did not help that i've jumped in at the deep end with my first app - could have chosen something a bit easier, I am learning quite quickly though! Cheers all Rich.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            You're welcome. I still recommend you work your way through the book you have, possibly skipping those specialized chapters that don't interest you yet. You really must grasp all the implications of value and reference types before you can do any serious work. :)

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            K 1 Reply Last reply
            0
            • L Luc Pattyn

              You're welcome. I still recommend you work your way through the book you have, possibly skipping those specialized chapters that don't interest you yet. You really must grasp all the implications of value and reference types before you can do any serious work. :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              K Offline
              K Offline
              kildareflare
              wrote on last edited by
              #6

              Well, I thought I did, but obviously not. I've been trying to do run through the book and code the app at the same time. Guess ill hit the book a bit more...:)

              C 1 Reply Last reply
              0
              • K kildareflare

                Well, I thought I did, but obviously not. I've been trying to do run through the book and code the app at the same time. Guess ill hit the book a bit more...:)

                C Offline
                C Offline
                Cfer83
                wrote on last edited by
                #7

                Try this one, http://www.csharphelp.com/archives/archive77.html

                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