CSharp fixed keyword
Since I’ve already mentioned my bias against using unsafe mode in this post:
I’ll skip my normal rant about that. Just suffice it to say they don’t call it “unsafe” for nothing.
So assuming you have some valid use for using unsafe mode, you’ll also need to make sure your memory isn’t moving around on you while you are accessing the pointers you’ve created in your unsafe block. For that, you’ll need the fixed keyword.
While the unsafe mode gives you the ability to use pointers in the first place, fixed makes sure the pointers aren’t moving around while you are using them. We need both so that we only fix the memory in its location while we are using it while still being able to write normal code within our unsafe block.
To create a pointer to a variable, you use the asterisk (*). To retrieve the address of a variable (so you can assign it to the variable) you use the ampersand (&). Just like we did in C++ for those of you who are familiar with that language.
So to create a pointer to an integer, your code might look something like this:
public unsafe void Foo() { int i = 20; int* pi = &i; }
At this point, pi points to i, which holds the value of 20.
But if we want to use pi to retrieve the value of 20, we want to make sure that the memory location of i doesn’t move while we are retrieving it.
Arguably, this is much more important when we are trying to access arrays in a loop. But to keep the illustration simple we’ll continue with retrieving our single integer value.
public unsafe void Foo() { int i = 20; int copyOfI; fixed (int* pi = &i) { copyOfI = *pi; } }
Now we can be sure that every time we access pi, it will still be pointing to the variable i.
Other post in Advanced CSharp
- Two Interfaces. Same Method. Two meanings. - September 29th, 2008
- Readonly variables in CSharp? Really?! - October 29th, 2008
- Dispose with Using - November 10th, 2008
- Delegates in .NET - December 4th, 2008
- Using Sealed in CSharp - December 8th, 2008
- CSharp checked and unchecked - December 11th, 2008
- Advanced CSharp - unsafe mode - December 15th, 2008
- Volatile variables and CSharp threads - December 22nd, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- CSharp fixed keyword - January 5th, 2009
- using - There's more there than you are using - February 2nd, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- CSharp ?? Operator - May 18th, 2009
- Using VB.NET From CSharp - July 1st, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
- Transaction Tracking Typed Datasets Using SqlTransaction - July 20th, 2010
- CSharp's Property Shortcuts - July 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Advanced CSharp - yield - November 27th, 2012
- Making values nullable - December 18th, 2012
- Stackalloc in CSharp - January 22nd, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
Related Post
One Pingback/Trackback
- 06 January 2009 at 9:01am
- Dew Drop - January 6, 2009 | Alvin Ashcraft's Morning Dew
[...] CSharp fixed Keyword (Dave M. Bush) ...





Pingback: Dew Drop - January 6, 2009 | Alvin Ashcraft's Morning Dew