Q:

C# program to demonstrate the example of New keyword

belongs to collection: C# Basic Programs | basics

0

C# program to demonstrate the example of New keyword

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 New keyword is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the New keyword in C#

using System;

class Sample1
{
    public void Method()
    {
        Console.WriteLine("Sample1.Method() called");
    }
}

class Sample2
{
    public void Method()
    {
        Console.WriteLine("Sample2.Method() called");
    }
}

class NewDemo
{
    static void Main()
    {
        Sample1 S1 = new Sample1();
        Sample2 S2 = new Sample2();

        S1.Method();
        S2.Method();
    }
}

Output:

Sample1.Method() called
Sample2.Method() called
Press any key to continue . . .

Explanation:

In the above program, we created three classes Sample1Sample2, and NewDemoSample1 and Sample2 class contains a method Method(). The NewDemo class contains the Main() method. In the Main() method we created the object S1 and S2 of Sample1 and Sample2 class. Then print the method Method() of both classes.

 

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

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to print size of various data types... >>
<< C# program to print backslash (\\)...