Click here to Skip to main content
15,914,820 members
Home / Discussions / C#
   

C#

 
GeneralRe: Excel's MOD in C#? Pin
OriginalGriff18-Aug-19 22:28
mveOriginalGriff18-Aug-19 22:28 
GeneralRe: Excel's MOD in C#? Pin
BillWoodruff19-Aug-19 21:48
professionalBillWoodruff19-Aug-19 21:48 
AnswerRe: Excel's MOD in C#? Pin
BillWoodruff19-Aug-19 23:43
professionalBillWoodruff19-Aug-19 23:43 
SuggestionRe: Excel's MOD in C#? Pin
Richard Deeming20-Aug-19 1:39
mveRichard Deeming20-Aug-19 1:39 
GeneralRe: Excel's MOD in C#? Pin
BillWoodruff20-Aug-19 3:07
professionalBillWoodruff20-Aug-19 3:07 
GeneralRe: Excel's MOD in C#? Pin
Richard Deeming20-Aug-19 3:23
mveRichard Deeming20-Aug-19 3:23 
GeneralRe: Excel's MOD in C#? Pin
BillWoodruff20-Aug-19 4:42
professionalBillWoodruff20-Aug-19 4:42 
GeneralRe: Excel's MOD in C#? Pin
Richard Deeming20-Aug-19 5:27
mveRichard Deeming20-Aug-19 5:27 
Looks like it's part of a much larger discussion that's going to take a while to make it into the language (if it ever does):
Champion "Type Classes (aka Concepts, Structural Generic Constraints)" · Issue #110 · dotnet/csharplang · GitHub[^]

Meanwhile, you can "fake" it by using Jon Skeet's generic operators from the MiscUtil project:
Generic Operators[^]
It hasn't been updated since 2009, but the concept still works.

For example:
C#
using System.Linq.Expressions;

public static class GenericOperators<T>
{
    public static readonly Func<T, T, T> Add = Create(Expression.Add);
    public static readonly Func<T, T, T> Subtract = Create(Expression.Subtract);
    public static readonly Func<T, T, T> Multiply = Create(Expression.Multiply);
    public static readonly Func<T, T, T> Divide = Create(Expression.Divide);
    
    private static Func<T, T, T> Create(Func<Expression, Expression, BinaryExpression> body)
    {
        try
        {
            Type typeT = typeof(T);
            var left = Expression.Parameter(typeT, "left");
            var right = Expression.Parameter(typeT, "right");

            if (typeT.IsEnum)
            {
                Type enumType = Enum.GetUnderlyingType(typeT);
                var x = Expression.Convert(left, enumType);
                var y = Expression.Convert(right, enumType);

                Expression op = body(x, y);
                if (op.Type == enumType) op = Expression.Convert(op, typeT);

                return Expression.Lambda<Func<T, T, T>>(op, left, right).Compile();
            }

            return Expression.Lambda<Func<T, T, T>>(body(left, right), left, right).Compile();
        }
        catch (InvalidOperationException ex)
        {
            string message = ex.Message;
            return delegate { throw new InvalidOperationException(message); };
        }
        catch (ArgumentException ex)
        {
            string message = ex.Message;
            return delegate { throw new InvalidOperationException(message); };
        }
    }
}
Of course, it wouldn't help much in this case, because you'd still need to convert to a double or decimal to call Math.Floor. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

Questionc# Pin
Member 1456180618-Aug-19 5:22
Member 1456180618-Aug-19 5:22 
AnswerRe: c# Pin
OriginalGriff18-Aug-19 6:35
mveOriginalGriff18-Aug-19 6:35 
AnswerRe: c# Pin
#realJSOP21-Aug-19 1:38
professional#realJSOP21-Aug-19 1:38 
QuestionZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Rikus Marais14-Aug-19 2:33
Rikus Marais14-Aug-19 2:33 
AnswerRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
OriginalGriff14-Aug-19 4:08
mveOriginalGriff14-Aug-19 4:08 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Richard Deeming14-Aug-19 7:56
mveRichard Deeming14-Aug-19 7:56 
AnswerRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Gerry Schmitz15-Aug-19 4:02
mveGerry Schmitz15-Aug-19 4:02 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
OriginalGriff15-Aug-19 4:12
mveOriginalGriff15-Aug-19 4:12 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Richard Deeming15-Aug-19 4:33
mveRichard Deeming15-Aug-19 4:33 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Gerry Schmitz15-Aug-19 9:26
mveGerry Schmitz15-Aug-19 9:26 
QuestionHow i can convert perl scripts to exe file to use in c#? Pin
Member 1455624211-Aug-19 1:25
Member 1455624211-Aug-19 1:25 
AnswerRe: How i can convert perl scripts to exe file to use in c#? Pin
OriginalGriff11-Aug-19 1:28
mveOriginalGriff11-Aug-19 1:28 
AnswerRe: How i can convert perl scripts to exe file to use in c#? Pin
Richard MacCutchan11-Aug-19 3:32
mveRichard MacCutchan11-Aug-19 3:32 
QuestionCheck List Box en C# to select View Schedules in Revit from Visual Studio Pin
Member 145554449-Aug-19 9:44
Member 145554449-Aug-19 9:44 
QuestionParse arithmetics in ONE string, e.g. "2 + 5 x 1024 - (32 x 512 - 16 x 14) + 256"? Pin
arnold_w8-Aug-19 23:49
arnold_w8-Aug-19 23:49 
AnswerRe: Parse arithmetics in ONE string, e.g. "2 + 5 x 1024 - (32 x 512 - 16 x 14) + 256"? Pin
Eddy Vluggen9-Aug-19 0:19
professionalEddy Vluggen9-Aug-19 0:19 
GeneralRe: Parse arithmetics in ONE string, e.g. "2 + 5 x 1024 - (32 x 512 - 16 x 14) + 256"? Pin
arnold_w9-Aug-19 0:43
arnold_w9-Aug-19 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.