Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Since ref passes the data in two ways and out passes the data in one way.But one thing i am not sure which one is better to use I have done a simple practical using ref and out which have the same output..If it has same output then whats the difference?

Here is my code using out keyword:

What I have tried:

static void Main(string[] args)
        {
            Console.Write("First number = ");
            int num1 = int.Parse(Console.ReadLine());

            Console.Write("Second number = ");
            int num2 = int.Parse(Console.ReadLine());

            int remainder;
            int division;
            


            
            Remainder(num1, num2,out  remainder, out division);

            Console.WriteLine("Your Output {0} / {1} is {2} having remainder = {3}",num1,num2,division,remainder);


        }
        static void Remainder(int FN,int SN,out int remainder,out int division)
        {
            remainder = FN % SN;
            division = FN / SN;
        }


Now using ref keyword :

static void Main(string[] args)
        {
            Console.Write("First number = ");
            int num1 = int.Parse(Console.ReadLine());

            Console.Write("Second number = ");
            int num2 = int.Parse(Console.ReadLine());

            int remainder = 0;
            int division = 0;
            


            
            Remainder(num1, num2,ref  remainder, ref division);

            Console.WriteLine("Your Output {0} / {1} is {2} having remainder = {3}",num1,num2,division,remainder);


        }
        static void Remainder(int FN,int SN,ref int remainder,ref int division)
        {
            remainder = FN % SN;
            division = FN / SN;
        }


Using method remainder it simply takes 2 numbers and gives the division and the remainder.Works same for both which one should i use?
Posted
Updated 17-Feb-18 2:25am
v2
Comments
phil.o 17-Feb-18 6:48am    
What do you mean by "ref passes the data in two ways and out passes the data in one way"? Where does this assumption come from?

ref and out are quite similar in that they allow to pass parameters by reference rather than by value.
The only difference is that out does not need the parameter to be initialized before being passed, whereas ref needs an already initialized parameter. Beyond that, none is better than the other; you have to use whichever suits your context. If the parameter is not initialized, use out; if not, use ref.
 
Share this answer
 
Comments
BillWoodruff 17-Feb-18 8:21am    
+5
phil.o 17-Feb-18 8:24am    
Thanks :)
abdul wadood 18-Feb-18 11:21am    
simple and helpful. Thank you
phil.o 18-Feb-18 12:44pm    
You are welcome.
As Phil has said, they are very similar.
But ... if you aren't using the passed in value - as your example doesn't - then using ref is redundant and just adds extra overhead to each call. Using out parameters instead is more reflective of what you are actually doing.
But ... I wouldn't use either in that case: I'd either create a DivisonResult struct:
C#
public struct DivisionResult
    {
    public int Remainder { get; set; }
    public int Quotient { get; set; }
    }
And return a new instance of that:
C#
public static DivisionResult Remainder(int FN, int SN)
    {
    return new DivisionResult() { Remainder = FN % SN, Quotient = FN / SN };
    }

Or I'd return a tuple: Tuple Class (System)[^]
 
Share this answer
 
Comments
phil.o 17-Feb-18 8:24am    
5 for a better alternative :)
BillWoodruff 17-Feb-18 8:25am    
+5
viva tuple c#7 !

public static (int,int) Remainder(int FN, int SN)
{
return (FN % SN, FN / SN);
}
OriginalGriff 17-Feb-18 9:00am    
I'm still not convinced about "anonymous tuples" like that - I'm not convinced they make code more readable unless you go to the effort and name them:

return (Remainder: FN % SN, Quotient: FN / SN);
...
(int Remainder, int Quotient) = Divide( FN, SN);

BillWoodruff 17-Feb-18 9:33am    
good point ! if i were on a team, i'd be using names. cheers, Bill
abdul wadood 18-Feb-18 11:29am    
Right now as a beginner i have started to learn about ref and out only - no idea about the struct and the tuple, will look at them later...

anyway thanks for the solution. +5

I would say that neither of your options are the best. I would use a Tuple. Something like this:

C#
static Tuple<int, int> Remainder(int dividend, int divisor)
       {

           int quotient = dividend / divisor;
           int remainder = dividend % divisor;
           return Tuple.Create(quotient, remainder);
       }

           int dividend = 10;
           int divisor = 3;
           var resultTuple = Remainder(dividend, divisor);
           Console.WriteLine("Your Output {0}/{1} is {2} having remainder = {3}",
             dividend, divisor, resultTuple.Item1, resultTuple.Item2);

 
Share this answer
 
Comments
abdul wadood 18-Feb-18 11:19am    
Don't know what Tuple is..
George Swan 18-Feb-18 11:40am    
https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900