Extension methods in C #: why does it work?

I am a little confused why this does not give an error. I found this code deep inside some outdated old software and was surprised to see it work.

public static string CleanFileName(this string fileName)
{
  return CleanFileName(fileName, 64);
}

public static string CleanFileName(this string fileName, int maxLength)
{
  //some logic
}

My experience using extension methods is to call it like this:

fileName.CleanFileName(64);

Does this only work because it is also a static method? Is this a common practice and something that I have not yet seen, or a piece of legacy code that I have to kill with fire?

+3
source share
2 answers

, "this" ( ). , .

, " , , ". - . , , . , , . , , , , , .

+10

, CleanFileName(string, int) , CleanFileName(string), , . , .

, static string Foo(this string foo, int bar) { } Foo(string, int) string.Foo(int).

+6

Source: https://habr.com/ru/post/1760833/


All Articles