C# program to demonstrate the example of New keyword

Here, we are going to learn about New keyword and its C# implementation.
Submitted by Nidhi, on September 12, 2020

Here we demonstrate the use of the New keyword, which is used to allocate the space for a class.

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 Sample1, Sample2, and NewDemo. Sample1 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.