अक्सर मुझे एक लूप को कोड करना होता है जिसमें संग्रह में पहला आइटम विशेष मामला होता है, कोड कभी स्पष्ट नहीं होता है जैसा कि यह होना चाहिए।लूप लिखने के लिए साफ तरीका है जिसमें संग्रह में पहले आइटम के लिए विशेष तर्क है
सी # भाषा का एक नया स्वरूप छोटा, इन लूप को कोड करने का सबसे अच्छा तरीका क्या है?
// this is more code to read then I would like for such a common concept
// and it is to easy to forget to update "firstItem"
foreach(x in yyy)
{
if (firstItem)
{
firstItem = false;
// other code when first item
}
// normal processing code
}
// this code is even harder to understand
if (yyy.Length > 0)
{
//Process first item;
For(int I = 1; I <yyy.Length; i++)
{
// process the other items.
}
}
मैं यहां जे स्कीट को बुला रहा हूं :) –
मुझे वास्तव में कुछ भी गलत नहीं दिख रहा है (आपका पहला उदाहरण) एक बुलियन की जांच कर रहा है, जो इसे देखता है उसे जल्दी से पता चलेगा कि आप क्या कर रहे हैं। –