2008-10-07 9 views
29

हर बार जब मैं कुछ सी # मैं इस कोडसमय की नकल करने की कोई संभावना() सी # में रूबी विधि?

for (int i = 0; i < N; i++) 
{ 
    ... 
} 

रूबी अध्ययन मैं विधि के बारे में सीखा है लिखने बार() जो की तरह एक ही अर्थ विज्ञान के साथ इस्तेमाल किया जा सकता का उपयोग कर एक एल्गोरिथ्म के अंदर एन बार करने की ज़रूरत है सी # में इस

N.times do 
    ... 
end 

कोड टुकड़ा और अधिक जटिल लग रहा है और हम बेकार चर घोषित करना चाहिए मैं

मैं विस्तार विधि है जो IEnumerable रिटर्न लिखने का प्रयास किया है, लेकिन क्योंकि फिर मैं एक चक्र चर मैं घोषित करने के लिए है मैं परिणाम से संतुष्ट नहीं हूं।

public static class IntExtender 
{ 
    public static IEnumerable Times(this int times) 
    { 
     for (int i = 0; i < times; i++) 
      yield return true; 
    } 
} 

... 

foreach (var i in 5.Times()) 
{ 
    ... 
} 

यह एन बार चक्र और अधिक सुरुचिपूर्ण बनाने के लिए कुछ नए सी # 3.0 भाषा सुविधाओं का उपयोग संभव है?

उत्तर

48

एक थोड़ा cvk's answer के संस्करण खनन:

public static class Extensions 
{ 
    public static void Times(this int count, Action action) 
    { 
     for (int i=0; i < count; i++) 
     { 
      action(); 
     } 
    } 

    public static void Times(this int count, Action<int> action) 
    { 
     for (int i=0; i < count; i++) 
     { 
      action(i); 
     } 
    } 
} 

उपयोग:

5.Times(() => Console.WriteLine("Hi")); 
5.Times(i => Console.WriteLine("Index: {0}", i)); 
14

यह वास्तव में के साथ संभव है सी # 3.0:

public interface ILoopIterator 
{ 
    void Do(Action action); 
    void Do(Action<int> action); 
} 

private class LoopIterator : ILoopIterator 
{ 
    private readonly int _start, _end; 

    public LoopIterator(int count) 
    { 
     _start = 0; 
     _end = count - 1; 
    } 

    public LoopIterator(int start, int end) 
    { 
     _start = start; 
     _end = end; 
    } 

    public void Do(Action action) 
    { 
     for (int i = _start; i <= _end; i++) 
     { 
      action(); 
     } 
    } 

    public void Do(Action<int> action) 
    { 
     for (int i = _start; i <= _end; i++) 
     { 
      action(i); 
     } 
    } 
} 

public static ILoopIterator Times(this int count) 
{ 
    return new LoopIterator(count); 
} 

उपयोग:

int sum = 0; 
5.Times().Do(i => 
    sum += i 
); 

आप .NET 3.5 का उपयोग कर रहे हैं तो आप विस्तार विधि का उपयोग कर सकते हैं बेशर्मी से http://grabbagoft.blogspot.com/2007/10/ruby-style-loops-in-c-30.html

0

चोरी प्रत्येक इस आलेख में प्रस्तावित है, और क्लासिक पाश से बचने के लिए इसका इस्तेमाल करें।

public static class IEnumerableExtensions 
    { 
     public static void Each<T>(
     this IEnumerable<T> source, 
     Action<T> action) 
     { 
      foreach(T item in source) 
      { 
       action(item); 
      } 
     } 
    } 

यह विशेष रूप से विस्तार विधि स्थान वेल्ड एक चीज़ पर प्रत्येक विधि कि IEnumerable लागू करता है। आप जानते हैं क्योंकि पर पहला पैरामीटर यह विधि परिभाषित करता है कि यह विधि शरीर के अंदर क्या होगा। एक्शन एक प्री-डिफ़ाइंड क्लास है जो मूल रूप से फ़ंक्शन (प्रतिनिधि) के लिए कोई मूल्य नहीं लौटाता है। विधि के अंदर, वह जगह है जहां तत्वों को सूची से निकाला जाता है। इस विधि को सक्षम बनाता है मेरे लिए कोड की एक पंक्ति में फ़ंक्शन को साफ़ करने के लिए सक्षम है।

(http://www.codeproject.com/KB/linq/linq-to-life.aspx)

आशा इस मदद करता है।

+0

मैं वास्तव में क्या चाहते थे, लेकिन उपयोगी भी: आप कोड यहाँ प्राप्त कर सकते: https://github.com/Razorclaw/Ext.NET

कोड बहुत जॉन स्कीट जवाब के समान है। धन्यवाद! –

0

मैं अपने खुद के विस्तार है कि पूर्णांक (प्लस कुछ अन्य सामान) के लिए टाइम्स जोड़ने लिखा था।

public static class IntegerExtension 
{ 
    public static void Times(this int n, Action<int> action) 
    { 
     if (action == null) throw new ArgumentNullException("action"); 

     for (int i = 0; i < n; ++i) 
     { 
      action(i); 
     } 
    } 
} 
संबंधित मुद्दे