Results 1 to 7 of 7

Thread: Equivalent of #define in C#

  1. #1
    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?

  2. #2
    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.

  3. #3
    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

  4. #4
    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.

  5. #5
    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

  6. #6
    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);
    }
    }

  7. #7
    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;       
    }   
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,564,353.50774 seconds with 17 queries