[ASK]-关于变量 [英] [ASK] - About variables

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

问题描述

专家们.. :)作为C#编程的新手,我想问一下有关变量和最佳编程中的最佳方法. :)

当我在代码中声明某些变量时,我希望这些变量可以同时被某些方法使用.我已经将这些变量设置为其他类.这是我的代码:


Hi experts.. :) As a newbie in C# programming, i wanna ask u about variables and the best way in the best programming. :)

When i declare some variable in my code, i want to those variables can be used by some method in the same time. I''ve made those variables other classes. Here is my code :


//Class Variable
class Variables 
{
   public IDictionary<int, string> accNum = new Dictionary<int, string>();
  //-- other variables...
}

class Process
{
  public void Process1()
  {
    Variables vs = new Variables();
    foreach(KeyValuePair<int, string> pair in vs.accNum)
    {
      //-- processing...
    }
  }


  public void Process2()
  {
    Variables vs = new Variables();
    foreach(KeyValuePair<int, string> pair in vs.accNum)
    {
      //-- processing...
    }
  }

}



我们假设"accNum"具有一定的价值,我们需要对其进行处理.我的问题是,还有其他实现此代码的方法肯定可以在内存中更可靠,更快和更有效吗?可以帮我高手:confused:



We assume that "accNum" has some value and we need to process it. My question is, is there any other way to implement this code which is certainly more reliable, fast, and efficient in memory ? May u help me master :confused:

推荐答案

您的想法看起来不错,而且乍一看有些新鲜.

但是,根据OOP 的"Encapsulation"原理,您应该Encapsulate 您的Classes中的变量和方法.因此,每个类都应封装并拥有该类需要访问和处理的自己的属性(变量)和方法.

软件工程的另一个原则是减少"Coupling".您越减少了系统中组件之间的依赖关系和耦合,代码就越易于管理,可重用和可扩展.现在,如果您有一个专有类来封装变量(如示例中所示),并且如果您使用该类的对象来访问系统中不同区域中的那些变量,那么您将增加系统中的耦合.不同的代码将取决于相同的变量(全局变量的种类),这些全局变量的微小变化将对整个系统产生若干影响.

因此,最好仅将相关变量保留在它们所对应的类中.这就是说,每个类都有自己的变量集,这些变量可能取决于上下文,因此外界可能无法访问.
Your idea looks fine and something new at first glance.

However, the "Encapsulation" principle of OOP says, you should Encapsulate your variables and methods within your Classes. So, each class should encapsulate and own their own properties (Variables) and methods that the class needs to access and process.

Another principle of Software Engineering is to Reduce "Coupling". The more you reduce dependency and coupling among the components in your system, the more manageable, reusable and extensible your code is. Now, if you have an exclusive class that encapsulates the variables (Like what you''ve shown in your example) and if you use object of that class to access those variables in different areas in your system, you are increasing coupling in your system. Different codes will be dependent on the same variables (Kind of Global variables) and a small change in these global variables would lead several impact in the entire system.

So, it is best to keep only related variables within the class where they correspond to. That means, each class will have their own set of variables, which may or may not be accessible by outside world, depending upon context.


您不想公开自己的课程级别变量作为公共变量.记住OOP规则,即封装.将所有变量设为私有并提供访问器(请参见此处 [ ^ ]以获得msdn帮助)

You don''t want to expose your class level variables as public. Remember the rule of OOP, encapsulation. Make all your variables private and provide accessors (see here[^] for msdn help)

//Class Variable
class Variables 
{
   private IDictionary<int,> accNum = new Dictionary<int,>();
  //-- other variables...

  public IDictionary<int,> AccNum
      {
        get ;
        set ;
      }
}


好..我会尝试从我认为您要问的问题中回答,

您想使一个方法的范围扩展到其成员变量之外,而扩展到它所包含的整个类..

好的..如果这就是您要问的,那么它很有可能,C#将ref关键字放置在成员参数列表之前,然后它将能够引用超出其范围的变量.所以我将其应用到您的代码中.

ok..I''ll try to answer it from what I think you''re asking about,

You want to make a method have its scope extending beyond its member variables but rather to the whole class it is contained in..

ok..if thats what you''re asking then its very possible, C# has the ref keyword which you''ll place before a members argument list and it will then be able to reference, the variable outside its scope..so I''ll apply it on your code..

class Variables 
{   
    public IDictionary<int, string> accNum = new Dictionary<int, string>();
  //-- other variables...
}

class Process{

Variables vs;  // Just create an instance of the Variable Class

// the ref keyword will open up the scope of the method to the instantiated variable above

public void Process1(ref vs)  
{       
   foreach(KeyValuePair<int, string> pair in vs.accNum)           {     
      //-- processing...    
}  

}  
<p></p>
public void Process2(ref vs)  
{   
 foreach(KeyValuePair<int, string> pair in vs.accNum) 
   {      
      //-- processing...    
   }  
}

}


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

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