Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: va!n on February 23, 2011

Title: [c#] Is there something like macros?
Post by: va!n on February 23, 2011
I would like to use in C# something like macros known in C/C++... Sadly it seems C# does not supports macros... However i have a lot of macros in mind and i dont want to write the same codeblock (macro) again and again inside my project and i dont want to use public methode, because this would be slow down the code, even when its called x times inside a loop.

So any idea, how to use something like macros in C#?

Code: [Select]
  #define MULT(x, y) x * y    // just as simple example
Title: Re: [c#] Is there something like macros?
Post by: Jim on February 24, 2011
No, C# doesn't have macros.  You could use a static class.
Code: [Select]
public static class Macros
{
  public static int Mult(int a, int b) { return a*b; }
}

...

int myanswer = Macros.Mult(5,4);

Let the compiler work out if it can inline that.

Jim
Title: Re: [c#] Is there something like macros?
Post by: va!n on February 24, 2011
Thanks for the fast reply Jim! So, when using "static", the compiler could probaly inline it?  :kewl:

[Edited - Added:]
Thanks a lot for this great tip! It works great! K++
Title: Re: [c#] Is there something like macros?
Post by: Jim on February 24, 2011
I have no idea.  The IL is dynamically recompiled and optimised at run-time and it's possible.
But it's nothing like C where you can determine this at compile time.

Jim