Entitas  0.40.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
CollectionExtension.cs
1 using System.Collections.Generic;
2 
3 namespace Entitas {
4 
5  public static class CollectionExtension {
6 
7  /// Returns the only entity in the collection.
8  /// It will throw an exception if the collection doesn't have
9  /// exactly one entity.
10  public static IEntity SingleEntity(this ICollection<IEntity> collection) {
11  if(collection.Count != 1) {
12  throw new SingleEntityException(collection.Count);
13  }
14 
15  return System.Linq.Enumerable.First(collection);
16  }
17 
18  /// Returns the only entity in the collection.
19  /// It will throw an exception if the collection doesn't have
20  /// exactly one entity.
21  public static TEntity SingleEntity<TEntity>(this ICollection<TEntity> collection) where TEntity : class, IEntity, new() {
22  if(collection.Count != 1) {
23  throw new SingleEntityException(collection.Count);
24  }
25 
26  return System.Linq.Enumerable.First(collection);
27  }
28  }
29 
31 
32  public SingleEntityException(int count) : base(
33  "Expected exactly one entity in collection but found " + count + "!",
34  "Use collection.SingleEntity() only when you are sure that there " +
35  "is exactly one entity.") {
36  }
37  }
38 }
static IEntity SingleEntity(this ICollection< IEntity > collection)
static TEntity SingleEntity< TEntity >(this ICollection< TEntity > collection)
Base exception used by Entitas.