变量重用 [英] reuse of variables

查看:146
本文介绍了变量重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作项目,需要做的调用相同的方法多次,但使用不同的参数。

I'm working on project that need do call the same method several times, but with different arguments.

我可以使用相同的变量或做我必须?声明另一个变量

Can I use the same variable or do I have to declare another variable?

例如:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(domains["ServiceLogin"]);
            req.Method = "GET";
            req.Referer = "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0";
            req.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            CookieCollection cookies = response.Cookies;
            response.Close();



etc..

我用在 REQ 变量或声明 REQ2 例如:

Do I use the req variable or declare req2 for example

  req = (HttpWebRequest)WebRequest.Create(domains["ServiceLogin"]);
  req.Method = "POST";
  req.CookieContainer = myCookieContainer;



什么是最好的方法/做法做到这一点?

What's the best way/practice to do this?

推荐答案

局部变量是价格便宜;有没有令人信服的好处他们不必要地使用重。因此:编写代码,这样的每个变量都有一个明确的目的的和目的是通过它的名字所描述。一旦每个变量的名称描述了它的目的,反而会变得更加清晰,你是否需要一个变量或多个变量。

Local variables are cheap; there's no compelling benefit to re-using them unnecessarily. Therefore: write your code so that each variable has a clear purpose and that purpose is described by its name. Once each variable's name describes its purpose, it will become more clear whether you need one variable or multiple variables.

这篇关于变量重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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