foreach & for
They are abit confusing for me, in Hashtable(), ArrayList() and List<T>.
private Hashtable m_Items = new Hashtable();
private ArrayList m_Mobiles = new ArrayList();
private List<object> m_ItemsNMobiles = new List<object>();
They are abit confusing for me, in Hashtable(), ArrayList() and List<T>.
private Hashtable m_Items = new Hashtable();
private ArrayList m_Mobiles = new ArrayList();
private List<object> m_ItemsNMobiles = new List<object>();
- As for the above, is there any preference that whether foreach or for shall be used?
- I notice ( perhaps I am wrong? ), sometimes it doesn't need guard from null reference, some other times however, guarding from null reference is needed. So when and why it is needed or not needed.
PHP:
if ( m_Items != null && m_Items.Count > 0 ) // null reference guard
{
for ( int i = 0; i < m_Items.Count; i++ )
{
}
}
if ( m_Items != null && m_Items.Count > 0 ) // null reference guard
{
foreach ( Item item in m_Items.Values )
{
}
}