You need a ref parameter, not an out parameter, since you both take a value and set a new value.
int iCount = 0; getFileCount(_dirPath, ref iCount); private void getFileCount(string _path, ref int iCount ) { try {
even better, don't use parameters at all.
private int getFileCount(string _path) { int count = Directory.GetFiles(_path).Length; foreach (string subdir in Directory.GetDirectories(_path)) count += getFileCount(subdir); return count; }
and even better than that, donβt create a function to do something already built-in framework.
int count = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length
and we are not improving ... do not lose space and loops by creating an array of files when you only need length. List them instead.
int count = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Count();
source share