Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to create sealed method to prevent override the virtual method from it in C#

1 Answer

0 votes
using System;

class X {
    protected virtual void M() { Console.WriteLine("class X - M()"); }
    protected virtual void M2() { Console.WriteLine("class X - M2()"); }
}

class Y : X {
    sealed protected override void M() { Console.WriteLine("class Y : X - M()"); }
    protected override void M2() { Console.WriteLine("class Y : X - M2()"); }
}

class Z : Y {
    // protected override void M() { Console.WriteLine("Zclass Z : Y - M()"); } //  error CS0239: `Z.M()': cannot override inherited member `Y.M()' because it is sealed

    protected override void M2() { Console.WriteLine("class Z : Y - M2()"); }
}

class Program 
{
    static void Main()
    {
        var z = new Z();

        // Console.WriteLine(z.M2()); // error CS1540: Cannot access protected member `X.M2()' via a qualifier of type `Z'. The qualifier must be of type `Program' or derived from it
    }
}



/*
run



*/

 



answered Nov 27, 2020 by avibootz

Related questions

1 answer 163 views
1 answer 165 views
2 answers 202 views
202 views asked Apr 25, 2017 by avibootz
1 answer 137 views
...