你可以如何在C#中检查类中的每个对象变量?

huangapple go评论67阅读模式
英文:

How can you check every objects variable in a class in C#?

问题

public class Enemies
{
    public char livingCondition { get; set; }

    public Enemies(char livingCondition)
    {
        this.livingCondition = livingCondition;
    }
}

Enemies Enemy00 = new Enemies('A');
Enemies Enemy01 = new Enemies('A');
Enemies Enemy02 = new Enemies('A');

这些是已定义的对象/敌人(它们除了名称以外都是相同的)。

例如,在这个类中,如果你想要检查每个对象的livingCondition,并且如果它们都死亡('D'),则应用程序关闭,你该如何做到这一点?

我尝试过将类转换为列表,然后使用for循环搜索 `Enemies.livingCondition == 'A'`,但那行不通,

我是C#的新手,所以我没有其他想法。

任何帮助将不胜感激 :)

编辑/附加信息:我正在开发一个游戏,实际上我需要的是living condition为A或D,基本上如果所有敌人都死了,计时器应该停止,我知道对于数组,例如,你可以使用循环来查找整个数组是否只有一个字符,所以我想知道是否可以对类对象进行类似的操作。

总结:我需要检查我拥有的所有对象是否都具有属性 .livingCondition = 'D'
英文:
public class Enemies
{
	public livingCondition { get; set; }

	public Enemies(char livingCondition)
	{
		this.livingCondition = livingCondition;
	}
}

Enemies Enemy00 = new Enemies('A');
Enemies Enemy01 = new Enemies('A');
Enemies Enemy02 = new Enemies('A');

These are some of the defined Objects/Enemies (they are all identical other than their names).

In this class for example if you want to check every Object for their livingCondition and if they are all dead ('D') the App closes, how can you do that?

I have tried turning the class into a list and using for loops to search for Enemies.livingCondition == 'A' and that didn't work,

I'm new to C# so I don't have any other ideas.

Any help would be greatly appreciated 你可以如何在C#中检查类中的每个对象变量?

Edit/additional info: I am developing a game and what I was actually needing was living condition A or D and basically if all the enemies are dead the timer should stop, and i know that for arrays for example you can use loops to find out if the whole array only had one char for example, so i was wondering if something similar was possible for class objects.

Tldr: I need to check if all the objects I have all have the attribute .livingCondition = 'D'.

答案1

得分: 1

你需要将敌人保存在一个容器中,最好是一个列表:

public class Enemy
{
    public char LivingCondition { get; set; }

    public Enemy(char livingCondition)
    {
        LivingCondition = livingCondition;
    }
}

// 在其他地方
List<Enemy> enemies = new();
enemies.Add(new('A'));
enemies.Add(new('A'));
enemies.Add(new('A'));
enemies.Add(new('C'));
enemies.Add(new('B'));

然后,您可以使用 System.Linq 以各种方式查询此列表。例如,如果您想要获取 LivingCondition 等于 A 的敌人:

var enemiesThatLive = enemies.Where(e => e.LivingCondition == 'A').ToList();

如果您想要检查列表中的所有元素是否满足条件:

bool allDead = enemies.All(e => e.LivingCondition == 'D');

如果您想要检查列表中是否至少有一个元素是活着的:

bool atLeastOneAlive = enemies.Any(e => e.LivingCondition == 'A');
英文:

You need to keep the enemies in a container, preferably a list:

public class Enemy
{
    public char LivingCondition { get; set; }

    public Enemy(char livingCondition)
    {
        LivingCondition = livingCondition;
    }
}

// in somewhere else
List&lt;Enemy&gt; enemies = new();
enemies.Add(new(&#39;A&#39;));
enemies.Add(new(&#39;A&#39;));
enemies.Add(new(&#39;A&#39;));
enemies.Add(new(&#39;C&#39;));
enemies.Add(new(&#39;B&#39;));

Then you can query this list in a variety of ways using System.Linq. For example, if you want to get the enemies with LivingCondition equals to A:

var enemiesThatLive = enemies.Where(e =&gt; e.LivingCondition == &#39;A&#39;).ToList();

If you want to check if all the elements in a list satisfy a condition:

bool allDead = enemies.All(e =&gt; e.LivingCondition == &#39;D&#39;);

If you want to check if there is at least one element that is alive in the list:

bool atLeastOneAlive = enemies.Any(e =&gt; e.LivingCondition == &#39;A&#39;);

huangapple
  • 本文由 发表于 2023年6月18日 22:49:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定