相关疑难解决方法(0)

访问和更改结构作为属性与字段

好吧,我将开始我的问题,说我理解可变结构背后的邪恶,但我正在使用SFML.net并使用大量的Vector2f和这样的结构.

我不明白为什么我可以拥有,并且更改类中的字段的值,并且在同一个类中不能对属性执行相同的操作.

看看这段代码:

using System;

namespace Test
{
    public struct TestStruct
    {
        public string Value;
    }

    class Program
    {
        TestStruct structA;
        TestStruct structB { get; set; }

        static void Main(string[] args)
        {
            Program program = new Program();

            // This Works
            program.structA.Value = "Test A";

            // This fails with the following error:
            // Cannot modify the return value of 'Test.Program.structB'
            // because it is not a variable
            //program.structB.Value = "Test B"; 

            TestStruct copy = program.structB;
            copy.Value = "Test B";

            Console.WriteLine(program.structA.Value); // …
Run Code Online (Sandbox Code Playgroud)

c# struct

1
推荐指数
2
解决办法
2008
查看次数

标签 统计

c# ×1

struct ×1