.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Swapping of two numbers in C# using pointer

Posted By:Abhisek Panda       Posted Date: November 14, 2009    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

See the below program for swapping of two numbers using pointer in C#.

class Swapping

//declaring pointers inside unsafe code
unsafe public static void UnsafeSwap(int* i, int* j)
int temp = *i;
*i = *j;
*j = temp;

static void Main(string args[])

// Values for swapping.

int i = 10, j = 20;
// Swap values using pointer

Console.WriteLine("Values before swap: i = {0}, j = {1}", i, j);

unsafe //passing the values to unsafe code
UnsafeSwap(&i, &j);

Console.WriteLine("Values after swap: i = {0}, j = {1}", i, j);

Values before swap: i = 10, j = 20
Values after swap: i = 20, j = 10


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend