Tutorial by Examples

C# allows using pointer variables in a function of code block when it is marked by the unsafe modifier. The unsafe code or the unmanaged code is a code block that uses a pointer variable. A pointer is a variable whose value is the address of another variable i.e., the direct address of the memory l...
You can retrieve the data stored at the located referenced by the pointer variable, using the ToString() method. The following example demonstrates this: using System; namespace UnsafeCodeApplication { class Program { public static void Main() { unsafe { ...
You can pass a pointer variable to a method as parameter. The following example illustrates this: using System; namespace UnsafeCodeApplication { class TestPointer { public unsafe void swap(int* p, int *q) { int temp = *p; *p = *q; *q = temp; ...
In C#, an array name and a pointer to a data type same as the array data, are not the same variable type. For example, int *p and int[] p, are not same type. You can increment the pointer variable p because it is not fixed in memory but an array address is fixed in memory, and you can't increment th...
For compiling unsafe code, you have to specify the /unsafe command-line switch with command-line compiler. For example, to compile a program named prog1.cs containing unsafe code, from command line, give the command: csc /unsafe prog1.cs If you are using Visual Studio IDE then you need to enabl...

Page 1 of 1