SIGN UP MEMBER LOGIN:    
ARTICLE

Unsafe Code in C#

Posted by Vineet Kumar Saini Articles | C# Language December 06, 2011
In this article you will see the use of the 'unsafe' keyword in C#.
Reader Level:

As you know, a Pointer is a variable which holds the address of another variable. If we want to use a pointer in C# then we use the  "unsafe" keyword for that purpose. Unsafe code is code which does not executed under the full management of common language runtime (CLR). It can cause some problems therefore each use must be marked as "unsafe". Unsafe code is also known as unmanaged code because it is not execute under the full management of CLR. There are two types of code in C#, Which are as follows:

  • Manages code
  • Unmanaged code

Managed Code : The code which is executed under the full management of CLR, is called managed code. Managed Code include programmers convenience and enhanced security .

Unmanaged code : The code which does not executed under the full management of common language runtime (CLR), is called unmanaged code. We can say unmanaged code is directly executed by the computer's CPU.

To set the compiler option in the Visual Studio development environment

image1.gif

OR

image2.gif

When we run the the program then there will be an error corresponding to unsafe code. For remove this error we go to application properties.

image3.gif

Now we will go to build option and check the Allow unsafe code.

image4.gif

Run application (Press F5).

image5.gif

To set compiler option programmatically

First of all we write the program on notepad then save in any drive with name "unsafe.cs".

When we compile this program then we write "csc/unsafe unsafe.cs".

image6.gif

Summary

So unsafe code is unmanaged code i.e. which does not execute under full management of CLR. We can say that, when we want use of pointer in C# then we will use unsafe keyword.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor