Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Length of any Collection in C#
//#mode simple
using System;
using System.Collections;
using System.IO;
//This is a joke. Don't use
//https://redd.it/7ch739
public static int Main(string[] Args)
{
Console.WriteLine(EnumerableSize(Args));
return 0;
}
/// <summary>
/// Gets the Size of **any** enumerable object
/// </summary>
/// <returns>Element Count of the enumerable collection</returns>
private static long EnumerableSize(IEnumerable A)
{
//Get Temporary Name
var D=Path.GetTempFileName();
//State of the Art File to Directory Conversion (TODO: Async)
File.Delete(D);
Directory.CreateDirectory(D);
//Loop over all items
foreach(var Item in A)
{
//Create file
var F=Path.GetTempFileName();
//Move to our Directory
File.Move(F,Path.Combine(D,Path.GetFileName(F)));
}
//Count Entries in the Directory
long Count = Directory.GetFiles(D).LongLength;
//Cleanup
Directory.Delete(D,true);
//Return Element Count
return Count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.