静态方法VS在C#中的实例方法 [英] Static methods vs instance methods in C#

查看:198
本文介绍了静态方法VS在C#中的实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我写一个应用程序,我想有极端的可扩展性和扩展方法似乎给我我想要的,加上打电话给他们没有一个实例的能力,这是我需要太多。

我记得读的静态方法比实例方法更快,但没有得到GC的优点。这是正确的?

这是极不可能,我会改变我的设计,除非我找到设计速度没有一种更好的选择。但还是提供额外的信息,我想知道在速度,气相色谱等方面的差异。

编辑:谢谢。更多信息:假设我们有一个Person类:

 类Person
 

它可以有一个实例距离方法,以便像:

  this.Distance(人P)
 

这是伟大的,但是这并没有给我算2分(说POINT3),之间的距离,而无需创建Person类的实例的能力。

我想要做的是这样的:

 类Person(无距离的方法)
 

但距离扩展方法:

 距离(此人,人)
距离(这POINT3,POINT3)
 

这样我既可以做的:

  myPerson.Distance(yourPerson)
 

  Extensions.Distance(pointA,pointB)
 

EDIT2:@乔恩,是的,我认为这是指的是什么(不要GC的优势),但我总觉得,静态方法创建这个包袱/开销

解决方案

你是什么意思没有得到GC的优势呢?方法是不是垃圾收集 - 实例

虚拟方法的的比非虚拟的慢,我想有这么讨厌的空检查任何实例方法之前,但它不是显著。去最合适的设计。

静态方法是用于测试的疼痛,虽然 - 例如,如果在身份验证方法美孚()通过调用一些静态方法,那么当你测试美孚()你不能只是调用一个模拟验证器(除非静态方法本身可以让你做到这一点)。如果你给什么,你正在测试包含模拟实现一些接口的进行身份验证()的方法,但是,你可以,但是你想要的行为。<原始实例/ P>

编辑:在这种情况下,这听起来像你真正需要的是在你的点实例方法键入计算两个点之间的距离(本之一,另一种) - 或潜在的静态工厂方法的距离键入

For an application I am writing, I want to have extreme extensibility and extension methods seem to give me what I want, plus the ability to call them without an instance, which I need too.

I remember reading that static methods are faster than instance methods but don't get the advantages of GC. Is this correct?

It's highly unlikely I will change my design unless I find a superior alternative by design not speed. But still for extra information I wanna know the differences in speed, GC, etc.

EDIT: Thanks. More info: Let's say we have a Person class:

class Person

which can have an instance Distance method so like:

this.Distance (Person p)

This is great, but this doesn't give me the ability to calculate the distance between 2 points (say Point3), without creating instances of the Person class.

What I want to do is this:

class Person (no Distance methods)

but extension methods of Distance:

Distance (this Person, Person)
Distance (this Point3, Point3)

This way I can both do:

myPerson.Distance (yourPerson)

and

Extensions.Distance (pointA, pointB)

EDIT2: @Jon, yeah I think that was what was meant by (don't get the advantages of GC), but I somehow thought that the static methods create this burden/overhead.

解决方案

What do you mean by "don't get the advantages of GC"? Methods aren't garbage collected - instances are.

Virtual methods are slightly slower than non-virtual ones, and I guess there's that pesky null check before any instance method, but it's not significant. Go with the most appropriate design.

Static methods are a pain for testing though - for instance, if you authenticate in method Foo() by calling some static method, then when you're testing Foo() you can't make it just call a mock authenticator (unless the static method itself lets you do that). If you give the original instance of whatever you're testing a mock implementation of some interface containing an Authenticate() method, however, you can make it behave however you want.

EDIT: In this case, it sounds like what you really need is an instance method on your Point type to calculate the distance between two points ("this" one and another) - or potentially a static factory method on the Distance type.

这篇关于静态方法VS在C#中的实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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