locked
Static class members in C# RRS feed

  • Question

  • User-390775641 posted

    I'm trying to understand if the following works similar to C++. Say, if do the following:

    public class MyClass{
        public static string str1;
        public string str2;
    };

    What is the difference between str1 and str2?

    Sunday, July 10, 2011 5:50 PM

Answers

  • User-654786183 posted

    Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

    A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

    So to access str2 you have to create an instance of MyClass and to access str1 you dont need to.  You can access str1 with class name itself

    for more details please see

    http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 10, 2011 5:55 PM

All replies

  • User-654786183 posted

    Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

    A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

    So to access str2 you have to create an instance of MyClass and to access str1 you dont need to.  You can access str1 with class name itself

    for more details please see

    http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 10, 2011 5:55 PM
  • User-390775641 posted

    Thank you for taking your time to explain it!

    Sunday, July 10, 2011 6:30 PM