tags:

views:

253

answers:

7

Hi

How to pass refernces in c#??/

+7  A: 
private void functionName (ref Type variableName)
{

}

To Call it

functionName(ref variable);
Kevin
and be sure to also use the ref keyword when calling the methodSomeMethod(ref variableName);
Lars Truijens
good point. I'll add it
Kevin
+3  A: 

You can pass parameters by reference in C# using this syntax.

public void MyMethod(ref string myString)
{

}

You will then need to put ref before the value when passing it.

Mitchel Sellers
Might be worth mentioning that this [typically] isn't necessary for passing objects, only primatives.
Jon B
Yes, very good point.
Mitchel Sellers
A: 

Here is a nice overview of parameter passing in C#:

http://www.yoda.arachsys.com/csharp/parameters.html

JerSchneid
+5  A: 

Your question is extremely unclear, but it's quite possible that my article on parameter passing in C# will answer whatever you really intended to ask.

In particular, you need to distinguish between passing a reference by value, and passing an argument by reference. If you're hazy on value types and reference types, you might also want to look at my article on that topic.

Jon Skeet
A: 

Calling Code:

string companyName = "New Company";
GetEmail(ref companyName);

Method Code:

private void GetEmail(ref string companyName)
{

}
Jon
A: 

Your questions isn't clear, but I'd like to point out that in C#, objects are passed by reference by default. Meaning, if you have an object, and then pass that object on to a method that makes changes to that object, those changes will affect the object in your calling code as well, since they both reference the same object.

BFree
"in C#, objects are passed by reference by default" : this is not true... they are passed by value unless you specify the 'ref' modifier. I think you are confusing the difference between reference types and value types, and the difference between passing parameters by reference or value. When you pass a reference type as parameter by value, you pass a reference by value ; the called function can't modify this reference, but it can modify the referenced object
Thomas Levesque
I find it quite fun when people change the state of an object that was not passed in by reference. Really makes for clean and obvious code.
Matthew Whited
@Thomas.. You're just nitpicking... primitive types are passed by value, objects are passed by reference. Yes, the reference to the object itself is passed by value, but the actual object itself isn't copied, rather a reference to it is sent over. Which is why I'm saying objects are passed by reference.
BFree
+1  A: 

Jon Skeet has a good article on this here.

In C#, value types (like int, double, byte and structs) are passed by value, by default. This means that the receiving method has a NEW instance of the type. If an int that has a value of 1 is passed to the method, and the method changes it to 2, this change is only reflected within the method, the calling location's int is still 1. If however the ref keyword is added, then changes made to that integer are reflected back to the calling location.

All classes in C# are reference types. This means, by default, the references are passed by value. This is the important part. This means, changes made to that instance of the object are reflected back to the calling location, because it is the same object. However, if the method changes it's reference to a different object, this change is not reflected. In the case you want these changes reflected back, you would need to use the ref keyword on the parameter.

    public static void Main()
    {
        int i = 1;
        Method1(i); //i here is still 1
        Method2(ref i); //i is now 2


        SimpleObj obj = new SimpleObj();
        obj.Value = 1;

        Method3(obj); //obj.Value now 2
        Method4(obj); // obj.Value still 2
        Method5(ref obj); //obj.Value now 5
    }

    private static void Method5(ref SimpleObj obj)
    {
        obj = new SimpleObj();
        obj.Value = 5;
    }

    private static void Method4(SimpleObj obj)
    {
        obj = new SimpleObj();
        obj.Value = 5;
    }

    private static void Method3(SimpleObj obj)
    {
        obj.Value++;
    }

    private static void Method2(ref int i)
    {
        i++;
    }

    private static void Method1(int i)
    {
        i++;
    }

    public class SimpleObj
    {
        public int Value { get; set; }
    }

The ref keyword is covered in section 10.6.1.2 of the C# 3.0 specification. Here is the msdn documentation.

Timothy Carter