Tuesday, 21 June 2011

ref keyword in c sharp


ref C#

The ref keyword causes arguments to be passed by reference. The effect is that any changes to the parameter in the method will be reflected in that variable when control passes back to the calling methodTo use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword.An argument passed to a ref parameter must first be initialized

 For example:

class
 RefExample

{

    static void Method(ref int i)
    {
        i = 44;
    }
    static void Main()
    {
        int val = 0;
        Method(ref val);
        // val is now 44
    }
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger