Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

having a problemwith using ref and out inC#

[b][red]This message was edited by cranium at 2004-11-20 12:20:44[/red][/b][hr]
[b][red]This message was edited by cranium at 2004-11-17 1:31:21[/red][/b][hr]
hi friends i m beginner...please help me about using a ref and out ...

actually i checked out the some samples about how to and when we use them but it is still not clear....!!!!
like ;
out ---->cannot read only can change the orginal value!!!?
out ---->do we have to initialize the orginal value which is in main function ???---->too confusing

ref ---->can read and can change the orginal value!!!?
ref ---->do we have to intialize orginal value which is in main function?

thanks





Comments

  • BaldusariusBaldusarius Member Posts: 162
    The 'ref' keyword is used when we want the value passed to a method to reflect the changes made to it by that method.

    [code]
    void RegularParam(int i)
    {
    // In this method, changes to 'i' are made
    // only to the copy that's local to this method
    i = 17;
    }

    void RefParam(ref int i)
    {
    // In this method, changes to 'i' affect the actual
    // variable passed in, not just a copy.
    i = 17;
    }

    int val = 0;

    RegularParam(val);

    Console.WriteLine(val); // still 0

    RefParam(ref val);

    Console.WriteLine(val); // val is now 17[/code]

    The 'out' keyword is almost the same as 'ref' except for the fact that
    the method declaring the 'out' parameter *must* assign a value to it before the method exits. Because it is guaranteed to be assigned a value, it need not be initialized when the method is called.
    [code]void OutParam(out int i)
    {
    // If we don't assign 'i' a value before this method
    // exits, the code will not compile
    i = 17;
    }

    int val;

    // Note that we haven't initialized 'val', which
    // is only allowed because it's being passed with the
    // 'out' keyword
    OutParam(out val);

    Console.WriteLine(val); // val is 17[/code]

    : [b][red]This message was edited by cranium at 2004-11-20 12:20:44[/red][/b][hr]
    : [b][red]This message was edited by cranium at 2004-11-17 1:31:21[/red][/b][hr]
    : hi friends i m beginner...please help me about using a ref and out ...
    :
    : actually i checked out the some samples about how to and when we use them but it is still not clear....!!!!
    : like ;
    : out ---->cannot read only can change the orginal value!!!?
    : out ---->do we have to initialize the orginal value which is in main function ???---->too confusing
    :
    : ref ---->can read and can change the orginal value!!!?
    : ref ---->do we have to intialize orginal value which is in main function?
    :
    : thanks
    :
    :
    :
    :
    :
    :

  • craniumcranium Member Posts: 6
    thanks for helping ,,,
    and thanks again it s really helped me....

Sign In or Register to comment.