Confusion over Static class and Static method!

Discussion in 'ASP .Net' started by Anup Daware, Feb 2, 2007.

  1. Anup Daware

    Anup Daware Guest

    Hi Group,
    I have a little confusion over the use of static class in C#.

    I have a static method in my static class. This method reads an xml
    and returns a collection of objects.
    This collection of objects can be different for different users.
    This method uses some non-static variables which are local to this
    method.

    Following is my static class:
    public static class ResponseXmlParser
    {
    public static Hashtable GetProductInfoList()
    {
    XmlTextReader xmlTextReader;
    bool isScheduleElement = false;
    bool isResultItem = false;
    ProductInfo productInfo= new ProductInfo();
    Hashtable productInfoList = new Hashtable();
    try
    {
    //Create an instance of the XMLTextReader.
    //MaterialSearchResponse.xml is different for different users.
    xmlTextReader = new XmlTextReader("MaterialSearchResponse.xml");
    // Process the XML file.
    while (xmlTextReader.Read())
    {
    if(productInfo == null)
    productInfo = new ProductInfo();
    if (xmlTextReader.NodeType == XmlNodeType.Element)
    {
    if (xmlTextReader.Name == "CHECK_SCHEDULE_EX")
    isScheduleElement = true;
    if (xmlTextReader.Name.Equals("CHECK_ITEM_OUT"))
    isResultItem = true;

    if (!isScheduleElement && isResultItem)
    {
    switch (xmlTextReader.Name)
    {
    case "ITM_NUMBER":
    if (!xmlTextReader.IsEmptyElement)
    productInfo.ItemNumber =
    xmlTextReader.ReadElementContentAsInt();
    break;
    ...
    ...
    ..
    case "REQ_QTY":
    if (!xmlTextReader.IsEmptyElement)
    productInfo.RequestedQuantity =
    xmlTextReader.ReadElementContentAsInt();
    if (productInfo != null)
    productInfoList.Add(productInfo.IPC, productInfo);
    productInfo = null;
    break;
    }
    }
    }
    }//END OF WHILE
    }
    catch (XmlException ex){}
    catch (Exception ex){}
    finally
    {
    if (xmlTextReader!= null)
    xmlTextReader.Close();
    }
    return productInfoList;
    }
    }


    My doubts are:
    1. Does a non-static local variable of static methods works fine in
    multi-user environment (Web Application) where method is expected to
    return different results?
    2. Is there any other performance a benefit using the static methods
    except the non-requirement of creation of object of class in which
    method resides?

    Thanks,
    Anup Daware
     
    Anup Daware, Feb 2, 2007
    #1
    1. Advertising

  2. Hi Anup,

    > My doubts are:
    > 1. Does a non-static local variable of static methods works fine in
    > multi-user environment (Web Application) where method is expected to
    > return different results?


    Yes indeed, local variables (function / proc ) are always non-static (even
    in static methods) and allocated on the call stack (of course reference types
    are created on the heap, but pointers to these objects are local too)

    > 2. Is there any other performance a benefit using the static methods
    > except the non-requirement of creation of object of class in which
    > method resides?


    I was you I wouldn't bother. But if you really want to know - it depends. If
    the instance method is virtual, one extra jump is required to dereference
    method address in V-table, so it’s more expensive (but as I said – these days
    it’s really a tiny thing). Please note that algorithm and logic is more
    important that language related improvements, in addition to that, if you are
    going to call this method once (not in multi thousand step loop), there is
    no point to bother. Anyway, if you are interested in this topic, there are
    nice articles out there,, for instance
    http://www.codeguru.com/csharp/.net/net_general/performance/article.php/c11849__1/

    Regards

    Milosz
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 2, 2007
    #2
    1. Advertising

  3. Anup Daware

    Anup Daware Guest

    Hi Milosz,

    Thanks for the answer.
    You cleared all my doubts related to static :)

    Thanks,
    Thanks

    On Feb 2, 3:24 pm, Milosz Skalecki [MCAD] <>
    wrote:
    > Hi Anup,
    >
    > > My doubts are:
    > > 1. Does a non-static local variable of static methods works fine in
    > > multi-user environment (Web Application) where method is expected to
    > > return different results?

    >
    > Yes indeed, local variables (function / proc ) are always non-static (even
    > in static methods) and allocated on the call stack (of course reference types
    > are created on the heap, but pointers to these objects are local too)
    >
    > > 2. Is there any other performance a benefit using the static methods
    > > except the non-requirement of creation of object of class in which
    > > method resides?

    >
    > I was you I wouldn't bother. But if you really want to know - it depends. If
    > the instance method is virtual, one extra jump is required to dereference
    > method address in V-table, so it's more expensive (but as I said - these days
    > it's really a tiny thing). Please note that algorithm and logic is more
    > important that language related improvements, in addition to that, if you are
    > going to call this method once (not in multi thousand step loop), there is
    > no point to bother. Anyway, if you are interested in this topic, there are
    > nice articles out there,, for instancehttp://www.codeguru.com/csharp/.net/net_general/performance/article.p...
    >
    > Regards
    >
    > Milosz
     
    Anup Daware, Feb 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=

    Confusion Over Postback, ViewState and Dynamically Added Controls

    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=, May 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,373
    Brock Allen
    May 3, 2005
  2. Jordan Willms
    Replies:
    2
    Views:
    850
    Richard Tobin
    Nov 9, 2004
  3. mikea_59
    Replies:
    1
    Views:
    359
    David Carlisle
    Apr 3, 2005
  4. Cndistin
    Replies:
    3
    Views:
    280
    Pierre Quentel
    Jan 6, 2004
  5. Replies:
    2
    Views:
    315
    Daniel T.
    Jan 16, 2006
Loading...

Share This Page