locked
Typedef in C# RRS feed

  • Question

  • How can the same effect as of C++'s typedef be achieved in C#?
    For instance, I want my assembly to be able to compile either with float or with double data types for the real number calculations, as a means of finding the correct tradeoff of memory consumption/speed and accuracy.
    I guess I probably can't without deriving a type from System.ValueType and whenever I want to change the actual variable I have to replace everything in that class - Or - making the classes generic.
    Friday, February 24, 2006 12:39 PM

Answers

  • Another way is to use the using directive like this:


    #if TYPE_FLOAT
    using TReal = System.Single; // (equal to float)
    #else
    using TReal = System.Double;
    #endif
     
    public class MyClass {
        TReal number = (TReal) 1.1;
        // ...
    }

     

    This allows you to use TReal in place of the actual type. Note that this may give some problems with literals; you need to either cast those to TReal or use float literals all the time.

    Monday, February 27, 2006 10:18 AM
  • Correct System.ValueType (amongst a few others) can not be derived from directly.  Even if you could it wouldn't have the result you want.  Unfortunately C# doesn't support typedefs at this time.  I don't believe generics would give you what you want.  You could change the type back and forth but it would clutter your interface and wouldn't prevent you from using char, int or anything else.  Even #define won't work in this case.  Your only other choice is to define 2 versions of every method that needs to differentiate between the 2 types.  Each method can have the same name provided you wrap it in a #define.  Unfortunately the ConditionalAttribute won't help here since you don't want both methods visible at the same time (it wouldn't compile).

    #if TYPE_FLOAT
       float Foo ( )
    #else
       double Foo ( )
    #endif

    Michael Taylor - 2/24/06

    Friday, February 24, 2006 2:07 PM
    Moderator

All replies

  • Correct System.ValueType (amongst a few others) can not be derived from directly.  Even if you could it wouldn't have the result you want.  Unfortunately C# doesn't support typedefs at this time.  I don't believe generics would give you what you want.  You could change the type back and forth but it would clutter your interface and wouldn't prevent you from using char, int or anything else.  Even #define won't work in this case.  Your only other choice is to define 2 versions of every method that needs to differentiate between the 2 types.  Each method can have the same name provided you wrap it in a #define.  Unfortunately the ConditionalAttribute won't help here since you don't want both methods visible at the same time (it wouldn't compile).

    #if TYPE_FLOAT
       float Foo ( )
    #else
       double Foo ( )
    #endif

    Michael Taylor - 2/24/06

    Friday, February 24, 2006 2:07 PM
    Moderator
  • Another way is to use the using directive like this:


    #if TYPE_FLOAT
    using TReal = System.Single; // (equal to float)
    #else
    using TReal = System.Double;
    #endif
     
    public class MyClass {
        TReal number = (TReal) 1.1;
        // ...
    }

     

    This allows you to use TReal in place of the actual type. Note that this may give some problems with literals; you need to either cast those to TReal or use float literals all the time.

    Monday, February 27, 2006 10:18 AM
  • One thing to note is that a using statement is only scoped to your current file (in other words you can't easily reuse your using 'typedef' across multiple files.

     

    Wednesday, May 16, 2007 12:57 AM