English 中文(简体)
如何确定物体的财产,以提及阵列中的某一要素?
原标题:How to set a property of an object to the reference of an element in an array?
  • 时间:2024-04-09 23:43:03
  •  标签:
  • c#
  • .net

I m trying to point the value of a property to an array element, which will be set at a later time. I have the following code:

using System;

public class HelloWorld
{
    private static string[] s = new string[] { null };
    
    public static void Main(string[] args)
    {
        var a = new A();
        a.Value = s[0];
        Console.WriteLine ("value bef = " + a.Value);
        s[0] = "test";
        Console.WriteLine ("value aft = " + a.Value);
    }
    
    public class A
    {
        public string Value { get; set; }
    }
}

在我管理上述法典时,我取得了以下成果:

value bef = 
value aft = 

然而,自此 C#用斜体设计,参考文献,预计产出将是

value bef = 
value aft = test

Why does this not work? Is there anyway I can do this in C#? Converting A.Value to a class or s to an array of As is not really an option.

最佳回答

段 次 页 次 C#用斜体设计,参考文献,我希望......

在第一次假释产出时,s[0]a.Value均为参考值的null<>>的参考变量。

但它们是不同变量,而与另一个变量相比,这并不重要。 由于<代码>a.Value在<代码>null<>/code>上有一个空洞的参考资料,你可以不改动一个变量或另一个变量。 参考<代码>[0]变量。

So when then assigning "test" to s[0], you changed that variable to refer to a new string object, but the a.Value variable still has the same null reference as before.

初始价值是否为<代码>null<>/code>或实际扼杀物体,也属事。 结果是一样的。


但是,如果<代码>s阵列提及我们的<代码>A类别?

private static A[] s = new A[] { null };

Then I ll change up the code just a bit so we have a value to work with:

var a = new A();
s[0] = a; // reversed the assignment
Console.WriteLine ("value bef = " + a.Value); // Shows "value bef = "
s[0].Value = "test"; // s[0] is an A object now, so we set it s property
Console.WriteLine ("value aft = " + a.Value); // Shows "value aft = test"

因此,我们重申,我们也可以这样做:

// create the object here
private static A[] s = new A[] { new A() };

A a = new A();
a.Value = s[0]; // but keep the original assignment
Console.WriteLine ("value bef = " + a.Value); // Shows "value bef = "
s[0].Value = "test";
Console.WriteLine ("value aft = " + a.Value); // Shows "value aft = test"

两者都显示变化。 理由是,<代码>[0]和a均指同一物体。 它们仍然是不同的变数,但它们提到同一项目。 然后,我们改变一个单一物体的财产,而不是标的本身。 它具有相同的目的,但对于财产具有不同价值,因此这两个变数仍显示相同的结果。

问题回答

该守则将以下列形式开展工作:

public static void Main(string[] args)
{
    var a = new A();
    a.Value = ref s[0]; // <- ref
    Console.WriteLine("value bef = " + a.Value);
    s[0] = "test";
    Console.WriteLine("value aft = " + a.Value);
}

public ref struct A
{
    public ref string Value;
}

由于物体可变。

s[0] = "test"; 

页: 1

a 。 价值仍然指原物体,该物体无效。





相关问题
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. ...

热门标签