In Visual Studio, you create a new project and you want to use pointers in the project, Probably visual studio 2017 will give an invalid pointer error.
We need to open unsafe mode in the project because the pointers are unsafe codes. In Studio Visual Studio we will look at the pointer with a simple example.
Step 1: Let’s start Visual Studio 2017 and open a console application(you can also open wpf or winform).
Step 2: Right click on the project name and click properties in the solution explorer.
Step 3: Click Build tab and check “Allow unsafe code”
Step 4: A Simple Pointer Example in C#
Add the unsafe keyword to Main method or Program class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
unsafe class Program { static void Main(string[] args) { int number = 10; int* numberPointer; numberPointer = &number; Console.WriteLine("Your number is"+*numberPointer); Console.ReadKey(); } } |