Q:

C# program to demonstrate the pointer as a data member

0

C# program to demonstrate the pointer as a data member

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to demonstrate the pointer as a data member is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the pointer as a data member.

using System;
unsafe class UnsafeEx
{
    int* p;
    int val;

    public UnsafeEx(int v)
    {
        val = v;
        p = &v;

        Console.WriteLine("Val : " + *p);
    }
    static void Main(string[] args)
    {
        UnsafeEx U1 = new UnsafeEx(10);
        UnsafeEx U2 = new UnsafeEx(20);
        UnsafeEx U3 = new UnsafeEx(30);
    }
}

Output:

Val : 10
Val : 20
Val : 30
Press any key to continue . . .

Explanation:

In the above program, we created class UnsafeEx that contains two data members val and an integer pointer p.  Here we defined a parameterized constructor to initialized the data member val and then assign the address of data member val to the pointer p and print the value of val using pointer within the constructor of the class. Here we used the unsafe keyword with the class definition.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now