Какая версия метода лучше, статическая или обычная?
Какая версия метода лучше?
public Anchor FindEmptyAnchor0()
{
for (int i = 0; i < anchors.Length; i++)
{
if (anchors[i].transform == null)
continue;
if (anchors[i].attachedTile == null)
return anchors[i];
}
return null;
}
или же эта?
public static Anchor FindEmptyAnchorStatic(VoxelTile tile)
{
for (int i = 0; i < tile.anchors.Length; i++)
{
if (tile.anchors[i].transform == null)
continue;
if (tile.anchors[i].attachedTile == null)
return tile.anchors[i];
}
return null;
}
В плане функционала, как я понял особой разницы нету, вопрос наверное только в красоте, вот так примерно я хотел использовать эти методы
public VoxelTile AddTile(VoxelTile basic)
{
basic.FindEmptyAnchor().SomeMethod();
VoxelTile secondary;
VoxelTile.FindEmptyAnchorStatic(secondary).SomeMethod();
// дальше что там сделать
}
Ответы (1 шт):
Автор решения: Aziz Umarov
→ Ссылка
Данная возможность - спецификация языка. Использовать вам, либо нет, зависит от задач, решаемых вами. Но, так уж случилось, что статичные методы и поля требуют другого подхода к использованию.
public Anchor FindEmptyAnchor0()
{
for (int i = 0; i < anchors.Length; i++)
{
if (anchors[i].transform != null && anchors[i].attachedTile == null)
return anchors[i];
}
return null;
}
А по-хорошему вам не нужен никакой метод
Anchors[] anchors = ...;
Anchor first = anchors.FirstOrDefault(anchor => anchors.transform != null && anchors.attachedTile == null);
FirstOrDefault - The default value for reference and nullable types is null.