传递类实例而不引用 [英] Pass class instance without reference

查看:65
本文介绍了传递类实例而不引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于是否通过引用传递一些实例的问题:这是我的问题:

案例 1:简单的 var,如 int:

private void button2_Click(object sender, EventArgs e){int nTest = 10;testInt(nTest);MessageBox.Show(nTest.ToString());//此消息显示我 10testIntRef(ref nTest);MessageBox.Show(nTest.ToString());//此消息显示我 11}私有无效 testInt(int nn){nn++;}私有无效 testIntRef(ref int nn){nn++;}

这正是我的想法,如果我使用ref,参数是通过引用传递的,所以如果改变了,当我退出函数时,值也改变了...

案例 2:类:

//简单类理解参考..公开课 cTest{内部测试;公共 cTest(){设置测试(0);}public void setTest(int n){n测试 = n;}公共 int getTest(){返回 nTest;}}//我的主要代码private void button3_Click(对象发送者,EventArgs e){cTest tt = 新 cTest();tt.setTest(2);测试类(tt);//我希望消息显示我 2,因为 testClass//没有(参考 cTest 测试)MessageBox.Show(tt.getTest().ToString());}私有无效测试类(cTest 测试){test.setTest(55);}

而且,正如代码注释中所写,我没有通过我的 cTest 作为参考,但结果是一样的,消息显示我 55 而不是 2..

如何在没有引用的情况下传递类?

解决方案

如何在没有引用的情况下传递类?

你不能.

你可以克隆那个实例并发送它,但它仍然会被引用发送......

  • class - 引用类型
  • struct - 值类型.

阅读:

引用 Jon Skeet C# 深入第二版:

<块引用>

误解 3:对象在 C# 中默认通过引用传递"

这可能是传播最广的神话.再一次,做这件事的人经常声称(尽管并非总是如此)知道 C# 的实际行为,但他们不知道通过引用传递"的真正含义.不幸的是,这对于那些知道这意味着什么.引用传递的正式定义比较复杂,涉及 l 值和类似的计算机科学术语,但重要的是事情是,如果你通过引用传递一个变量,你正在调用的方法可以改变通过改变其参数值来改变调用者变量的值.现在记住引用类型变量的值是引用,而不是对象本身.你可以改变参数引用的对象的内容,而参数本身没有通过引用传递.例如,下面的方法改变了StringBuilder 对象,但调用者的表达式仍将引用与之前相同的对象:

void AppendHello(StringBuilder builder){builder.Append(你好");}

<块引用>

调用此方法时,参数值(对 StringBuilder 的引用)为按值传递.如果我要更改 builder 变量的值方法——例如,使用语句 builder = null;——这种变化不会是被来电者看到,与神话相反.

C# 深度值类型和引用类型第 46 页

I've this question about pass some instances by ref or not: here is my problem:

Case 1: simple var like int:

private void button2_Click(object sender, EventArgs e)
{
    int nTest = 10;

    testInt(nTest);
    MessageBox.Show(nTest.ToString());
    // this message show me 10

    testIntRef(ref nTest);
    MessageBox.Show(nTest.ToString());
    // this message show me 11
}

private void testInt(int nn)
{
    nn++;
}

private void testIntRef(ref int nn)
{
    nn++;
}

this is exactly what I think, if I use the ref, the parameter is passed by reference, so if is changed, when I exit from the function, the value is changed...

Case 2: class:

// simple class to understand the reference..
public class cTest
{
    int nTest;
    public cTest()
    {
        setTest(0);
    }

    public void setTest(int n)
    {
        nTest = n;
    }

    public int getTest()
    {
        return nTest;
    }
}

// my main code
private void button3_Click(object sender, EventArgs e)
{
    cTest tt = new cTest();
    tt.setTest(2);

    testClass(tt);

    // I expect that the message shows me 2, 'cause testClass
    // doesn't have (ref cTest test)
    MessageBox.Show(tt.getTest().ToString());
}

private void testClass(cTest test)
{
    test.setTest(55);
}

and, as written in the comment on the code, I don't have passed my cTest as reference, but the result is the same, the message show me 55 and not 2..

How can I pass a class without reference?

解决方案

How can I pass a class without reference?

You can't.

You can clone that instance and send it, but it will still be sent by ref...

  • class - Reference type
  • struct - Value type.

Reading:

Quoting Jon Skeet C# in depth second edition:

MYTH #3: "OBJECTS ARE PASSED BY REFERENCE IN C# BY DEFAULT"

This is probably the most widely propagated myth. Again, the people who make this claim often (though not always) know how C# actually behaves, but they don’t know what "pass by reference" really means. Unfortunately, this is confusing for people who do know what it means. The formal definition of pass by reference is relatively complicated, involving l-values and similar computer science terminology, but the important thing is that if you pass a variable by reference, the method you’re calling can change the value of the caller’s variable by changing its parameter value. Now remember that the value of a reference type variable is the reference, not the object itself. You can change the contents of the object that a parameter refers to without the parameter itself being passed by reference. For instance, the following method changes the contents of the StringBuilder object in question, but the caller’s expression will still refer to the same object as before:

void AppendHello(StringBuilder builder)
{
    builder.Append("hello");
}

When this method is called, the parameter value (a reference to a StringBuilder) is passed by value. If I were to change the value of the builder variable within the method—for example, with the statement builder = null;—that change wouldn’t be seen by the caller, contrary to the myth.

C# in depth Value types and reference types page 46

这篇关于传递类实例而不引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆