When to use Shared methods in .NET

﹥>﹥吖頭↗ 提交于 2019-12-05 11:09:53
Yves M.

Since shared methods and members are called static in C# there is already stuff on Stack Overflow...

When to use static classes in C#

Use static methods (shared in Visual Basic) when they contain behaviour that isn't associated with a particular object. They don't require any state to perform their tasks.

See Static Classes and Static Class Members on the MSDN:

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

In your case, you probably don't want to use static methods if PersonManager contains some object state. Instead, you should be able to create multiple PersonManager objects and manipulate them separately.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!