Pointers are widely used for fast execution of a program as pointer allow you to easily write low level code. Simply we can say, A Pointer is a memory variable that holds up the address of another variable. In spite of such great thing poses by pointer it is not supported in C# inherently, however they can be used in unsafe code- the code that is marked as unsafe.
We can mark a piece of our program code to unsafe by creating a block using unsafe keyword and putting that code inside the unsafe block. A piece of program code is considered to be type safe if it is accessing other piece of code using only interfaces. Therefore if your program contains some code that has been marked as unsafe, then C# compiler does not compile your program code until you explicitly tell the compiler to skip the type safety checking.
How you gonna do this
Well one way to skip the type safety checking is to use the C# command line compiler utility that is – csc.exe, to compile the C# source code and specify the unsafe keyword while computing the C# source code.
Creating Pointer in Console
Step 1: Open Visual Studio. By pressing Ctrl +Shift + N you will get your “New Project” Window.
Step 2: After pressing OK you will get into your Coding Part where you will see three files in Solution Explorer [Properties, References, Program.cs], in which Program.cs file is your main file where you embed all your Inheritance program code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace consolepointer
- {
- class Program
- {
-
- unsafe static void Main(string[] args)
- {
- int number = 200;
- int* merapointer;
-
- merapointer = &number;
-
-
- Console.WriteLine("Value of Number:" + number);
- Console.WriteLine("Value of MeraPointer:"+(int)merapointer);
-
- Console.ReadKey();
- }
- }
- }
Press Run and you will get an error, since you haven’t told compiler to skip the type safety as you have used Unsafe Keyword.
To skip type safety, Go to your Project - Right Click Properties, Build, then
Allow unsafe Code.
Now run your project and you will get your Solution in Console.
Output Dereferencing Pointer - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace consolepointer
- {
- class Program
- {
-
- unsafe static void Main(string[] args)
- {
- int number = 200;
- int* merapointer;
-
- merapointer = &number;
-
- Console.WriteLine("Printing OLD VALUES");
- Console.WriteLine("-----------------------------");
- Console.WriteLine("Value of Number:" + number);
- Console.WriteLine("Value of MeraPointer:" + (int)merapointer);
- Console.WriteLine("*MeraPointer:" + *merapointer);
- *merapointer = 100;
-
-
- Console.WriteLine("------------------------------");
-
-
- Console.WriteLine("Printing NEW VALUES");
- Console.WriteLine("------------------------------");
- Console.WriteLine("Number:" + number);
- Console.WriteLine("MeraPointer:" + (int)merapointer);
- Console.WriteLine("*MeraPointer:" + *merapointer);
- Console.ReadKey();
- }
- }
- }
Output Hope you liked this. Thank you for reading. Have a good day.