Click here to Skip to main content
15,886,077 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.2K   1.6K   60  
A simple tutorial that shows how to write unsafe code using C#
using System;

struct Point {
	public int iX;
	public int iY;
}

class MyClass {
	public unsafe static void Main() {

		// reference of point
		Point refPoint = new Point();
		refPoint.iX = 10;
		refPoint.iY = 20;

		// Pointer of point
		Point* pPoint = &refPoint;

		Console.WriteLine("X = " + pPoint->iX);
		Console.WriteLine("Y = " + pPoint->iY);

		Console.WriteLine("X = " + (*pPoint).iX);
		Console.WriteLine("Y = " + (*pPoint).iY);

	}
}

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