Reference as a 'pointer' to a value type
-
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 -
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 RichTake 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.
-
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 RichHi, 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] }
-
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] }
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.
-
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.
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] }
-
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] }
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...:)
-
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...:)
Try this one, http://www.csharphelp.com/archives/archive77.html