c# - How to Implement Generic Method to do Math calculations on different value types -


I have a method that accepts an IEnumerable-decimals and performs various math tasks. I have an IEnumerable- Int want to use the same method. How do I implement this? For example, find a simple amount?

  zero counting & lt; T & gt; (IEnumerable & lt; T & gt; ListOFNumbers) {int count = ListofNumbers.Count (); ? Yoga =?; }   

It is easily available in all operator category Provides access to general arithmetic; And there are general implementations (as extensions methods) of Sum , average , etc. - and additionally any type with the appropriate operator Works from antiquity So for example, you can use yoga, etc.

Note that it currently uses .NET 3.5; I had a 2.0 version somewhere, but it is not in the form of a test ....

A simplified example of yoga is shown in the following:

< Pre> Public Static T Yoga & lt; T & gt; (This is IEnumerable & lt; T & gt; source) {T sum = operator & lt; T & gt; .Zero; Foreign Currency (T Value in Source) {if (value! = Null) {sum = Operator.Add (sum, value); }} Refund Amount; }

However the IIRC real implementation is slightly more ...

Note one side, dynamic (In .NET 4.0 / C # 4.0) Supports operators, but we have to wait for beta what it does. By my last appearance on dynamic in CTP, I hope this is slightly slower than MiscUtil code, but we will see.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -