Use Of Pointers In C#

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.

Console application

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.

program

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace consolepointer  
  7. {  
  8.     class Program  
  9.     {  
  10.   
  11.        unsafe static void Main(string[] args)  
  12.         {  
  13.             int number = 200;  
  14.             int* merapointer;  
  15.   
  16.             merapointer = &number;  
  17.   
  18.               
  19.             Console.WriteLine("Value of Number:" + number);  
  20.            Console.WriteLine("Value of MeraPointer:"+(int)merapointer);  
  21.             
  22.            Console.ReadKey();  
  23.         }  
  24.     }  
  25. }  
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.

Press Run

To skip type safety, Go to your Project - Right Click Properties, Build, then Allow unsafe Code.

properties

build

Now run your project and you will get your Solution in Console.

Output

run

Dereferencing Pointer
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace consolepointer  
  7. {  
  8.     class Program  
  9.     {  
  10.   
  11.        unsafe static void Main(string[] args)  
  12.         {  
  13.             int number = 200;  
  14.             int* merapointer;  
  15.   
  16.             merapointer = &number;  
  17.   
  18.             Console.WriteLine("Printing OLD VALUES");  
  19.             Console.WriteLine("-----------------------------");  
  20.             Console.WriteLine("Value of Number:" + number);  
  21.             Console.WriteLine("Value of MeraPointer:" + (int)merapointer);  
  22.             Console.WriteLine("*MeraPointer:" + *merapointer);  
  23.             *merapointer = 100;  
  24.   
  25.   
  26.             Console.WriteLine("------------------------------");  
  27.   
  28.   
  29.             Console.WriteLine("Printing NEW VALUES");  
  30.             Console.WriteLine("------------------------------");  
  31.             Console.WriteLine("Number:" + number);  
  32.             Console.WriteLine("MeraPointer:" + (int)merapointer);  
  33.             Console.WriteLine("*MeraPointer:" + *merapointer);  
  34.             Console.ReadKey();  
  35.         }  
  36.     }  
  37. }  
Output

Output

Hope you liked this. Thank you for reading. Have a good day.

 

Up Next
    Ebook Download
    View all
    Learn
    View all