静态函数存储在单个类中 [英] Static functions stored in a single class

查看:52
本文介绍了静态函数存储在单个类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于将静态函数集成到单个类中以在ASP.NET页面中使用的想法是什么?是好是坏?

What''s your idea about making static functions integraded in a single class to use in ASP.NET pages? Is it good or bad?

推荐答案

这取决于您的目的.它们的存在,因此对某些事物有好处.

首先,您需要了解它们是什么.请查看我过去的回答:
什么使静态方法可访问? [ http://en.wikipedia.org/wiki/Singleton_pattern [ http://csharpindepth.com/Articles/General/Singleton.aspx [希望您在这里……只有一次 [ http://msdn.microsoft.com/zh-CN /library/aa288467%28v=vs.71%29.aspx [ http://msdn.microsoft.com/en-us/library/bb383977.aspx [ ^ ].

—SA
It depends on your purpose. They exist, so they are good for something.

First, you need to understand what are they. Please see my past answer:
What makes static methods accessible?[^].

One important application for static members is the singleton pattern, please see:
http://en.wikipedia.org/wiki/Singleton_pattern[^].

Here is the good C# implementation; you can figure out the purpose of static:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

Please see my article for a very advanced (and funny) application:
Wish You Were Here… Only Once[^].

Static methods are used not so often; and they should be used with care when they also use static fields with access from different threads. In my article referenced above, you can find some valuable criticism by Paulo Zemec provided to fix a mistake I made.

At the same time, there are many functions where using them as instance (non-static) once would be a mistake. If they don''t access instance data and are instance methods, it''s a certain performance leak. FxCop, for example, will report it as a mistake.



Two more important uses of static methods:

Operator overloading: http://msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx[^].

Extension methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx[^].

—SA


这篇关于静态函数存储在单个类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆