What is the unsafe keyword?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

What is the unsafe keyword?

Rating:

The unsafe keyword is used in case of pointers in C#. The unsafe keyword denotes an unsafe code or block, which is required for any operation involving pointers. The unsafe keyword is applied as a modifier in the declaration of callable members such as methods, properties, and constructors (but not static constructors), as given below:

static unsafe void UnsafeTestFunc(string str, int count) //unsafe method
{
//unsafe code: can use pointers here
}
unsafe
{
   //unsafe block
}

The following code snippet displays the use of the unsafe keyword:

using System;
class UnsafeTestCode
{
    unsafe public static void Main()
   {
       int count = 99098;
       int* pointer; //create an int pointer.
       pointer = &count; //put address of count into pointer.
       Console.WriteLine( "Initial value of count is " + *pointer);
       *pointer = 10; //assign 10 to count via pointer.
       Console.WriteLine( "New value of count is " + *pointer);
       Console.ReadLine();
   }
}


Rating:



Other articles

Click here to Article home

Microsoft Certification MCSE: MCSA , MCTS, MCDST, MCAD, MCDBA, MCSE Messaging, MCSE Security
JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.
 
HACKER SAFE certified sites prevent over 99.9% of hacker crime.