JMacDonald
JMacDonald

Reputation: 21

Thread Safe Method?

I have a class with the following static method:

public static Content GetContentById(int id)
{
    Content c = null;

    string sql = "SELECT QUERY";

    using (SqlDataReader dr = SqlHelper.ExecuteReader(Constants.ConnectionString, CommandType.Text, sql, new SqlParameter("@id", id)))
    {
          if (dr.HasRows && dr.Read())
          {
               c = new Content(dr.GetInt32(0));
          }
    }

    return c;
 }

Now, I've done some reading up on threading and in my mind it should be safe as it's only using local variables and not manipulating an object / member in global state?

Can someone confirm this for me?

EDIT: To Include content constructor

    public Content(int Id)
    {
        this.Id = Id;
    }

Upvotes: 2

Views: 167

Answers (2)

Tudor
Tudor

Reputation: 62439

You are only using local variables and only doing reads on the database. It's safe in my opinion.

Upvotes: 1

Chris Shain
Chris Shain

Reputation: 51309

Assuming that the constructor for Content doesn't do anything surprising (read: unsafe for multithreading), then yes it looks thread safe to me.

Upvotes: 1

Related Questions