我试图检查allinterconnectmembers是否为空。 [英] I tried to check if allinterconnectmembers is empty or not.
问题描述
var InterconnectDictList = new List< IDictionary< string,string>>(){};
var allinterconnectMembers = getAllData(https://+ con.oneViewIP +/ rest /互连,con)[成员];
if(!string.IsNullOrEmpty(allinterconnectMembers.ToString()))
// if(allinterconnectMembers!= null)
{
我尝试过:
当我试图在allinterconnectMembers中没有任何东西时检查这一行,然后,它也进入循环。
if(!string.IsNullOrEmpty(allinterconnectMembers.ToString()))
// if(allinterconnectMembers!=
{
请帮助我。
< blockquote>首先,这一行:
if (!string.IsNullOrEmpty(allinterconnectMembers.ToString() ))
永远不会为空:默认的ToString实现f或类返回该类的全名。因此,除非您的类实现ToString以覆盖该行为,否则返回值将始终是异常(如果它为null,因为尝试在null上调用ToString将在它到达IsNullOrEmpty之前失败)或非空字符串。
我建议您检查null,然后使用Count方法(假设您的类基于标准.NET集合):
if (allinterconnectMembers!= null && allinterconnectMembers.Count ()> 0 )
var InterconnectDictList = new List<IDictionary<string, string>>() { };
var allinterconnectMembers = getAllData("https://" + con.oneViewIP + "/rest/interconnects", con)["members"];
if (!string.IsNullOrEmpty(allinterconnectMembers.ToString()))
//if(allinterconnectMembers!=null)
{
What I have tried:
when i tried to check this line when nothing was in allinterconnectMembers , then, also, it is getting in the loop.
if (!string.IsNullOrEmpty(allinterconnectMembers.ToString()))
//if(allinterconnectMembers!=
{
please help me.
First off, this line:
if (!string.IsNullOrEmpty(allinterconnectMembers.ToString()))
Will never be empty: the default ToString implementation for a class returns the full name of that class. So unless your class implements ToString to override that behaviour the return value will always be an exception (if it's null, because the attempt to call ToString on a null will fail before it gets to IsNullOrEmpty) or a non-blank string.
I'd suggest that you check for null, and then use the Count method (assuming your class is based on a standard .NET collection):
if (allinterconnectMembers != null && allinterconnectMembers.Count() > 0)
这篇关于我试图检查allinterconnectmembers是否为空。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!