C# pointers Tutorial with Examples

C# pointers Tutorial with Examples

Today, We want to share with you C# pointers Tutorial with Examples.
In this post we will show you Basics Of Pointers In C#, hear for how to use pointer in c# we will give you demo and example for implement.
In this post, we will learn about Pointers on pointers – C# tutorial with an example.

Introduction: Pointers In C#

In this post, we will learn about  Pointers In C# with example.

Now in this post, I will explain about Pointers In C# with appropriate example.A pointer is simply a type of variable whose value is the address of another variable. Pointers Variable cannot be used directly.

READ :  Trigger button click on enter key JavaScript

To use a pointer, we need to define the block of code for it. The ‘unsafe’ keyword defines the scope to use pointer variables.Pointer variables can only be used inside an unsafe code block in the program.

how to use pointer in c#

For example,
int* num; // 'num' is a pointer to an Integer
char* b; // 'b' is a pointer to an Char
float* pi; // 'pi' is a pointer to an Float

Now, create Console Application in Visual Studio and write below line of code in it.

using System;  
  
namespace Infinityknow
{  
    class Program  
    {  
        static unsafe void Main(string[] args)  
        {  
            // variable 'p1' of int type is initialised with value 100  
            int p1 = 50;  
  
            // pointer variable 'q1' contains the address of variable 'p1' and, '&' used to obtain address of a variable  
            int* q1 = &p1;  
  
            Console.WriteLine("Value: " + p1);  
            Console.WriteLine("Address: " + (int)q1);  
            Console.ReadKey();  
        }  
    }  
  
}  

Address: 122743465
Note
  1. Use of unsafe code may increase security risks.
  2. we have to make a compromise with the automatic garbage collector while using pointer.

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Pointers In C# – C# Tutorials.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Add a Comment

Your email address will not be published. Required fields are marked *