What is the dfference between static methods and nonstatic methods? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the dfference between static methods and nonstatic methods?

In c# lessons sometime a method is declared "static int" and sometime is just "int". So my question is: what is the difference between "static <type>" and just "<type>"

2nd Sep 2018, 9:44 AM
Gabriel Patrascu
4 Answers
+ 4
static means the value going to be shared by all instances instead of individual value for each instance. You can always refer to MSDN for good explanation and example:- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static
2nd Sep 2018, 9:48 AM
Zephyr Koo
Zephyr Koo - avatar
+ 3
You can always initialize an instance of the class provided it's not static. Think of it like a blueprint. Let's say we have a class ElectricPokemon with attributes Name and a static attribute Type. In this case, every new ElectricPokemon (instance) created (i.e. Pikachu, Jolteon, Zapdos & etc.) will have different *Name* but sharing the same *Type*. Crystal clear or clear as mud? 😉
2nd Sep 2018, 10:13 AM
Zephyr Koo
Zephyr Koo - avatar
0
Can you tell me like to an 6 year child, what "shared by all instances" means. Because englosh isn't my main laguage and I did'n understant that l part
2nd Sep 2018, 9:55 AM
Gabriel Patrascu
0
Yes. Thanks.
2nd Sep 2018, 10:29 AM
Gabriel Patrascu