在静态属性 getter 中创建的新对象是否只创建一次?

发布于 2024-08-17 15:14:18 字数 907 浏览 3 评论 0原文

我正在为我正在编写的 Web 服务创建一个静态帮助程序类。我正在利用 Telligent 的 API,从而创建这些 Telligent“服务”对象,使我能够使用该平台执行操作。例如,我在辅助类中创建一个 MembershipSerice 对象,这样我就可以使用同一对象执行成员资格操作。在我的辅助类中,我有一个带有 getter 的属性,它创建了这些对象中的一个新对象:

private static MembershipService _MembershipService {
  get {
    return new MembershipService(path, usr, pwd);
  }
}

然后我有一个返回该属性的方法:

public MembershipService getMembershipService() {
  return _MembershipService;
}

这是否意味着由于该属性是静态的,因此仅创建在 getter 中创建的新对象一次?另外,就编码风格而言,将属性公开是不是更好,因此该方法就没有必要了。

或者...最好将对象创建为私有变量并通过公共方法返回它:

private static MembershipService _mbrSvc = new MembershipService(path, usr, pwd);
public MembershipService getMembershipService() {
  return _mbrSvc;
}

// then called elsewhere by me via:
MembershipService svc = MyHelperClass.getMembershipService();

如果您对我如何设置它以使其高效且性能良好的风格有任何想法,请告诉我。

I'm creating a static helper class for web services I'm writing. I'm tapping into the API of Telligent and therefore creating these Telligent "service" objects that allow me to do things with the platform. For example, I'm creating a MembershipSerice object in my helper class so I can do membership stuff with the same object. In my helper class I have a property with a getter that creates a new one of these objects:

private static MembershipService _MembershipService {
  get {
    return new MembershipService(path, usr, pwd);
  }
}

And then I have a method that returns that property:

public MembershipService getMembershipService() {
  return _MembershipService;
}

Does that mean since the property is static that the new object created in the getter is only created once? Also, in terms of coding style, would it just be better to make the property public and therefore the method would be unnecessary.

Or... would it be better to just create the object as a private variable and return it via the public method:

private static MembershipService _mbrSvc = new MembershipService(path, usr, pwd);
public MembershipService getMembershipService() {
  return _mbrSvc;
}

// then called elsewhere by me via:
MembershipService svc = MyHelperClass.getMembershipService();

If you have any thoughts on the style of how I set this up so its efficient and performs well, please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-08-24 15:14:18

这是否意味着由于该属性是静态的,因此在 getter 中创建的新对象仅创建一次?

不,因为您在

new MembershipService(path, usr, pwd);

getter 内部,无论 _MembershipService 是否声明为 static,每次调用都会创建一个新实例。在此上下文中,static 意味着您可以在没有定义类的实例的情况下调用该方法。

至于风格的评论,这实际上取决于你想做什么。您想要单个实例吗?如果是这样,则创建一个静态成员变量 _mbrSvc 并从 getter 返回该变量:

private static MembershipService _mbrSvc = new MembershipService(path, usr, pwd);
private static MembershipService _MembershipService {
    get {
        return _mbrSvc;
    }
}

否则,照原样就可以了。

Does that mean since the property is static that the new object created in the getter is only created once?

No, because you have

new MembershipService(path, usr, pwd);

inside the getter, regardless of _MembershipService being declared static a new instance will be created on every invocation. All that static means in this context is that you can invoke the method without having an instance of the defining class.

As far as commenting on the style, well, it really depends on what you want to do. Do you want a single instance? If so, create a static member variable _mbrSvc and return that from the getter:

private static MembershipService _mbrSvc = new MembershipService(path, usr, pwd);
private static MembershipService _MembershipService {
    get {
        return _mbrSvc;
    }
}

Otherwise, as it is is fine.

青衫负雪 2024-08-24 15:14:18

杰森、马克
为了完整起见,它还取决于 MembershipService 的定义。
如果 MembershipService 是一个类,则该对象在调用方法之间共享。
如果 MembershipService 是一个结构,则始终会创建一个副本。仅仅将其称为 MembershipService 对象并不能说明区别,因为在 DotNet 中一切都是对象。

如果创建对象的成本很高,请考虑使用单例模式与延迟加载相结合:

private static MembershipService _mbrSvc;
private static MembershipService _MembershipService { 
    get { 
        if(_mbrSvc == null)
        {
          _mbrSvc = new MembershipService(path, usr, pwd); 
        }
        return _mbrSvc; 
    } 
} 

Jason, Mark
Just for completeness sake, it als depends on the definition of MembershipService.
If MembershipService is a class then the object is shared between the calling methods.
If MembershipService is a struct, a copy is always being created. Just calling it a MembershipService-object does not specify the difference, since in DotNet everything is an object.

If the object is expensive to create, consider using a singleton pattern combined with lazy loading:

private static MembershipService _mbrSvc;
private static MembershipService _MembershipService { 
    get { 
        if(_mbrSvc == null)
        {
          _mbrSvc = new MembershipService(path, usr, pwd); 
        }
        return _mbrSvc; 
    } 
} 
情未る 2024-08-24 15:14:18

每次使用new都会创建一个新对象。

因此,第一种方法在每次访问属性时都会创建一个新对象。

您正在寻找的称为 Singleton - 您的第二种方法是在 C# 中实现它的一种可能性。

Everytime you use new a new object is created.

Therefore, the first approach creates a new object everytime the property is accessed.

What you are looking for is called a Singleton - your second approach is one possibility to implement it in C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文