Advanced CSharp – unsafe mode
One of the “advantages” of using CSharp instead of VB.NET is that if programmers want to, they have the option of bypassing the memory management of .NET and working with memory directly. This is called “unsafe” mode.
While I will show you how to use this keyword, I have to tell you up front that I’ve been using CSharp since Beta 2 of .NET 1.0 and I’ve NEVER needed to switch into unsafe mode to do any of the work that I’ve done.
I’ve even written code that bridged down to some unmanaged C++ code and still have not needed to use unsafe mode.
One of the main advantages of using .NET to begin with is the fact that .NET manages our memory for us. I’ve worked on far too many C++ programs that leaked memory due to their complexity and bad architecture to purposely go back to that model.
Yes, it is true that you might get a slight performance improvement by bypassing the memory management and working with the memory directly. But is that slight improvement worth the risk of all of the issues that arise when using memory directly?
If you find that you need to use unsafe mode, I would recommend that you consider writing that part of your code in a language that was designed for that level of coding. Assembler or C++ are some good choices.
If these are not options, here’s where the unsafe keyword comes in.
Any code you wrap in the unsafe keyword:
unsafe { // this code is unmanaged }
becomes unmanaged. You will also need to add the /unsafe compiler switch to your compiler options.
You can also make an entire method unsafe by adding the keyword to the method declaration:
public unsafe void Foo(int *i) { // i is a pointer that is unsafe }
In a future post, we’ll look at some ways of using pointers in CSharp code.
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
- Dispose, Finalize and SuppressFinalize - July 9th, 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
Related Post
2 Pingbacks/Trackbacks
- 16 December 2008 at 10:12am
- Dew Drop - December 16, 2008 | Alvin Ashcraft's Morning Dew 05 January 2009 at 9:01am
- CSharp fixed keyword
[...] Advanced C# - Unsafe Mode (Dave M. Bush) ...
[...] Advanced CSharp - unsafe mode ...




Pingback: Dew Drop - December 16, 2008 | Alvin Ashcraft's Morning Dew
Pingback: CSharp fixed keyword