当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Understanding Static Class and static method

问题描述:

Let's say I have a method like this:

public class MyDataAccess

{

public static StudentDataTable GetStudentRecords(int schoolID)

{

var ta = new StudentTableAdapter();

using (ta.Connection = new SqlConnection(GetCS()))

{

var result = ta.GetStudentRecords(schoolID);

return result;

}

}

}

If this method is accessed 1,000 times at a single time, using a console app or any other means, would there be any chance of it returning records for one school's student to another school?

e.g.

var records1 = GetStudentRecords(1);

.....

var records1000 = GetStudentRecords(1000);

Is there any chance of the records1 variable getting records of schoolID = 1 mixed with records from records1000, even if they calls come from their own threads?

Also what is going to happen if I change this class to static as well?

My understanding at this point

1) Yes, I understand what a class is

2) Yes, I know static classes don't need required instances to be used

3) I know simple static code should be there, for example converting datatypes.

But what I am confused with is:

What happens when there is a single, static class and there are a million objects trying to access the same object in STACK

especially when it's a dataAccess class, or a class which accesses activeDirectory objects.

I had a chat in the programmers SE chat room. They didn't say it explicitly, but I got the impression to never use static classes for querying e.g. sql, active directory, etc.. And that static classes should be limited to simple static code only - no querying.

网友答案:

Looking at the code you've given us, the code seems thread-safe. There is no (in-memory) data being shared between threads.

My only concern would be at the db layer - make sure you're using the correct isolation level to avoid dirty reads.

Also what going to happen if I change this class to static as well.

In regards to GetStudentRecords? Nothing.


Here's a classic example of thread-unsafe code.

public class MyDataAccess
{

    private static int _retrievalCount = 0;

    public static StudentDataTable GetStudentRecords(int schoolID)
    {
        _retrievalCount ++;

        // retrieve and return records;            
    }
}

The field _retrievalCount is shared across threads. And the operation ++ is not atomic. That is, it involves three steps

  1. retrieving the integered stored in _retrievalCount
  2. adding 1 to it
  3. and storing the result back in _retrievalCount.

If two threads do this simultaneously, and these 3 steps are interleaved, then this could happen instead (assume _retrievalCount starts at 0 and there are 2 threads running):

  1. Thread A reads _retrievalCount as 0
  2. Thread A adds 1 to 0, result = 1
  3. Thread B reads _retrievalCount as 0
  4. Thread B adds 1 to 0, result = 1
  5. Thread A stores result=1 in _retrievalCount
  6. Thread B stores result=1 in _retrievalCount

The expected output was 2, but the actual output was 1.

网友答案:

In C#, "static" merely means that you can call the method directly from the class definition as opposed to calling it against an instance of the class.

Think of the "class" as the blueprint for an object. When you create an instance of an object, the blueprint tells the computer how to create it. There are, however, some aspects of the class that don't require an actual object instance, and those aspects are candidates to be provided via "static" methods.

If you change the declaration of your method to include 'static', it changes the way it must be called.

public class Example
{   
    static void StaticMethod() {...};  // object instance *not* required
    void InstanceMethod() {...};       // object instance *is* required
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: