I'm focusing on a little project and that i discovered this problem.

The project output is really a library that contains an interface. I must implement that interface and seal the functions inside it such as this if at all possible:

public interface ITest
{
    void SomeMethod();
}

class A : ITest
{
    public sealed override SomeMethod()
    {

    }
}

The concept is to achieve the interface open to everybody and also have some specialized class that implements it. The exception is the fact that I wish to make certain when someone produce a specialized class of type A, he/she will not have the ability to alter the method's behavior.

The issue is you cannot place the "override" keyword inside because the method is not declared as "virtual" within the interface. And also you can't declare it as being "virtual" within the interface becasue it is not permitted. And also you can't take away the "override" keyword becasue it is required by "sealed".

Any workaround or brainstorming idea could be welcome, but when someone can develop an answer which includes an interface, I'd be really pleased to learn it!

Thanks!

EDIT: Forget this! Like Ani stated, I didn't remember that automatically method in C# are sealed. Appears like it certainly is good to return to the fundamentals every now and then...

I might have completely misinterpreted the question, but when your intention would be to seal the technique in A, you can easily do:

class A : ITest
{
    public void SomeMethod()  { ... }
}

Unlike Java, techniques in C# are sealed automatically. Subclasses of A will not have the ability to override the technique because it has not been marked virtual.

However, in case your intention would be to mark the technique 'almost sealed' within the interface, to ensure that it forces upon an applying class to instantly seal it, that is not possible. It is not (and should not be) the process of the interface to dictate such particulars of implementation - an interface is supposed to represent a specs.

Why don't you make use of an abstract class like below.

Haven't examined it but this will work?

public abstract class Test
{
    public virtual void SomeMethod() {}
    //OR
    public abstract void SomeMethod();//MSDN says:
    //an abstract method is implicitly virtual
}

class A : Test
{
    public sealed override SomeMethod()
    {

    }
}

Make use of an abstract base class with internal visibility. This base class isn't visible outdoors from the library but enables you to definitely seal the technique and also the class still implements the interface.

public interface ITest
{
    void SomeMethod();
}

internal abstract class SuperA : ITest
{

    public abstract void SomeMethod();

}

class A : SuperA
{
    public sealed override void SomeMethod()
    {

    }
}

Knowing about it of sealed keyword is incorrect. Like a method modifier, sealed can be used to avoid a virtual method(defined within the base class) to become override within the next generation of derived classes. For instance:

class Base
{
   public virtual void M() { }
}

class Derived : Base
{
   public sealed override void M() { }
}

class A : Derived
{
   public override void M() { } //compile error, M is sealed in Derived
}

Designers can invariably use new modifier to define a technique with similar title within the derived class, that hides the main one defined within the base class.

if a person produce a specialized class of type A, he/she will not have the ability to alter the method's behavior.

If "specialized class" means a category based on A, the reply is: he is able to always hide the technique inside a, but he can't alter the method's behavior.

Techniques in C# are sealed automatically.. This is a sample

class Program
{
    static void Main(string[] args)
    {

        A obj = new A();
        obj.SomeMethod();
        b ss = new b();
        ss.SomeMethod();
        Console.ReadLine();
    }
}

public interface ITest { void SomeMethod(); }

class A : ITest { public void SomeMethod() {

    Console.WriteLine("SomeMethod Called from Class A object");
} }

class b : A
{
    //public override void SomeMethod()
    //{
    //    Console.WriteLine("Called from Class B Object");
    //}
}