SteveHarper スティーブン
SteveHarper スティーブン
CC#
Created by Nguynnn :> on 12/18/2023 in #help
can somebody explain to me the func of static void?
If you create a class called Person, and Person has a property called Name, you would instantiate a new Person like this. Person bob = new(); bob.Name="Bob";. If you had a method in it called public string GetPlanet(), it would have to return a string, and you would call it like this: string? planet = bob.GetPlanet(); If you have a static method, however, you would NOT use bob to call it. You would use Person to call it. If, for example, the Person class had a static method called public static int GetTheNumber1(), then you would call it like this: int number = Person.GetTheNumber1(); and it would ALWAYS return the same thing no matter how many times you made a new Person. Also, it would NOT be tied to any particular person. If you did bob.GetTheNumber1(); you would get a compilation error. (CS0176 Member 'Person.GetTheNumber1()' cannot be accessed with an instance reference; qualify it with a type name instead)
7 replies