我正在从一本书中学习C#,我正在扩展一个例子,以便更好地理解语法.
我正在尝试使用以下代码循环遍历一组对象并仅选择某些对象,以便我可以将它们加载到一个单独的数组中.我现在正在努力解决这个问题:
if (animalCollection[i].Equals(Chicken))
Run Code Online (Sandbox Code Playgroud)
这是Program.cs的完整代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex02
{
class Program
{
static void Main(string[] args)
{
Animals animalCollection = new Animals();
animalCollection.Add(new Cow("Jack"));
animalCollection.Add(new Chicken("Vera"));
animalCollection.Add(new Chicken("Sally"));
Animal[] birds = new Animal[2];
for (int i = 0; i < animalCollection.Count; i++)
{
if (animalCollection[i].Equals(Chicken))
birds[i] = animalCollection[i];
}
foreach (Animal myAnimal in animalCollection)
{
myAnimal.Feed();
}
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是只将对象类型Chicken加载到一个名为birds的新数组中.
这是Animal类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex02
{
public abstract class Animal
{
protected string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public Animal()
{
name = "The animal with no name";
}
public Animal(string newName)
{
name = newName;
}
public void Feed()
{
Console.WriteLine("{0} has been fed." , name);
}
internal bool equals()
{
throw new NotImplementedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是鸡类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex02
{
public class Chicken : Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg." , name);
}
public Chicken(string newName): base(newName)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里是动物类的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex02
{
public class Animals : CollectionBase
{
public void Add(Animal newAnimal)
{
List.Add(newAnimal);
}
public void Remove(Animal newAnimal)
{
List.Remove(newAnimal);
}
public Animals()
{
}
public Animal this[int animalIndex]
{
get
{
return (Animal)List[animalIndex];
}
set
{
List[animalIndex] = value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本面
要确定某个对象是否属于给定类型,您可以使用typeof或is
if (typeof(someObject) == typeof(Chicken))
Run Code Online (Sandbox Code Playgroud)
要么
if (someObject is Chicken)
Run Code Online (Sandbox Code Playgroud)
特别是在你的情况下
if (animalCollection[i].Equals(Chicken))
Run Code Online (Sandbox Code Playgroud)
变
if (typeof(animalCollection[i]) == typeof(Chicken))
Run Code Online (Sandbox Code Playgroud)
要么
if (animalCollection[i] is Chicken)
Run Code Online (Sandbox Code Playgroud)
您还可以像这样确定对象的类型
Type t = animalCollection[i].GetType();
Run Code Online (Sandbox Code Playgroud)
快速的方式
现在我已经介绍了它如何在基本级别上工作,这是使用Linq在一行中完成相同的方法
var chickens = animals.OfType<Chicken>().ToArray();
Run Code Online (Sandbox Code Playgroud)
顺便说说
如果您希望将类型名称作为字符串,则可以执行此操作
string typeName = t.FullName;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
207 次 |
| 最近记录: |