Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Writing Unsafe code using C#

Rate me:
Please Sign up or sign in to vote.
4.77/5 (55 votes)
10 Oct 2001CPOL5 min read 236.1K   1.6K   60  
A simple tutorial that shows how to write unsafe code using C#
using System;

class MyClass {
	public static void Main() {
		TestClass Obj = new TestClass();
		unsafe { 
			int x = 10;
			int y = 20;
			Console.WriteLine("Before swap x = " + x + " y= " + y);
			Obj.swap(&x, &y);
			Console.WriteLine("After swap x = " + x + " y= " + y);
		}
	}
}

class TestClass {
	public unsafe void swap(int* p_x, int* p_y) {
		int temp = *p_x;
		*p_x = *p_y;
		*p_y = temp;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

Comments and Discussions