开发者

C# - Member variables passed to internal methods?

When a class has an internal variable, should it be passed to methods internally, or 开发者_如何学编程should the method 'know' about it?

Eg

int _someid;

private void MyFunction(int ID)
{ use ID ... }

or

private void MyFunction()
{ use _someid ... }


No, you should use the second example.

If this method is meant to only use the member variable, then the second example is correct.

If your intentions are to use this method with other passed in values, from say other methods within the class, or maybe some external calls, then the first option will do.


The method should 'know' about it. This is a large part of the point of having fields in the first place.


Well, it just depends I guess. Do you ever want to call this method with anything but _someId as the parameter? If so, use the first example. If not, use the second.


The member variable is scoped to the class. So member functions 'know' about it. So assuming your member function is not static, your second example is correct.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜