English 中文(简体)
1. 提出反对,不予以通过,
原标题:Setting the object to null without passing it as ref
  • 时间:2012-04-06 10:44:30
  •  标签:
  • c#

C# 语言对以下行为做了哪些规定。 属性价值保持不变,但新情况(或新物体)未更新到实际参数。 它的基本职能是,除改变其提出的标的外,它只是重新发挥作用。

主要职能中的标语保持不变(并非无效),但说明性属性已改为“世界”。

class Program
{
    class MyClass
    {
        public string str;
    }

    static void MyMethod(MyClass obj)
    {
        obj.str = "Hello World";
        obj = null;
    }

    static void Main(string[] args)
    {
        MyClass o = new MyClass();
        o.str = "Hello";
        Console.WriteLine(o.str);
        MyMethod(o);

        Console.WriteLine(o.str); // prints "Hello World"
    }
}
最佳回答

在NET语言中,物体参考资料按价值计算。

因此,这意味着什么? 从概念上讲,你的法典与这部法典相同,要点明确:

static void MyMethod(MyClass *obj)
{
    obj->str = "Hello World";
    obj = NULL;
}

static void Main(string[] args)
{
    MyClass *o = new MyClass();
    o->str = "Hello";
    Console.WriteLine(o->str);
    MyMethod(o);

    Console.WriteLine(o->str); // prints "Hello World"
}

参数转至<代码>MyMethod,是点代码,指代码。 您可以参照点人确定<代码>>str的价值,但确定实际点值,使之无效,不影响电传方法中的变量。

可以通过参考通过:

C# 语言对以下行为做了哪些规定。 属性价值保持不变,但新情况(或新物体)未更新到实际参数。 它的基本职能是,除改变其提出的标的外,它只是重新发挥作用。

class Program
{
    class MyClass
    {
        public string str;
    }

    static void MyMethod(ref MyClass obj)
    {
        obj.str = "Hello World";
        obj = null;
    }

    static void Main(string[] args)
    {
        MyClass o = new MyClass();
        o.str = "Hello";
        Console.WriteLine(o.str);
        MyMethod(ref o);

        Console.WriteLine(o.str); // throws NullReferenceException, o is now null
    }
}
问题回答

您的标值是参照的。 这使得你能够改变物体的属性价值,并在呼吁方法中看到变化。 但是,对“<代码>obj”所指的改动将不见于电话方法。

If you want the variables reference to be changed throughout you need to pass the reference by reference. To do this you should change the method declaration to:

static void MyMethod(ref MyClass obj){...}

参考文件见http://msdn.microsoft.com/en-us/library/14akc27%28v=vs.110%29.aspx”rel=“nofollow”>。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签