There are some conceptual errors in the question and the comment of the PA in the other question is important, but in reverse.
The Program class was created automatically next to the Static void Main method
Learn how to program, don’t learn what some tool helps you. C# doesn’t create anything, so work with this idea.
I made a method for data entry in this class
In real codes we do not usually do this, each with its own responsibility.
When I went to call this method in the method static void Main
, I needed to instantiate the class Program
within itself
In the way that it was made needed even.
I realized this happens when it’s a method static
. Why the Static method forces us to instantiate the class itself?
No, the problem is having a method that’s not static
. Static methods are available for application, instance methods, which is the case of the EntradaDeDados()
, require that an instance be created. So the question does not fit.
I modified the method Main
of static
for public
public
and static
are unrelated, so could have both, need not change.
The Main()
need to be called by . NET, ie, need to be available for the application without needing to instantiate anything. So it must be static. The Main()
in itself is not so special, you can determine the application’s entry point, but it always has to be static. The Main()
is an interesting convention.
This has all been answered in Why the application entry point is a static method?. So technically the question is a duplicate, but there’s one small detail that’s not in the question that makes it interesting.
I just don’t know what to use it for static
The correct question should be: why not use static
?
What do you get from not using static
?
The gain is that you can create instances of that, you can generate an object based on that model. Actually not only can, requires, after all if a member is available to the instance you can only access it by the instance.
The simpler the better. Creating an instance without necessity makes no sense. It is true that in a real application this method would not even be there, but since it is, there is no reason why it should not be static
. That’s the simple thing. Even at the right place would only make sense in another context, it would have to be a class that deals with the input of publisher data and created in a way that the instance would be more suitable than the static form.
But I can access methods that are not static
within the static
, need to make the instance of an object of the class that has this method
Power, it can, but for what?
One of the problems of the simplified example to learn is that it does not teach to structure an application. It’s great for learning simple, punctual concepts that many programmers ignore and that’s skipping a step, so most programmers can’t program, you’re going the right way, just don’t think you’re now learning to structure the entire application, This is a lot harder than people think.
Learn the punctual first and the whole after. Just do not lose sight that while you are learning the punctual is not structuring correctly. This is a common mistake in beginners, they see an example and they think that it is the right one when in fact it is just a simplification to show the point being taught. Nor will I get into the merit that some people can’t do and try to teach.
When you do something that requires an instance then we can help in this way, right now the solution to your problem is to make the method static.
Instantiate class or use public methods?
If you want you can close or delete this question (I don’t know if you can delete it).
– HeyJoe