[转载]Using Pointers in C#
Using Pointers in C#信息来源:邪恶八进制信息安全团队([url]www.eviloctal.com[/url])
Back in the late 1980's and up to around 1997, most of my development work was done using the C programming language. Anyone who has done a fair amount of work in C or C++ would not have been able to avoid the use of pointers. Recently I had to re-visit some old code I had written in C and one of my first comments was 'wow pointers, I remember them!'
I have known since I started using C# (and that's been a few years) that you could use pointers, but to be honest, I have never had to use them and have never gone looking for a reason to use them. The main reasons I have written this article is to introduce the concept of pointers to any developers who have not seen them before, and as a little nostalgic trip back to the days when they were part of my daily coding diet!
So, what is a Pointer?
A pointer is nothing more than a variable that holds the address in memory of another variable. In C#, pointers can only be used on value types and arrays. As a structure is a value type, pointers can be used with them, but there is one caveat with this, the structure must not contain any reference types if you plan to use pointers. Any of the following may be a pointer:
Sbyte
byte
short
ushort
int
uint
long
ulong
char
float
double
decimal
bool
unsafe keyword:
Typically when we write code in C# by default it is what's known as safe code. Unsafe code allows you to manipulate memory directly, in the normal world of C# and the .NET Framework, this is seen as potentially dangerous, and as such you have to mark your code as unsafe. Using unsafe code, you are loosing garbage collection and whilst directly accessing memory, you have the possibility of accessing memory that you didn't want to and causing untold problems with your application. Unsafe code can only be used within a fully trusted assembly.
Now it is time to write some program code to demonstrate the use of pointers. Consider the following code.
static void Main(string[] args)
{
int age = 32;
Console.WriteLine("age = {0}", age);
}
A very simple piece of code that will print age = 32 on the console. Now let's introduce a pointer into the mix. When we declare a pointer, we have to declare it in a certain way, this being type* variable; the asterisk (dereferencer symbol) informs the compiler that we are declaring a pointer variable, the type says that we intend to use a pointer to store the address of type type. So using our simple piece of code above, let's creates an integer pointer to point to age.
static void Main(string[] args)
{
unsafe
{
int age = 32;
int* age_ptr;
Console.WriteLine("age = {0}", age);
}
}
Notice the use of the unsafe keyword, if we had not enclosed our code within unsafe, we would have received the following compilation error.
Pointers and fixed sized buffers may only be used in an unsafe context.
We could have declared the whole method as unsafe and this would have worked fine, for example:
unsafe static void Main(string[] args)
So, what can we do with our pointer? As stated at the beginning of the article, pointers hold addresses in memory to other variables, so we need to make age_ptr point to the memory location of age. To do this, we use the following line of code:
age_ptr = &age;
The ampersand '&' is the referencer and means the 'location of'. So if we now do the following in our code:
static void Main(string[] args)
{
unsafe
{
int age = 32;
int* age_ptr;
age_ptr = &age
Console.WriteLine(
页:
[1]
