I would do this but in C#
#define swap (a, b) temp=(a); (a)=(b); (b)=temp
how?
I would do this but in C#
#define swap (a, b) temp=(a); (a)=(b); (b)=temp
how?
It is not possible in C#. The #define only allows to define symbols. Instead uses a static method that takes object as parameter type.
I know that it is not possible, it is precisely why I posted this message, to find another solution to the #define
Well, I gave you an answer. My solution is more flexible than C #define elsewhere, and easier to debug if it was useful.
Yeah but in fact I do not know really how I started in C# function is used to share a with b, so it should return 2 values, it bothers me a little
Ok, you must pass parameters by reference with the keyword "ref". Thus a variable of value type (int, etc ...) has kept its value after calling the static method.
For against, you are obliged to specify the type in fact. You will not make a generic swap function for all value types.
Here is an example for integers:
Code:public class Common { public static void Swap(ref int a, ref int b) { int tmp = a; a = b; b = tmp; } } class Program { static void Main(string[] args) { int a = 3; int b = 4; Common.Swap(ref a, ref b); } }
Bookmarks