Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 20-04-2009
Member
 
Join Date: May 2008
Posts: 962
Equivalent of #define in C#

I would do this but in C#

#define swap (a, b) temp=(a); (a)=(b); (b)=temp

how?
Reply With Quote
  #2  
Old 20-04-2009
Member
 
Join Date: Feb 2008
Posts: 194
Re: Equivalent of #define in C#

It is not possible in C#. The #define only allows to define symbols. Instead uses a static method that takes object as parameter type.
Reply With Quote
  #3  
Old 20-04-2009
Member
 
Join Date: May 2008
Posts: 962
Re: Equivalent of #define in C#

I know that it is not possible, it is precisely why I posted this message, to find another solution to the #define
Reply With Quote
  #4  
Old 20-04-2009
Member
 
Join Date: Feb 2008
Posts: 194
Re: Equivalent of #define in C#

Well, I gave you an answer. My solution is more flexible than C #define elsewhere, and easier to debug if it was useful.
Reply With Quote
  #5  
Old 20-04-2009
Member
 
Join Date: May 2008
Posts: 962
Re: Equivalent of #define in C#

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
Reply With Quote
  #6  
Old 20-04-2009
Member
 
Join Date: Feb 2008
Posts: 194
Re: Equivalent of #define in C#

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);
}
}
Reply With Quote
  #7  
Old 20-04-2009
Member
 
Join Date: May 2008
Posts: 945
Re: Equivalent of #define in C#

Quote:
Originally Posted by Ashok.M View Post
For against, you are obliged to specify the type in fact. You will not make a generic swap function for all value types.
False, it may very well be a generic approach using generics.

Code:
public class Common   
{       
public static void Swap<T>(ref T a, ref T b)       
{           
T tmp = a;           
a = b;           
b = tmp;       
}   
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Equivalent of #define in C#"
Thread Thread Starter Forum Replies Last Post
How to Define Namespaces in PHP? SKREECH Software Development 5 21-02-2010 02:00 AM
Can we use #define inside the structure? Harper 21 Software Development 5 18-01-2010 08:39 AM
How to define Union? seema_thk Software Development 5 15-12-2009 02:07 PM
How to define a variable containing an include GeforceUser Software Development 4 18-11-2009 08:47 PM
Define the term- CLR for .NET Jesus2 Software Development 3 14-11-2009 09:10 PM


All times are GMT +5.5. The time now is 10:32 AM.