Theunsafe code or the unmanaged code is a code block that uses apointer variable. Apointer 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:
·Openproject propertiesby double clicking the properties node in the Solution Explorer.
·Click on theBuildtab.
·Select the option "Allow unsafe code".
For example,
using System; namespace UnsafeCodeEx { classTestPointer { publicunsafestaticvoid Main() { TestPointer p = newTestPointer(); int var1 = 10; int var2 = 20; int* x = &var1; int* y = &var2; p.DemoMultiplication(x, y); Console.ReadKey(); } publicunsafevoid DemoMultiplication(int* p, int* q) { int result = *p + *q; Console.WriteLine("Multiplication is: " + result); } } }
Output:
Similar way, you can write more program related to unsafe code .
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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".
For example,
Output:
Similar way, you can write more program related to unsafe code .