There are some conceptual errors in the question and the AP comment on the other question is important, but on the contrary.
The Program class was automatically created next to the static void Main method
Learn to program, do not learn what any tool helps you. C # does not create anything, so work on this idea.
I made a method for entering data in this class
In real codes we do not usually do this, each with its own responsibility.
When I was calling this method in the static void Main method, I needed to instantiate the Program class inside itself
In the way it was done, it does.
I noticed this happens when it's a static method. Why does the static method force you to instantiate the class itself?
No, the problem is having a method that is not static
. Static methods are available for the application, instance methods, which is the case of EntradaDeDados()
, require that an instance be created. So the question does not fit.
I modified the Main method of static to public
public
and static
are not related, so you could have both, do not need to change.
Main()
needs to be called by .NET, that is, it must be available to the application without instantiating anything. So it must be static. The Main()
itself is not so special, you can determine the entry point of the application, but it always has to be static. Main()
is an interesting convention.
This has all been answered in Why is the application entry point a static method? . So technically the question is a duplicate, but it has a small detail that is not in the question that makes it interesting.
I just do not know what to use for static
The correct question should be: why not use static
?
What do you get in not using static
?
The gain is that you can create instances of that, you can generate an object based on that model. In fact not only can, requires, after all if a member is available to the instance you can only access it by instance.
The simpler the better . Creating an instance without necessity does not make 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
. This is simple. Even in the right place it would only make sense in another context, it would have to be a class that handles the publisher's data entry and created it in a way that the instance would be more appropriate than the static form.
But I can access methods that are not static within static, I need to instantiate an object of the class that has this method
Power can, but for what?
One of the simplified example problems to learn is that it does not teach structuring an application. It is great for learning simple, timely concepts that many programmers ignore and this is to skip a step, so most programmers can not program, you are going in the right way, just do not think you are now learning to structure the whole application, This is a lot harder than people think. Learn the punctual first and the whole afterwards. Just keep in mind that while learning the punctual is not structuring correctly. This is a common mistake in beginners, see an example and think that is correct when in fact it is only a simplification to show the point being taught. Nor will I enter into the merit that there are those who do not know how to do and try to teach.
When you do something that requires an instance there we can help you this way, the solution to your problem at this point is to make the method static.
Instantiate class or use public methods?