Question How to use pointers?

joenhel

New member
Joined
Apr 12, 2013
Messages
2
Programming Experience
Beginner
Hi guys, i'm still a newbie here, i just want to ask how can i utilize pointers in c#? thanks for the big help, much appreciated.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,196
Location
Sydney, Australia
Programming Experience
10+
In C#, code that uses pointers is called "unsafe code" because you don't have the safety of the system ensuring that you never modify memory that you shouldn't. In order to use pointers, you must first allow unsafe code in your project, which you do by checking the appropriate box on the Build page of the project properties. Next, you must define an unsafe block in your code. You can do that in two ways:

1. Define a block within a method in which unsafe code is allowed, e.g.
private void SomeMethod()
{
    // no pointers allowed here

    unsafe
    {
        // pointers allowed here
    }

    // no pointers allowed here
}
2. Define that unsafe code is allowed within the entire body of a method, e.g.
unsafe private void SomeMethod()
{
    // pointers allowed here
}
If a method needs to have parameters of pointer types then you must use option 2.

For further reading on how to actually use pointers once they're allowed, this is a good place to start and you can follow the appropriate links:

unsafe (C# Reference)
 
Top Bottom