gpt4 book ai didi

c# - 如何将结构定义为属性?

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:38 25 4
gpt4 key购买 nike

标题可能有误,如有请修改。我不确定如何提出我的问题,所以只看代码,因为它应该很明显。

使用注释代码可以,但我想知道为什么实际代码不起作用。我确定这是错误的,但如何解决?或者这不是它的做法?

using System;

namespace SomethingAwful.TestCases.Structs
{
public class Program
{
public static void Main()
{
Foo f = new Foo();
f.Bar.Baz = 1;

Console.WriteLine(f.Bar.Baz);
}
}

public class Foo
{
public struct FooBar
{
private int baz;

public int Baz
{
get
{
return baz;
}
set
{
baz = value;
}
}

public FooBar(int baz)
{
this.baz = baz;
}
}

private FooBar bar;

public FooBar Bar
{
get
{
return bar;
}
set
{
bar = value;
}
}

//public FooBar Bar;

public Foo()
{
this.bar = new FooBar();
//this.Bar = new FooBar();
}
}
}

最佳答案

结构仅按值进行复制,因此您最终要做的就是更改返回的副本。使用类。

关于c# - 如何将结构定义为属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1117835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com