2

I'm currently building an asp.net web application. I want create some static method as helper methods. Is it a good idea or would I run into problems later on? No fields or properties. Just methods, some with return type and some with no return type.

Is static method shared across all users like fields and properties or are they unique?


    private static string userName;
    public static string UserName
    {
        get
        {
            if (User.Identity.IsAuthenticated)
            {
                if (userName == "" || userName == null)
                {
                    userName = User.Identity.Name;
                }
                return userName;

            }
            else
            {
                throw new ArgumentNullException("Illegal Access", "You're not login or authorize to perform such task");
            }


        }
    }
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
zXSwordXz
  • 176
  • 4
  • 15

2 Answers2

5

Yes, they are shared, but what do you think that means for a method?

Static methods are perfectly safe in ASP.NET. Even if the method is called multiple times by multiple users in multiple requests, there is no shared data between the calls.

That is, unless the static method modifies static data, in which case, you should avoid if possible, but in any case, need to lock.


Public Class MyPage
    Inherits Page

    Private Shared _iAmShared As Integer

    Private Shared Sub StaticMethod()
        Dim iAmNotShared As Integer = 0
        _iAmShared = _iAmShared + 1
        iAmNotShared = iAmNotShared + 1
    End Sub

    Public Sub Page_Load()
        StaticMethod()
    End Sub
End Class

The code above is wrong. The increment of _iAmShared needs to be interlocked. If (when) multiple requests execute that code at the same time, there is no guarantee that the increment will be atomic. There is one copy of _iAmShared for all users, and all requests.

On the other hand, iAmNotShared is not shared at all. Each call to StaticMethod gets its own copy of iAmNotShared.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • John, I realize that I didn't word my question properly. I meant if the data are shared between calls. Thanks for answering that for me. Out of curiosity, is static properties and fields data shared between calls? – zXSwordXz Nov 19 '14 at 15:17
  • Yes, static _data_ is shared. There is no difference between ASP.NET and, say, a console application. Static data is "shared" in a console application as well. The only difference is that there's nothing to share it _with_, so it doesn't matter. In the case of ASP.NET, data is shared across all users and requests. – John Saunders Nov 19 '14 at 15:31
  • But which data were you asking about? Local variables are still local to a static method, just as they are in an instance method. – John Saunders Nov 19 '14 at 15:32
  • John, here is an example(not a practical one). If userA was to call the UserName property and then UserB call it, would give give the same result? See my example below... – zXSwordXz Nov 19 '14 at 19:55
  • I copy and paste it in the below reply. – zXSwordXz Nov 19 '14 at 19:56
0

Static methods are great for helper functions in an ASP.net website. You can create a helper class with static functions that can be used throughout the site. Each call to the static method is unique and nothing is shared.

RickJames
  • 1,705
  • 1
  • 21
  • 38