C#中foreach中获取的数字相加
Addition of numbers obtained in foreach in C#
用下面的代码,我会分别收到所有Windows个硬盘的volume
foreach (var drive in DriveInfo.GetDrives())
{
int bb = Convert.ToInt32(drive.TotalSize / 1024 / 1024 / 1024);
}
这就是 returns
100GB
500GB
2300GB
但是我想集数交上去
但这就是我想要的
100GB + 500GB + 2300GB
2900GB
这样你就可以得到所有磁盘的字节容量:
int byteTotalCapacity = 0;
foreach (var drive in DriveInfo.GetDrives())
{
byteTotalCapacity += drive.TotalSize;
}
如果你想要以 GB 为单位,你必须除以 (1024*1024)
您可以 test/run 此代码 https://dotnetfiddle.net/27YcIp
using System.Linq;
using System.IO;
using System;
Console.WriteLine(Drives.GetAllDriveSizeA());
Console.WriteLine(Drives.GetAllDriveSizeB());
Console.WriteLine(Drives.GetAllDriveSizeC());
Console.WriteLine(Drives.GetAllDriveSizeD());
Console.ReadLine();
public static class Drives
{
public static long GetAllDriveSizeA() // With Linq
{
return DriveInfo.GetDrives().Where(d => d.IsReady).Sum(d => d.TotalSize / 1024 / 1024 / 1024);
}
public static long GetAllDriveSizeB()
{
long total = 0;
foreach (DriveInfo drive in DriveInfo.GetDrives()) //With foreach loop
{
if (drive.IsReady)
{
total += drive.TotalSize / 1024 / 1024 / 1024;
}
}
return total;
}
public static long GetAllDriveSizeC()//With for loop
{
long total = 0;
DriveInfo[] drives = DriveInfo.GetDrives();
for (int i = 0; i < drives.Length; i++)
{
if (drives[i].IsReady)
{
total += drives[i].TotalSize / 1024 / 1024 / 1024;
}
}
return total;
}
public static long GetAllDriveSizeD() //With List<T>.Foreach
{
long total = 0;
DriveInfo.GetDrives().ToList().ForEach(drive =>
{
if (drive.IsReady)
{
total += drive.TotalSize / 1024 / 1024 / 1024;
}
});
return total;
}
}
//DriveInfo.GetDrives() - Get Drive list.
//Where(d => d.IsReady) - filters only drives(elements) that are ready otherwise will get exception when trying to get total size.
//Sum(d => d.TotalSize / 1024 / 1024 / 1024) - Sum each element / 1024 / 1024 / 1024
//d => .... Is lambda expression (anonymous function). d is the parameter of the function and .... is the value returned by this function.
输出我的电脑
1360
1360
1360
1360
参考资料
Lambda expressions
Linq - Where
Linq - Sum
List.Foreach
用下面的代码,我会分别收到所有Windows个硬盘的volume
foreach (var drive in DriveInfo.GetDrives())
{
int bb = Convert.ToInt32(drive.TotalSize / 1024 / 1024 / 1024);
}
这就是 returns
100GB 500GB 2300GB
但是我想集数交上去 但这就是我想要的 100GB + 500GB + 2300GB
2900GB
这样你就可以得到所有磁盘的字节容量:
int byteTotalCapacity = 0;
foreach (var drive in DriveInfo.GetDrives())
{
byteTotalCapacity += drive.TotalSize;
}
如果你想要以 GB 为单位,你必须除以 (1024*1024)
您可以 test/run 此代码 https://dotnetfiddle.net/27YcIp
using System.Linq;
using System.IO;
using System;
Console.WriteLine(Drives.GetAllDriveSizeA());
Console.WriteLine(Drives.GetAllDriveSizeB());
Console.WriteLine(Drives.GetAllDriveSizeC());
Console.WriteLine(Drives.GetAllDriveSizeD());
Console.ReadLine();
public static class Drives
{
public static long GetAllDriveSizeA() // With Linq
{
return DriveInfo.GetDrives().Where(d => d.IsReady).Sum(d => d.TotalSize / 1024 / 1024 / 1024);
}
public static long GetAllDriveSizeB()
{
long total = 0;
foreach (DriveInfo drive in DriveInfo.GetDrives()) //With foreach loop
{
if (drive.IsReady)
{
total += drive.TotalSize / 1024 / 1024 / 1024;
}
}
return total;
}
public static long GetAllDriveSizeC()//With for loop
{
long total = 0;
DriveInfo[] drives = DriveInfo.GetDrives();
for (int i = 0; i < drives.Length; i++)
{
if (drives[i].IsReady)
{
total += drives[i].TotalSize / 1024 / 1024 / 1024;
}
}
return total;
}
public static long GetAllDriveSizeD() //With List<T>.Foreach
{
long total = 0;
DriveInfo.GetDrives().ToList().ForEach(drive =>
{
if (drive.IsReady)
{
total += drive.TotalSize / 1024 / 1024 / 1024;
}
});
return total;
}
}
//DriveInfo.GetDrives() - Get Drive list.
//Where(d => d.IsReady) - filters only drives(elements) that are ready otherwise will get exception when trying to get total size.
//Sum(d => d.TotalSize / 1024 / 1024 / 1024) - Sum each element / 1024 / 1024 / 1024
//d => .... Is lambda expression (anonymous function). d is the parameter of the function and .... is the value returned by this function.
输出我的电脑
1360
1360
1360
1360
参考资料
Lambda expressions
Linq - Where
Linq - Sum
List.Foreach