locked
static properties RRS feed

  • Question

  • User-203654341 posted

    what are the  advantages of static properties and static functions???

    Thursday, September 1, 2011 12:14 AM

Answers

  • User1642208711 posted

    Static members can be accessed from anywhere with in the application without creating a new instance of the class.

    public static class MathUtility
    {
        public static decimal PI
        {
            get
            {
                return 3.14;
            }
        }
    
        public static bool isEven(int value)
        {
             return value % 2 == 0;
        }
    }

    To make a call to above members no separate instance of MathUtility class is required.

    decimal pivalue = MathUtility.PI;
    bool isEvenValue = MathUtility.isEven(54);

    If the static keywords are not used in MathUtility class, then call to the MathUtility members varies as shown below

    var m1 = new MathUtility();
    var m2 = new MathUtility();
    
    //below 2 property calls, gives same result irrespective of the instances
    decimal pivalue = m1.PI;
    decimal pivalue = m2.PI;
    
    //below 2 method calls, gives same result irrespective of the instances
    bool isEvenValue = m1.isEven(54);
    bool isEvenValue = m2.isEven(54);

    PI and isEven result not gonna change based on different instances. So it is better to keep common methods and properties (whose result not gonna change based on different instances) in a static class.

    So the advantages of static class are

    1. There will be always one copy of a class exists through out the application.

    2. Static properties can be used to persist data in application level. (It can be used like global variable)

    3. Common methods whose result not gonna change based on instances can utilize static class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 12:25 AM

All replies

  • User1642208711 posted

    Static members can be accessed from anywhere with in the application without creating a new instance of the class.

    public static class MathUtility
    {
        public static decimal PI
        {
            get
            {
                return 3.14;
            }
        }
    
        public static bool isEven(int value)
        {
             return value % 2 == 0;
        }
    }

    To make a call to above members no separate instance of MathUtility class is required.

    decimal pivalue = MathUtility.PI;
    bool isEvenValue = MathUtility.isEven(54);

    If the static keywords are not used in MathUtility class, then call to the MathUtility members varies as shown below

    var m1 = new MathUtility();
    var m2 = new MathUtility();
    
    //below 2 property calls, gives same result irrespective of the instances
    decimal pivalue = m1.PI;
    decimal pivalue = m2.PI;
    
    //below 2 method calls, gives same result irrespective of the instances
    bool isEvenValue = m1.isEven(54);
    bool isEvenValue = m2.isEven(54);

    PI and isEven result not gonna change based on different instances. So it is better to keep common methods and properties (whose result not gonna change based on different instances) in a static class.

    So the advantages of static class are

    1. There will be always one copy of a class exists through out the application.

    2. Static properties can be used to persist data in application level. (It can be used like global variable)

    3. Common methods whose result not gonna change based on instances can utilize static class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 12:25 AM
  • User675042318 posted

    static variable, functions and classes are those for which no initialization (no object creation) is required. Just call the static class name and your variable will be avaiable to it.

    This is used for those classes which you want to use anywhere but static is already loaded when you run your application

    Thursday, September 1, 2011 1:53 AM
  • User-2067077449 posted

    Static can be used in following ways:

    1. Static data members

    2. Static constructor

    3. Static Properties

    4. Static methods or function.

    details of all above topic is avilable in this given URL

    http://www.c-sharpcorner.com/UploadFile/puranindia/SomethingAboutStaticInC05202009004313AM/SomethingAboutStaticInC.aspx

    Thursday, September 1, 2011 2:39 AM
  • User221175343 posted

    Hii,

           Static Function:- Without Creating object of Class you can call that function.

            Static Variale:- Only one copy in Memory.

    And see this link

    http://forums.asp.net/t/1699890.aspx/1

    Thursday, September 1, 2011 12:35 PM
  • User675042318 posted

    Cool. I like it

    Friday, September 2, 2011 7:50 AM
  • User-25859188 posted

    Hi

    Static variables need not be intialized , they need do not rquire an object of the class to use them
    eg:
    protected class s
    {
        public static int a;
        public static void ge()
        {
            ...............
           ................
        }
    }
    inside the code the variable and method can be accessed directly by class name
    s.a=0;
    s.ge();

    Static means it is only created once for the type .static variables are used if you want to share it to all users.Performance is one of the advantage of static variables/cache.
    Global variables don't need to be copied and only one is needed in the website application. Use static properties to store single instance data

    One disadvantage of static variables(or the ASP.Net Cache) is that you have to avoid conflicts when different users access/change them parallel by yourself
    You have be careful when declaring database connections static because they will be used simultanously by many users

    Regards

     

    Friday, September 2, 2011 8:08 AM
  • User675042318 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 class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

    Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

    The main features of a static class are:

    • They only contain static members.

    • They cannot be instantiated.

    • They are sealed.

    • They cannot contain Instance Constructors (C# Programming Guide).

    Friday, September 2, 2011 8:14 AM