Unsafe Code in C#

22 maart 2021 om 10:00 by ParTech Media - Post a comment

Most of us know that in C#, the code is run under the control of the Common Language Runtime (CLR) of the .NET frameworks. You must be wondering what is Common Language Runtime in the .NET framework? It is nothing but the virtual machine component or the very basic component of the .NET framework).

In the .NET framework, CLR is the run-time environment that helps in running the code and also offers a variety of services to make the development process easy. All the programs that run in C# are controlled by the CLR while the parts of the program which are written using the unsafe code are beyond the control of CLR of the .NET frameworks. The article explains what is unsafe code in C#.

Table of content

  1. What is Unsafe Code in C#?
  2. When to implement unsafe code in C#
  3. What is a pointer?
  4. How to run unsafe code in C#?
  5. Example of unsafe code
  6. Advantages of unsafe code
  7. Disadvantages of unsafe Code
  8. Conclusion

What is Unsafe Code in C#?

Unsafe Code is also known as Unmanaged Code. In C#, the activities like managing stacks, allocation, and release of memory, etc. are taken care of by the CLR. This ensures that the programmer does not have to worry about them. But there are also cases when the programmer wishes to take control of these activities. He can instruct the compiler that the management of the code will be done by him/her whenever he/she uses the keyword “unsafe”.

The unsafe code written in the program brings stability to the program. But it also creates multiple security risks which are caused due to absence of checks related to the boundaries of arrays. This will often result in memory-related errors. Class, methods, types, struct, and code blocks are the various sub-programs that can be made unsafe by the programmer.

Following is the general syntax of Unsafe Code in C#:-

unsafe Context_declaration

When to implement unsafe code in C#?

  • Whenever pointers are to be implemented in the program.
  • When using native methods.
  • When a code is written that interfaces with some unmanaged code or you want the operating system requirements to be unsafe.
  • During the implementation of a time-critical algorithm.
  • While accessing a memory-mapped device.

What is a pointer?

A pointer variable is used in the code block while writing unsafe or the unmanaged code in C#. The pointer variable has the address of another variable. This means that the pointer variable is used whenever there is a necessity to store the memory location or the address of another variable. It should be noted that the data type of the pointer variable and that of the variable to which it points should be the same. So, to indicate the address of an integer data type pointer variable should also have an integer data type.

The following operators are used while using pointers in the program:-

  • &
  • ->

The general syntax for pointer declaration is as given below:-

Type *var-name; 

Here, the usage of the operator “*” is done to create the pointer variable.

How to run unsafe code in C#?

Following are the steps to be followed to enable the usage of Unsafe Code in Visual Studio IDE:-

  1. Go to Solution Explorer and double-click on properties. This will open the project properties on the screen.
  2. Select ‘Allow Unsafe Code’ from the Build Tab.

Following the above steps will enable the usage of Unsafe code in the program on Visual Studio IDE.

Example of unsafe code

The following program shows the usage of pointers in C# with the usage of unsafe modifier:-

Output:-

Advantages of unsafe code

  • Unsafe code increases stability and the performance of the program
  • Fixed buffers are used in unsafe code. Without any managed overhead the raw memory can be read and written by the programmer by using fixed buffers.
  • Unsafe code provides a medium to interface with the memory

Disadvantages of unsafe code

  • Since unsafe code creates security risks, responsibility at the programmer’s end increases.
  • Making use of unsafe code can lead to errors that might occur as type checking is bypassed.
  • Pointers arithmetic is not supported in C#. As the code is unmanaged, the CLR which usually maintains the safety and security of the code will not be able to manage the code, hence bypassing the security. Thus, the object access rules are not followed strictly in the unsafe code hence reducing its safety even further.

Conclusion

In this post, we have seen what is unsafe code and how to use it in your program. Though unsafe code has its fair share of advantages, its benefits cannot be ignored especially when you want to improve the stability of the program and want to take control of it.