开发者

Question on static classes - general use (examples) [duplicate]

开发者 https://www.devze.com 2023-01-22 18:08 出处:网络
This question already has answers here: Closed 12 years ago. Learning static classes, I read some examples and would like to ask whether my opinion is correct:
This question already has answers here: Closed 12 years ago.

Learning static classes, I read some examples and would like to ask whether my opinion is correct: Menus - MY opinion: is it because I only need one menu and instances of menu are not making sense?

Constant information - MY opinion: I could have simply const variables, right?

Helper methods - MY opinion: it is because th开发者_开发问答ey do not belong to any object and are more "generic"? Thanks


Did you do any reading/research before you posted here?

Static Classes and Static Class Members (C# Programming Guide)

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.

And from Static Classes and Static Class Members (C# Programming Guide)

A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the .NET Framework Class Library, the static System.Math class contains methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class. That is, you apply the members of the class by specifying the class name and the method name, as shown in the following example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号