What's REF Keyword?

What's REF Keyword?

REF:-A reference type and sending a parameter by reference are two different
things. If you send a reference type to a method (passed by value), then
the reference is passed by value. Since you are sending a reference, you
can still access the same object outside of the method using another
variable set to the same reference.

Now, if you pass that reference by ref, you can modify the value of that
parameter so that it holds a new reference to a new object. When you pass
it by value, you can't do that.


Example:-
Using System;
class Color
{
    public Color()
    {
        this.red = 255;
        this.green = 0;
        this.blue = 125;
    }

    protected int red;
    protected int green;
    protected int blue;

    public void Getscol(ref int red, ref int green, ref int blue)
    {
        red = this.red;
        green = this.green;
        blue = this.blue;
    }
}

class RefTest1App
{
    public static void Main()
    {
        Color color = new Color();
        int red;
        int green;
        int blue;
        color.Getscol(ref red, ref green, ref blue);
        Console.WriteLine("red = {0}, green = {1}, blue = {2}",
red, green, blue);
    }
}

By:kamal
Date:2012-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Links

C interview questions and answers for experienced and fresher