7

Can I use static methods in my ASP.NET Pages and UserControls classes if they don't use any instance members? I.e.:

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
 gridStatement.DataSource = CreateDataSource();
 gridStatement.PageIndex = e.NewPageIndex;
 gridStatement.DataBind();
}
private static DataTable CreateDataSource()
{
 using (var command = new SqlCommand("SELECT foobar"))
 {
 var table = new DataTable();
 new SqlDataAdapter(command).Fill(table);
 return table;
 }
}

Or this is not thread-safe?

asked Dec 8, 2009 at 11:35
1
  • What type of variable is the "command" object? Commented Dec 8, 2009 at 11:36

3 Answers 3

9

Yes, you can use static methods - they are thread-safe. Each thread will execute in a separate context and therefore any objects created inside a static method will only belong to that thread.

You only need to worry if a static method is accessing a static field, such as a list. But in your example the code is definitely thread-safe.

Mohammed Osman
4,2863 gold badges33 silver badges30 bronze badges
answered Dec 8, 2009 at 11:49
Sign up to request clarification or add additional context in comments.

1 Comment

I think your comment "you can use static members" is incorrect. A "member" is a field, property, or method. So by that definition a static member (variable) and a static field are the same thing. Maybe you meant to say it's ok to use static methods. The statement about any objects created in a static method will only belong to that thread is misleading. Any instance variable (non-static) is threadsafe regardless of if the method is static or not. A static method just means that the code is static; it says nothing about the variables.
2

nothing shared across threads, so it is thread safe. unless you access static members that other static methods have a chance of executing concurrently with it...

answered Dec 8, 2009 at 11:39

Comments

1

it is. The only thing to worry about in your context about thread-safeness is a concept that involves static members, as already said. When any method (static or not) accesses a static member, you should worry about multithreading issues. Consider the following:

public class RaceConditionSample
{
 private static int number = 0;
 public static int Addition()
 {
 int x = RaceConditionSample.number;
 x = x + 1;
 RaceConditionSample.number = x;
 return RaceConditionSample.number;
 }
 public int Set()
 {
 RaceConditionSample.number = 42;
 return RaceConditionSample.number;
 }
 public int Reset()
 {
 RaceConditionSample.number = 0;
 return RaceConditionSample.number;
 }
}
RaceConditionSample sample = new RaceConditionSample();
System.Diagostics.Debug.WriteLine(sample.Set());
// Consider the following two lines are called in different threads in any order, Waht will be the
// output in either order and/or with any "interweaving" of the individual instructions...?
System.Diagostics.Debug.WriteLine(RaceConditionSample.Addition());
System.Diagostics.Debug.WriteLine(sample.Reset());

The answer is: It may be "42, 43, 0", "42, 0, 1" you wont know before..

answered Mar 11, 2011 at 15:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.