How does the compiler know that class x is an extension?

Asked

Viewed 83 times

4

In this example, I create a class extension method DateTime in the c#:

static class DateTimeExtensions
{
    public DateTime PrimeiroDiaDoMes(this DateTime data)
    {
        return new DateTime(data.Year, data.Month, 1);
    }

    public DateTime UltimoDiaDoMes(this DateTime data)
    {
        return new DateTime(data.Year, data.Month, DateTime.DaysI.......);
    }

    public bool EhFimDeSemana(this DateTime data)
    {
        return data.DayOfWeek == DayOfWeek.Saturday
            || data.DayOfWeek == DayOfWeek.Sunday;
    }
}

The point is, as the compiler knows that this is an extension class, I am in doubt between the class name (Base class name + Extension) or the this DateTime data?

  • 1

    the operator this who does that job. https://docs.microsoft.com/pt-br/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

  • related: https://answall.com/questions/136913/para-que-usar-m%C3%A9todos-de-extens%C3%A3o-do-c

  • missing in the example the static also in the methods

1 answer

8


The compiler does not need to know that the class is extension, nothing requires this, and there is actually no extension class (yet), there are static classes that have static methods that are extension. One reason for recommending that you write something in the name to indicate that the class has the purpose of extending another (s) class(s). Even the static class need not only have extension methods. The name indicates nothing, including I only use Ext at the end of the name because it’s enough for me to identify who it’s about.

The extension method is defined (up to the current version that I write this) by a keyword this that goes before the first declared parameter. This syntactic difference is enough for the compiler to know.

In the future there must be another way to express various extensions, not only methods, and may be by a syntax that demonstrates this more clearly and will have something like an extension class, but not a class. Classes are overvalued.

The static class itself is kind of weird. C# tried to copy a little the idea of Java that was in fashion and that sold the idea that everything should be in classes, after at that time only the technology that was sold as object oriented was successful, And then an obsession started, so it’s not over yet, and it ruins a lot of people’s heads, and it makes horrible codes be written to look like they’re OOP, even when the ideal is that they’re not. Now they’re more prepared to have things that aren’t classes, because a class doesn’t always make sense. Call it a module or something, you don’t have to invent something that’s not a class just to pretend it’s OOP. Actually the problem is even bigger, was what we could summarize in a paragraph.

  • @Cool days have passed, but I still ask here: in your answer, "because a class does not always make sense". Could you give me an example? Thank you!

  • 1

    @Luizaugusto this depends on a lot of information to do in a comment and is completely out of the question subject, but having static classes is a sample of this, so call something of class if it is not even a class?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.