在C#中调用循环内参数化方法的最佳方法是什么?

时间:2018-02-26 05:11:36

标签: c# pass-by-reference pass-by-value memory-consumption

内存消耗品设计的缺点是什么?

我知道ref关键字通过引用传递参数&不会在函数参数中使用新的内存引用。据我所知,第一次设计将比第二次设计更有效。我对么?

1)。

public void SyncWeighEntityData(string sqlConnectionString, int weighBridgeID, string weibridgeName, int lastSyncedTicetNo, string imagesPath)
   {
       using (var cn = DBUtils.GetNewOpenConnection(sqlConnectionString))
       {

           var query = "SELECT * FROM [WeighEntry] WHERE TicketNo > @TicketNo";
           var lastTicketNo = cn.Query<int>("SELECT MAX(TicketNo) FROM [WeighEntry]").Single();
           var results = cn.Query(query, new { TicketNo = lastSyncedTicetNo }).ToList();
           results.ForEach(x => x.WeighBridgeID = weighBridgeID);
           cn.Close();


          SyncImages(ref results, ref imagesPath, ref weibridgeName);
           SyncWeighEntityDataToLocalDB(ref results, ref  lastTicketNo, ref weighBridgeID);
       }

   }

   private void SyncImages(ref List<dynamic> WeightEntitiesToSync, ref string imagesPath, ref string weibridgeName)
   {
       foreach (var item in WeightEntitiesToSync)
       {
           for (int i = 0; i < 4; i++)
           {
              SaveTicketImageOnLocalPath(Path.GetFullPath(CommonResources.DecodeFromBase64(imagesPath) + string.Format($"\\{item.TicketNo}_CropedIn.Jpg")), item.WeighBridgeID, weibridgeName, string.Format($"{item.TicketNo}_CropedIn.Jpg"));
           }
       }
   }

   private void SyncWeighEntityDataToLocalDB(ref List<dynamic> WeightEntitiesToSync, ref int lastTicketNo, ref int weighBridgeID)
   {
      // Code to save data
   }

或 2)。

public void SyncWeighEntityData(string sqlConnectionString, int weighBridgeID, string weibridgeName, int lastSyncedTicetNo, string imagesPath)
   {
       using (var cn = DBUtils.GetNewOpenConnection(sqlConnectionString))
       {

           var query = "SELECT [TicketNo],[VihNo],[Supervisor],[Driver],[Cleaner],[SealNO1],[SealNO2],[SealNO3],[ContainerNo],[ItemDis],[TimeIn],[TimeOut],[Weigh1],[Weigh2],[Remarks1st],[Remarks2ed],[CustName],[DateIn],[DateOut],[GinorGrnNo],[UserName],[AccountBillCount],[StoresBillCount],[CustomerBillCount] FROM [WeighEntry] WHERE TicketNo > @TicketNo";
           var lastTicketNo = cn.Query<int>("SELECT MAX(TicketNo) FROM [WeighEntry]").Single();
           var results = cn.Query(query, new { TicketNo = lastSyncedTicetNo }).ToList();
           results.ForEach(x => x.WeighBridgeID = weighBridgeID);
           cn.Close();


          SyncImages( results,  imagesPath,  weibridgeName);
           SyncWeighEntityDataToLocalDB( results,   lastTicketNo,weighBridgeID);
       }

   }

   private void SyncImages( List<dynamic> WeightEntitiesToSync,  string imagesPath,  string weibridgeName)
   {
       foreach (var item in WeightEntitiesToSync)
       {
           for (int i = 0; i < 4; i++)
           {
              SaveTicketImageOnLocalPath(Path.GetFullPath(CommonResources.DecodeFromBase64(imagesPath) + string.Format($"\\{item.TicketNo}_CropedIn.Jpg")), item.WeighBridgeID, weibridgeName, string.Format($"{item.TicketNo}_CropedIn.Jpg"));
           }
       }
   }

   private void SyncWeighEntityDataToLocalDB( List<dynamic> WeightEntitiesToSync,  int lastTicketNo,  int weighBridgeID)
   {
      // Code to save data
   }

1 个答案:

答案 0 :(得分:2)

这是不成熟的优化。我们先来看看你的第一个代码有多少优化。

  1. List<T>string都是参考类型。因此,无论如何,在方法调用期间,它们不会被推入堆栈。因此标记它们ref参数不应该产生任何积极影响。

    private void SyncImages(ref List<dynamic> WeightEntitiesToSync, ref string imagesPath, ref string weibridgeName)
    
  2. 同样,List<T>是引用类型,而两个int变量是值类型。所以可以通过不将它们推到堆栈上来获得一些好处,但它是无关紧要的。

    private void SyncWeighEntityDataToLocalDB(ref List<dynamic> WeightEntitiesToSync, ref int lastTicketNo, ref int weighBridgeID)
    
  3. 总结一下,好处可能是微不足道的,你最好把时间花在更关键和紧迫的问题上,而不是太努力去优化它。