gpt4 book ai didi

c# - 线程安全方法?

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:29 25 4
gpt4 key购买 nike

我有一个具有以下静态方法的类:

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

现在,我已经阅读了一些关于线程的文章,在我看来它应该是安全的,因为它只使用局部变量而不是在全局状态下操作对象/成员?

有人可以帮我确认一下吗?

编辑:包含内容构造器

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

最佳答案

假设 Content 的构造函数没有做任何令人惊讶的事情(阅读:多线程不安全),那么它看起来对我来说是线程安全的。

关于c# - 线程安全方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com