C# Program to Swap Two Numbers Using the Pointer

This article is a very important article. I mean in this C# Program to Swap Two Numbers Using the Pointer we are going to know very clearly about this. We hope you like this article. Please comment if you liked this article.

C# Program to Swap Two Numbers Using the Pointer

C# Program to Swap Two Numbers Using the Pointer

//C# program to swap two numbers using the pointer.

using System;

class UnsafeEx
{
    static unsafe void Swap(int* X, int* Y)
    {
        int Z = 0;

        Z  = *X;
        *X = *Y;
        *Y = Z;
    }
    static unsafe void Main(string[] args)
    {
        int A = 10;
        int B = 20;

        Console.WriteLine("Before Swapping:");
        Console.WriteLine("\tA: " + A);
        Console.WriteLine("\tB: " + B);

        Swap(&A, &B);

        Console.WriteLine("After Swapping:");
        Console.WriteLine("\tA: " + A);
        Console.WriteLine("\tB: " + B);
    }
}
Before Swapping:
        A: 10
        B: 20
After Swapping:
        A: 20
        B: 10
Press any key to continue . . .

Final Words

C# Program to Swap Two Numbers Using the Pointer We have clearly identified the information you need through the article. If in doubt please let us know your doubts via the comment box Thank you.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment