Asp.net Declare a Pointer

Type * var-name ;type  is the pointer’s referent type.var-name  is the name of the pointer variable.example. To declare ip  to be a pointer as like  int.
 int* ip;
If you come from a C/C++ background, then you need to be aware of an important difference between the way C# and C/C++ declare pointers in asp.net programming .


Example of Declare a Pointer:

using System;
class myclass
{
    unsafe static void Inc(int* p)
    {
        //++
        *p=*p+1;
    }

    public static void Main()
    {
        int i = 1;
        unsafe
        {
            Inc(&i);
        }
        //print the i
        Console.WriteLine(i);
    }
}
  in this example we make a class 'myclass' and a function inc which takes one argument p which is a pointer type .in this function we increasing value of p by arthritic operation.
by doing the operation on pointer.
using System is a name space .public static void main is a main function where the program start execution.
When program  run then, you'll see 2 printed on screen. because you have passed the address of the 
variable i to the function Increment. Variable i is create on the stack. &i gives  address on the stack. Thus when within the function Inc, p is pointing to i's address. Thus when we increment by   1 to *p(pointer p) we are really increment i;

Second Example :

using System;class pointerclass
{public  void Method()
{
{int x = 10;   //x is a variableint y = 20;    //declare y as a variable int *ptr = &x; //ptr is a pointer variable int *prt = &y; //prt same as ptr.
Console.WriteLine(ptr);
Console.WriteLine(

Console.WriteLine(prt);
Console.WriteLine(*ptr);
Console.WriteLine(*prt);
}
}

Console.WriteLine(*ptr);
Console.WriteLine(*prt);
}
}
{public static void Main()  //main function
{
pointerclass  pc = 

{
pointerclass  pc = new pointerclass();  //make the object of pointerclass
pc.Method();
}

//make an other class -----------------------------------------------------class MypointerClass   // main class.



In asp.net with C# programming  array elements can be accessed by using pointer(memory address) notations.we know that array has primary address at [0] index .

// Example with array :-using System;class pointerclass
{
public  void Method()
{
int []iArray = new int[10];for(int count=0; count < 10; count++)
{
iArray[count] = count*count;
}
fixed(int *ptr = iArray)
Display(ptr);

}public  void Display(int *prt)
{
for(int i=0; i < 14;i++)
{
Console.WriteLine(*(prt+i));
}
}
}


class mtpointerclass
{
public static void Main()
{
pointerclass pc=new pointerclass();
pc.Method();
}


Unsafe code in asp.net programming?



Popular posts from this blog