Click here to Skip to main content
15,891,888 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have problem with foreach. The code inside foreach doesn't run, im put a Console WriteLine inside and it doesn't show up.

C#
List<string> targetStatus = properties.GetTargetStatus(); //This give not null value


C#
foreach (string status in targetStatus)
            {
                Console.WriteLine("Hello boss!"); //This message doesn't show up
            }


After run console application my code inside foreach doesn't show. someone can explain or give solutions for me?

What I have tried:

All i try at above....... And i dont know why :(, if List of targetStatus count = 0 the foreach will run?
Posted
Updated 12-Dec-16 23:55pm

The most obvious reason is because there are no items in the collection.
Use the debugger: put a breakpoint on the line
C#
foreach (string status in targetStatus)
And run your code in the debugger. When it reaches the line, it will stop and hand control to you. You can then examine variable contents, single step lines, and so forth.
If it is an empty collection, then it's up to you to find out why: put another breakpoin on the line
C#
List<string> targetStatus = properties.GetTargetStatus();
And run your code in the debugger again. This time, when it reaches the line step into the method and follow closely what happens there.
 
Share this answer
 
Comments
F. Xaver 13-Dec-16 5:49am    
5ed
Quote:
if List of targetStatus count = 0 the foreach will run?
No, of course. You cannot enumerate an empty set. What would be the value of status?
 
Share this answer
 
Comments
F. Xaver 13-Dec-16 5:50am    
5ed
CPallini 13-Dec-16 5:54am    
Thank you.
EADever 13-Dec-16 7:29am    
`properties.GetTargetStatus();` get List<string> value from [XmlAttribute("")]
targetStatus may not be null, but it doesn't necessarily mean that there are any items in the collection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900