Meaning of the parentheses in the instantiation in C#

Asked

Viewed 106 times

2

I can’t understand the need for () to instantiate a class.

Retangulo n1 = new Retangulo();

Is it the language’s own thing or has meaning behind?

1 answer

7


Can you understand that every so-called method needs parentheses? Even to differentiate from a field or property or otherwise than to execute?

And do you know that every code execution must be within a method? The code cannot be loose.

Well, what you’re doing there is exactly the call of a method. It’s called a builder (the new delivery that). It executes the code needed to build the object, so allocate memory and initialize its members with base values to start, and who knows how to run something else that is needed at the beginning of each object.

So there’s nothing special in these parentheses, it’s calling a method that you don’t have arguments to pass, then nothing is passed, but still uses parentheses because it is a method call and not a simple variable or something else.

What you probably didn’t know was about the constructor method. There’s something else about it in What good is a builder?. In most cases a class does not have a constructor with parameters it is wrong, even if it works. And if you have a constructor with parameters, in many cases you should not have a constructor without them. And even if you do, you should almost always not use.

This same example, without even seeing the context I can say that it is wrong, after all why create an object that has no data? It doesn’t make much sense. Is there any standard data within it? I doubt it, but if you have what advantage there is in having standard data? If you are going to change later, why? What advantage does it have? There are disadvantages! And why can you change the data later? If you change the data, does the object not become another object? If it is immutable, which would require having a constructor with parameters to be useful, then it should not create a struct and not a class? I’m not guaranteeing that this is a class, it’s a failure of C#.

Programming is thinking about all these things, not just syntax.

Some languages have chosen to allow the parentheses of the method/function to be optional when they are empty, or even in other cases, but it generates ambiguity, complicates for the compiler and it is common to become more difficult to read code so because you have to stop to think whether that is a method or not.

Particularly I would prefer a differentiated syntax for the constructor, but I’ve never seen a language do this, I think it would simplify some things, but this is another subject I’m not going to raise details here.

C# is evolving to future versions need to rely less on the constructor, but only in some scenarios can it be a danger in the wrong hands (which are the same that no longer create constructors when they should).

Browser other questions tagged

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