+ 3
static keyword in csharp
can someone help me to understand more the static keyword and why to use it simply? also static classes, methods, and fields in c#
2 ответов
+ 10
As Anhjje said, static keyword lets you access data without creating object (instance) of a class
Usually usage of static methods and properties should be minimised. But when you need to create some kind of method which will usually setup some things that are not necessary to create object for, then you should use static method.. for example
Image having an arena game
Objects should be entities like fighters etc., they shouldn't be using static properties, but then you are going to have entity like arena which will affect the game flow. Arena would probably use static keyword for "status method"
+ 4
One of my latest posts:
Because they belong to a class rather than an object.
Example:
public Class Program{
public static int getOne (){
return 1;
}
public int getTwo() {
return 2;
}
public static void main(String[] args) {
getOne(); //doesn't need an object because it's static
Program x = new Program();
x.getTwo(); // we needed to create object x because method wasn't static
}
}
https://www.sololearn.com/Discuss/1868168/?ref=app