Pointers in C#

Pointer.cs

using System;
class un{
public static void Main()
{
un1 o=new un1();
o.meth();
}
}
class un1
{
unsafe public void meth()
{
int *i;
int j=103;
i=&j;
Console.WriteLine((int)i);
Console.WriteLine(j);
}}

Pointer1.cs

using System;
class zzz
{
public static void Main()
{
yyy a = new yyy();
a.abc();
}}
class yyy
{
unsafe public void abc()
{
int *i;
int j=1, k = 1;
//An & in front of any variable tells us where it starts in memory
i = &j;
//pointing to j
Console.WriteLine((int)i);
*i = 10;
//changing value of j thru this
Console.WriteLine(j);
i = &k;
Console.WriteLine((int)i);
*i = 100;
Console.WriteLine(k + ” ” + j);
}}

Related posts :

  • Unsafe code in C#
  • Read a file in C#
  • Leave a Comment