问题描述
我在< List>中有一个不同对象的列表结构体。每种物品只有一个类别。
目前我有以下几种方法:
public static Flag GetFlagObj()
{
foreach(Thingy s in _Thingies)
{
if(s是国旗)
返回(旗帜)s;
}
返回null;
}
public static Cloth GetFlagObj()
{
foreach(Thingy s in _Thingies)
{
if(s is Cloth)
return(Cloth)s;
}
返回null;
}
公共静态BedSprd GetFlagObj()
{
foreach(Thingy s in _Thingies)
{
if(s是BedSprd)
return(BedSprd)s;
}
返回null;
}
有没有办法我可以使用一个通用方法将
参数作为我希望返回的项目?
感谢您的帮助。
Al
Hi,
I have a list of different objects in a <List> Structure. There is only
one category of each kind of object.
Current I have the following methods:
public static Flag GetFlagObj()
{
foreach (Thingy s in _Thingies)
{
if (s is Flag)
return (Flag)s;
}
return null;
}
public static Cloth GetFlagObj()
{
foreach (Thingy s in _Thingies)
{
if (s is Cloth)
return (Cloth)s;
}
return null;
}
public static BedSprd GetFlagObj()
{
foreach (Thingy s in _Thingies)
{
if (s is BedSprd)
return (BedSprd)s;
}
return null;
}
Is there a way I could have one generic method that would take as a
parameter the item I wish returned?
Thank you for your help.
Al
推荐答案
这篇关于返回对象引用的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!