简体   繁体   中英

passing objects as parameters

The application is printing 24 but shouldn't it be printing 18 when we know that without ref keyword only a copy of object is passed and no change is made to the original object. I have created a class called myclass and an object me. age is a public variable in class myclass. I have set me.age as 18 and through the method show I have changed it to 24.

class Program
{        
    static void Main(string[] args)
    {
        myclass me = new myclass();
        me.age = 18;
        show(me);
        Console.WriteLine(me.age);
        Console.ReadLine();
    }

    public static void show( myclass you)
    {
        you.age = 24;        
    }
}

class myclass
{
    public int age;
}

It's printing the right thing.

myclass is an object, and the default behavior is to pass the reference of the object in C#, so when you don't specify anything, you pass the reference.

If you declare struct myclass though, you'll have the behavior you want, because structs aren't references by default.

Don't confuse the variable and what the variable points to.

When you have:

MyClass myVar = new MyClass();
MyClass myVar2 = myVar;

That will create only a single instance of an object, but 2 variables pointing to it.

The same thing is happening to your parameter: you is a copy of the variable me , but both point to the same object. So when you modify you.age , you are also modifying me.age .

In your function, if you then did

you = new myClass();

only then would me and you refer to different objects. If you did this, me would still point to the original object.

If you added ref to the parameter you , then if you did

you = new myClass();

then the variable me would be updated to point to that same object.

For objects, you need to separate the variable from what the variable points to.

You're probably confusing this with C++ classes. In C#, classes are reference types , which means that whenever you have a variable of a type that's class, that variable doesn't hold the object itself, it holds only a reference to it (you can think of it as a pointer). So, when you pass your object into a method, you actually pass a reference to that object. This means the behavior you're observing is correct.

C# also supports value types (unlike eg Java), which you create by using struct instead of class . If you changed myclass into a srtuct , you would get the behavior you expected.

You are confusing value types and reference types.

public void addTwo(int a)
{
    a += 2;
}

...

int a = 5;
addTwo(a);
Console.WriteLine(a); // will give "5";

public void addTwo(ref int a)
{
    a += 2;
}

...

int a = 5;
addTwo(ref a);
Console.WriteLine(a); // will give "7";

For reference types (anything that is defined as class instead of struct , what you are passing on is a reference to the object, not a copy. So you are in fact changing the object.

You are sending an object to your function.

Not an atomic type or a struct, therefor it is sent by reference (this is how C# works), anything you change in this object in the function will also change in the original object because it is the same.

More information about passing parameters: http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM