My sharing function is not working

I want to clearly discuss my problem.
I have a function to change the value of two variables

    public static void Swap<T>(ref T first, ref T second)
    {
        T temp = first;
        first = second;
        second = temp;
    }

I use it in my code like this:

        Swap<DateTime>(ref TarikhePayaneGozaresh, ref TarikheShorooeGhozaresh);

I checked it many times and now I'm confused. As you can see, the value has not changed!
Update . I wrote more of my code.

+3
source share
2 answers

The deal is working fine:

var TarikhePayaneGozaresh = DateTime.Parse("9/9/2010 12:00:00 AM");
var TarikheShorooeGharardad = DateTime.Parse("9/9/1991 12:00:00 AM");
Swap<DateTime>(ref TarikhePayaneGozaresh, ref TarikheShorooeGharardad);
Console.WriteLine(TarikhePayaneGozaresh); // 09/09/1991 00:00:00
Console.WriteLine(TarikheShorooeGharardad); //09/09/2010 00:00:00

I can only conclude that the problem is something outside the code that you showed us, for example:

  • confused which fields / properties / variables TarikheShorooeGharardad and TarikhePayaneGozaresh belong to each point (click "show definition" to make sure they are the same)
  • in the case of fields / properties confusing the objects to which they relate
  • , (.. , )... , , , ,
    • , (, , )
+4

?

, CalculateMablagheDariaftieKol() ref non-ref.

, CalculateMablagheDariaftieKol() () .

+1

All Articles