+ 2

What does it mean static in C#

11th Nov 2017, 11:56 PM
Marta P
Marta P - avatar
2 Réponses
+ 6
Static methods and properties are accessed via their class type rather than via a class instance. A simple example of this can be seen when reviewing the String type. The String type has both static and instance versions for the Equals() method. Let's see how they differ. string userA = "Marta P"; string userB = "David C" //Using the non static version of the Equals() method. userA.Equals(userB); //Since the Equals() method is tied to the instance object, it direct has access to the value "Marta P.". //-------------------- //Using the static version of the Equals() method. String.Equals(userA, userB); //This static method must be called using the class type name. Since it is not tied to an object instance, it has no access to the values of either string value. Therefore, both string variables must be passed to the method to be compared. Hopefully this very basic explanation makes sense.
12th Nov 2017, 12:45 AM
David Carroll
David Carroll - avatar
+ 6
Static, in C#, is a keyword that can be used to declare a member of a type so that it is specific to that type. Thestatic modifier can be used with a class, field, method, property, operator, event or constructor. Source: https://www.techopedia.com/definition/4913/static-c
12th Nov 2017, 12:10 AM
qwerty
qwerty - avatar