Click here to Skip to main content
15,885,244 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
harold aptroot17-Sep-10 8:12
harold aptroot17-Sep-10 8:12 
GeneralRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
Yanshof17-Sep-10 8:19
Yanshof17-Sep-10 8:19 
GeneralRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
harold aptroot17-Sep-10 8:22
harold aptroot17-Sep-10 8:22 
GeneralRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
Yanshof17-Sep-10 8:28
Yanshof17-Sep-10 8:28 
GeneralRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
harold aptroot17-Sep-10 8:51
harold aptroot17-Sep-10 8:51 
AnswerRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
Ennis Ray Lynch, Jr.17-Sep-10 9:30
Ennis Ray Lynch, Jr.17-Sep-10 9:30 
GeneralRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
Yanshof17-Sep-10 10:19
Yanshof17-Sep-10 10:19 
AnswerRe: How to force the Generic Class to get only Value type ( like Int32 or double ) ? Pin
Paul Michalik18-Sep-10 1:04
Paul Michalik18-Sep-10 1:04 
This won't work, at least not in the way you want it... You are writing a numeric algorithm which should work upon different 'arithmetic' types and want to utilize arithmetic operators on T, right? This is not possible. There is no common base type for all 'numerics' which implements the appropriate operators (operators must be defined static on the type which implements them). Each CLR numeric type is implemented as a structure without common base (which is not supported for structures anyway), ergo you cannot restrict a type parameter in a generic to something which does not exists. The numeric types implement various interfaces, but these cannot implement operators since these must be static. So you are stuck, for the moment.

The only clean way (without CLR sourcery) is to define this non-existent common base:
public abstract class NumericBase<T> where T : struct {
        public static NumericBase<T> operator + (NumericBase<T> pA, NumericBase<T> pB) {
            return pA.Add(pB);
        }
        public static NumericBase<T> operator - (NumericBase<T> pA, NumericBase<T> pB) {
            return pA.Sub(pB);
        }
        public abstract T Value { get; set; }
        public abstract NumericBase<T> Add(NumericBase<T> pOther);
        public abstract NumericBase<T> Sub(NumericBase<T> pOther);
    }


Specialize for int:

public class NumericInt : NumericBase<int> {
        public NumericInt() : this(0) {}

        public NumericInt(int pValue) {
            Value = pValue;
        }
        public override int Value { get; set; }
        public override NumericBase<int> Add(NumericBase<int> pOther) {
            return new NumericInt(Value + pOther.Value);
        }
        public override NumericBase<int> Sub(NumericBase<int> pOther) {
            return new NumericInt(Value - pOther.Value);
        }
    }


And you can 'Foo':

public class MySummator<T> 
        where T : struct {

        public NumericBase<T> Result { get; set; }

        public void Foo(NumericBase<T> pParameter) {
            Result = Result + pParameter;
        }


Clearly, you have a lot of overhead with virtual calls, so I would not use it that way in situations where performance is critical. On the other hand, you could use a bit of CLR sourcery here:
1. Write your algorithms using NumericBase<T>. You can run and test them and you are maintaing one generic common code base.
2. For real world usage System.Emit code, which is structurally identical to the generic NumericBase implementation, but uses concrete numeric types i.e. you replace NumericBase<int> with int and so on.
Questionwindows service username Pin
tomorrow_ft17-Sep-10 1:46
tomorrow_ft17-Sep-10 1:46 
AnswerRe: windows service username Pin
Calla17-Sep-10 2:09
Calla17-Sep-10 2:09 
AnswerRe: windows service username Pin
Ennis Ray Lynch, Jr.17-Sep-10 3:45
Ennis Ray Lynch, Jr.17-Sep-10 3:45 
AnswerRe: windows service username Pin
Mycroft Holmes17-Sep-10 4:58
professionalMycroft Holmes17-Sep-10 4:58 
QuestionOK, how would I do this in c# please Pin
stephen.darling16-Sep-10 12:34
stephen.darling16-Sep-10 12:34 
AnswerRe: OK, how would I do this in c# please Pin
harold aptroot16-Sep-10 12:40
harold aptroot16-Sep-10 12:40 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 12:46
stephen.darling16-Sep-10 12:46 
AnswerRe: OK, how would I do this in c# please [modified] Pin
Luc Pattyn16-Sep-10 13:24
sitebuilderLuc Pattyn16-Sep-10 13:24 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 13:47
stephen.darling16-Sep-10 13:47 
GeneralRe: OK, how would I do this in c# please Pin
Luc Pattyn16-Sep-10 13:58
sitebuilderLuc Pattyn16-Sep-10 13:58 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 14:13
stephen.darling16-Sep-10 14:13 
GeneralRe: OK, how would I do this in c# please Pin
Luc Pattyn16-Sep-10 14:19
sitebuilderLuc Pattyn16-Sep-10 14:19 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 14:31
stephen.darling16-Sep-10 14:31 
GeneralRe: OK, how would I do this in c# please Pin
Luc Pattyn16-Sep-10 14:51
sitebuilderLuc Pattyn16-Sep-10 14:51 
GeneralRe: OK, how would I do this in c# please Pin
AspDotNetDev16-Sep-10 15:46
protectorAspDotNetDev16-Sep-10 15:46 
AnswerRe: OK, how would I do this in c# please Pin
PIEBALDconsult16-Sep-10 16:15
mvePIEBALDconsult16-Sep-10 16:15 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 16:50
stephen.darling16-Sep-10 16:50 

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.