1.

What is Unsafe Code in C#?

Answer»

Unsafe code in general is a keyword that denotes a code section that is not handled by the Common Language Runtime (CLR). Pointers are not supported by default in C# but unsafe keyword allows the use of the pointer variables.

However, extra care is required while handling unsafe code to PREVENT errors or security risks as pointers are complex and may LEAD to memory related errors such as stack overflow, overwritten system memory etc.

The following is an example:

using System; namespace PointerDemo {   class Example   {      STATIC unsafe void Main(STRING[] args)      {         char val = 'A';         char* PTR = &val;         Console.WriteLine("The character value is: {0} ", val);         Console.WriteLine("Address of the value is: {0}", (int)ptr);      }   } }

The output of the above program is as follows:

The character value is: A Address of the value is: 220412608


Discussion

No Comment Found