一个静态类与非静态类的静态方法的静态方法(C#) [英] Static Method of a Static Class vs. Static Method of a Non-Static Class ( C# )

查看:142
本文介绍了一个静态类与非静态类的静态方法的静态方法(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人问我,在接受采访时对上述问题。你能解释一下有什么不同? (性能 - 内存 - 使用 - 时使用的)

I was asked the above question in an interview. Could you please explain the differences? ( performance - memory - usage - when to use which ? )

感谢您,

二崁

推荐答案

声明一个静态类文件的该类意图是静态的功能集合,任何人都加入实例成员会得到一个编译错误。

Declaring a static class documents your intent for that class to be a collection of static functionality, and anyone adding instance members will get a compilation error.

与静态成员的非静态类通常表明类被设计在某些时候被实例化。这些类的静态方法通常做两件事情之一:

A non-static class with static members usually indicates that the class is designed to be instantiated at some point. Static methods of these classes usually do one of two things:


  1. 用于创建类型的实例提供一个工厂方法;

  2. 提供不需要的类型的实例的辅助功能;

此外,如已经提到的,扩展方法只能在一个静态类声明。

Also, as mentioned already, extension methods can only be declared on a static class.

这篇关于一个静态类与非静态类的静态方法的静态方法(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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