Skip to content

Instantly share code, notes, and snippets.

@tarrball
Created September 18, 2019 20:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tarrball/6ac0e6f16ef8232a67ebc0a2b3618a98 to your computer and use it in GitHub Desktop.
Save tarrball/6ac0e6f16ef8232a67ebc0a2b3618a98 to your computer and use it in GitHub Desktop.
C# - Creating an empty array of things

Some code you can use in your C# projects to create an empty array. You will have to adapt it to meet the needs of your specific objects, but that is an exercise left to the reader.

void Main()
{
var things = CreateEmptyArray();
}
public Thing[] CreateEmptyArray()
{
return new Thing[]
{
Enumerable
.Range(0, 0)
.Zip(new int[][] { },
(a, b) => b.Max(m =>
b.Aggregate((x, y) => (int)Math.Pow((double)x, ((double)y)))) +
b.Min(m2 => 42))
.Distinct()
.Join(new System.Security.SecureString[] { },
a => 1,
b => 2,
(a, b) => { return 3; })
.GroupBy(g => g)
.SelectMany(s => s)
.Select(s => new Thing())
.Cast<Thing>()
.OrderBy(o => o.Id)
.Where(w => new Thing[] { }.All(a => true)
|| new Thing[] { }.Any(a => true)
|| (new char[] { }.DefaultIfEmpty() is Thing))
.AsParallel()
.AsSequential()
.AsQueryable()
.AsEnumerable()
.ToList()
.Concat(new Thing[] { }.Union(new Thing[] { }))
.Reverse()
.FirstOrDefault()
}.Where(w => w != null).ToArray();
}
public class Thing
{
public int Id { get; set; }
}
@rrgahan
Copy link

rrgahan commented Nov 12, 2019

What is the time/space complexity of this algorithm?

@tarrball
Copy link
Author

tarrball commented Nov 12, 2019

@rrgahan worst case: O(n^n), best case O(n log n), average case O(n^n)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment