Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在字符串生成器上添加变量时,静态还是非静态更容易预制?_C# - Fatal编程技术网

C# 在字符串生成器上添加变量时,静态还是非静态更容易预制?

C# 在字符串生成器上添加变量时,静态还是非静态更容易预制?,c#,C#,原谅我,我是新来的。我需要创建一个类,它将用值填充字符串生成器。所以我可以用两种方法来实现这一点 public class QrCode { private static StringBuilder str=new StringBuilder(); public static void PopulateString(int value1,int value2,decimal value3) { str.Append(value1 + "|"

原谅我,我是新来的。我需要创建一个类,它将用值填充字符串生成器。所以我可以用两种方法来实现这一点

 public class QrCode
 {

     private static StringBuilder str=new StringBuilder();
     public static void PopulateString(int value1,int value2,decimal value3)
     {
         str.Append(value1 + "|" + value2 + "|" + value3 + "|");
     } 

     public static void Init()
     {
         str.Clear();
     }

     public static string GetResult()
     {
         return str.ToString();
     }

 }
在我的表格里,我也在使用这个

QrCode.Init();  
for(int a = 0; i<=grid.RowsCount; i++)
QrCode.PopulateString(
  Convert.ToInt32(grid.GetFocusedRowCellValue("value1")),
  Convert.ToInt32(grid.GetFocusedRowCellValue("value2")),
  Convert.ToDecimal(grid.GetFocusedRowCellValue("value3"))
);
var result=QrCode.GetResult();
//rest of my code
在我的表格里:

 var qrCode = new QrCode();
 for(int a=0; i<=grid.RowsCount; i++)
   qrCode.PopulateString(
    Convert.ToInt32(grid.GetFocusedRowCellValue("value1")),
    Convert.ToInt32(grid.GetFocusedRowCellValue("value2")),
    Convert.ToDecimal(grid.GetFocusedRowCellValue("value3"))
  );
  var result=qrCode.GetResult();
   //rest of my code
var qrCode=新的qrCode();

对于(int a=0;i来展开上面的评论:如果下面的方法可行,那么static就不是一个选项:

void doSomething()    
{
   QRCode code1 = new QRCode();
   QRCode code2 = new QRCode();

   code1.Populate("1,2,3");
   code2.Populate("4,5,6");

  // the content with the static implementation would now contain data from BOTH Populate calls - since the StringBuilder is 'shared' amongst instances.
}

StringBuilder类的append方法向字符串生成器保留的字符串添加新文本。 因此,如果您计划拥有QrCode类的多个实例,那么使用此方法静态是错误的,因为每个新对象的“str”将包含来自先前创建的对象的文本(在这种情况下,静态方法+追加会生成意外的文本累加器)

根据最佳实践,您应该始终尝试封装实现-因此meke方法是非静态的,除非您真的(出于明确的原因/要求)需要使它们是静态的。此外,使方法非静态(属于对象)可以节省很多麻烦(例如线程安全)


如果您担心代码只有一个版本,那么就不要这样做。非静态方法event If instatiated多次,只编写一次,并保存在一个类中。

这是两件截然不同的事情。哪一件“更好”取决于你想如何使用它。如果你需要两个独立的
QrCode
s,静态解决方案或多或少都是无用的。我只想为QrCode提供一个唯一的方法。我只是想知道是否将所有这些静态变量保存在内存中更痛苦。我怀疑你是否想要
StringBuilder
这里,字符串插值n似乎足够了
stringresult=$“{1},{2},{3.05m}”
好吧,它们将在应用程序的整个生命周期内一直存在。这可能会成为一种痛苦,也可能不会。如果您不将其设置为静态,但引用的应用程序寿命范围为一个,并且只实例化一个,那么应该不会有太大区别。您在哪里以及如何访问StringBuilder中的数据?看起来您您只是添加数据,而不是使用数据。旁注:
qrcode1=新的QRCode(“1,2,3”);
(没有
Populate(…)
)似乎更自然。或者我的问题有点不清楚,请检查我修改的问题
void doSomething()    
{
   QRCode code1 = new QRCode();
   QRCode code2 = new QRCode();

   code1.Populate("1,2,3");
   code2.Populate("4,5,6");

  // the content with the static implementation would now contain data from BOTH Populate calls - since the StringBuilder is 'shared' amongst instances.
}