Extension methods for a private class in C #

I have a class sealed,

public sealed class A
{
    public string AName {get;set;}
}

and someone can write an extension method for it like this:

public static class Extensions
{
   public static void ExtensionMethodForA (this A a)
   {
      Console.WriteLine("A Extension method!");
   }
}

The question is how do you prevent this?

+4
source share
3 answers

No. You can not. And you should not want this.

Instance methods always prefer extension methods, so it should not be a conflict. Other than that, they are just syntax / convenience. Do not try to make life uncomfortable for callers.

+13
source

" ". ; . , , .

, .

+4

. , , .

'this' , , . . .

- , , .

+3

All Articles