C# Inheritance, adding new methods [closed]

Why can’t I do this and then do :

B classb = new B(1,2,3);

classb.blah(4);

You absolutely can. There’s no problem with that at all, other than the syntactic errors in your code. Here’s a complete example which compiles and runs with no issues. (I’ve fixed the naming convention violation at the same time for the blah method.)

using System;

class A
{
    public A(int a, int b, int c)
    {
    }
}

class B : A
{
    public B(int a, int b, int c) : base(a, b, c)
    {
    }

    public void Blah(int something)
    {
        Console.WriteLine("B.Blah");
    }
}



class Test
{
    static void Main()
    {
        B b = new B(1, 2, 3);
        b.Blah(10);
    }
}

Leave a Comment