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");
            }


        }
    }

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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, 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...
I copy and paste it in the below reply.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.