---
title: "What is unsafe code (code using pointers) in c#?"  
description: "What is unsafe code (code using pointers) in c#?"  
author: "Manoj Bhatt"  
published: 2016-01-11  
updated: 2016-01-12  
canonical: https://www.mindstick.com/forum/33851/what-is-unsafe-code-code-using-pointers-in-c-sharp  
category: "c#"  
tags: ["c#", ".net", "performance", ".net assembly"]  
reading_time: 2 minutes  

---

# What is unsafe code (code using pointers) in c#?

I'm interested how to use of [unsafe](https://www.mindstick.com/blog/115/unsafe-code-in-c-sharp) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) [options](https://www.mindstick.com/articles/43878/making-the-best-use-of-the-options-trade-ideas) in C#? And when? Please give me appropriate answer. Thank you to all in advance.

## Replies

### Reply by Anupam Mishra

The **unsafe code** or the unmanaged code is a code block that uses a pointer variable. A pointer is a variable whose value is the address of another variable i.e., the direct address of the memory location. Similar to any variable or constant, you must declare a pointer before you can use it to store any variable address.

If you are using Visual Studio IDE then you need to enable use of unsafe code in the project properties.

To do this:

· Open project properties by double clicking the properties node in the Solution Explorer.

· Click on the Build tab.

· Select the option "Allow unsafe code".

![What is unsafe code (code using pointers) in c#?](https://www.mindstick.com/mindstickforums/9c1145a0-a771-4f5a-924c-20a634a3485d/images/4ea1284f-9ca7-43aa-9203-e0e8320bdffc.png)\

**For example,**

```
using System;namespace UnsafeCodeEx{    class TestPointer    {        public unsafe static void Main()        {            TestPointer p = new TestPointer();            int var1 = 10;            int var2 = 20;            int* x = &var1;            int* y = &var2;            p.DemoMultiplication(x, y);            Console.ReadKey();        }        public unsafe void DemoMultiplication(int* p, int* q)        {            int result = *p + *q;             Console.WriteLine("Multiplication is: " + result);        }    }}
```

## Output:

![What is unsafe code (code using pointers) in c#?](https://www.mindstick.com/mindstickforums/9c1145a0-a771-4f5a-924c-20a634a3485d/images/a6d96b26-d12a-4178-85b9-796c137bb7c7.png)\

Similar way, you can write more program related to unsafe code .

\


---

Original Source: https://www.mindstick.com/forum/33851/what-is-unsafe-code-code-using-pointers-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
