Thursday, May 5, 2011

Redefining C# keywords?

Is it possible to redefine a C# keyword like int to use Int64 instead of Int32?

If not, how can one do this? I want to have the flexibility to redefine the int I use, so later I could change it to int64 if I wanted, instead of going around to replace all the int values manually.

From stackoverflow
  • Add

    using MyIntType = System.Int64;
    

    after the namespace declaration.

  • No it's not possible and you really should not be doing this. Redefining keywords to custom values will only serve to make your code less readable and maintainable. People getting introduced to your code base would have to forget everything they know about C# defaults and learn your defaults. This is not a good way to make a maintainable code base.

    What you should consider though is creating a new type name and using a "using alias" to redirect that type within your code base.

    using FexibleInt = System.Int32;
    
    Joan Venge : Thanks also can I make this global so all C# files would know the type, instead of using the same line in every file?
    Michael Myers : FexibleInt - I like it! ;)
  • Sorry - this cannot be done using C# unless you put all your files through a preprocessor before compiling them.

  • I don't think so. Are you trying to have the same functionality as typedef in c++?

    One thing you can do is to encapsulate int into a custom defined class/struct.

    or

    using MyIntType = System.Int64;

    Joan Venge : Yes, I was trying to do that.

0 comments:

Post a Comment