Lengthとcount 違い

フォルダ内ののファイルを全て取得(getfilesメソッドで取得)し、フォルダ内のファイル存在有無判定をしようと考えています。

Length=0とcount=0どちらを使おうか悩んでいるのですが、この二つの違いってなんでしょうか

Hi @natsumikan

Length: This property is typically used with arrays. It returns the total number of elements in the array.

Example:

array.Length

Count: This property is used with collections such as lists, dictionaries, or enumerable types. It returns the number of elements in the collection.

Example:

list.Count

In your case, if you’re using GetFiles method in UiPath, it returns an array of strings representing the files in the specified directory. Therefore, you should use .Length to get the number of files in the folder.

Example in UiPath:

arrFiles = Directory.GetFiles("C:\YourFolderPath")
If arrFiles.Length > 0 Then
    // Files exist
Else
    // No files exist
End If

Hope it helps!!

こんにちは

結論から言うと、今回の例のように対象が配列の場合はLengthの方が良いと思います。
それぞれ、Array.Lengthプロパティと、Enumerable.Countメソッドになりますが、Lengthの方が値の取得コストが小さい=軽いと思います。
なお対象がList<T>の場合はCountプロパティがあるので、こちらを使います。

ちなみに、配列の要素数をチェックするのであれば、条件式として

arrFile.Any()

のように記述することも可能です。(上記は配列に1つ以上要素があれば、Trueが返ります)