|
■No96829 (魔界の仮面弁士) に追記 > ST_1 を使うのを忘れていたので書き直し。(structData の階層が抜けてました!)
「struct + インスタンス フィールド」を 「class + インスタンス プロパティ」に置き換えたバージョン。
using System; public class ST_1 { private readonly float[] _span = new float[4]; public float a { get => _span[0]; set => _span[0] = value; } public float b { get => _span[1]; set => _span[1] = value; } public float c { get => _span[2]; set => _span[2] = value; } public float d { get => _span[3]; set => _span[3] = value; } public ST_1(float a, float b, float c, float d) => (this.a, this.b, this.c, this.d) = (a, b, c, d); public ST_1(float[] f) : this(f[0], f[1], f[2], f[3]) { } public ST_1() : this(0f, 0f, 0f, 0f) { } } public class ST_2 { public float value1 { get; set; } public ST_1 structData { get => new ST_1(valueArray); } public float[] valueArray { get; init; } }
class Program { static void Main() { ST_2 x = new ST_2 { value1 = 1.23f, valueArray = new float[] { 2.34f, 3.45f, 4.56f, 5.67f } };
Console.WriteLine($"value1 = {x.value1}"); Console.WriteLine($"valueArray[] = {{ {string.Join(", ", x.valueArray)} }}"); Console.WriteLine($"structData.a = {x.structData.a}"); Console.WriteLine($"structData.b = {x.structData.b}"); Console.WriteLine($"structData.c = {x.structData.c}"); Console.WriteLine($"structData.d = {x.structData.d}"); Console.WriteLine($"valueArray[0] = {x.valueArray[0]}"); Console.WriteLine($"valueArray[1] = {x.valueArray[1]}"); Console.WriteLine($"valueArray[2] = {x.valueArray[2]}"); Console.WriteLine($"valueArray[3] = {x.valueArray[3]}");
Console.WriteLine(); Console.WriteLine("☆☆☆ value1, valueArray[1], structData.d を書き換え ☆☆☆"); x.value1 = -123.45f; x.valueArray[1] = -234.56f; x.structData.d = -345.67f;
Console.WriteLine($"value1 = {x.value1}"); // ★ 1.23 → -123.45 Console.WriteLine($"valueArray[] = {{ {string.Join(", ", x.valueArray)} }}"); // ★ Console.WriteLine($"structData.a = {x.structData.a}"); Console.WriteLine($"structData.b = {x.structData.b}"); // ★ 3.45 → -234.56 Console.WriteLine($"structData.c = {x.structData.c}"); Console.WriteLine($"structData.d = {x.structData.d}"); // ★ 5.67 → -345.67 Console.WriteLine($"valueArray[0] = {x.valueArray[0]}"); Console.WriteLine($"valueArray[1] = {x.valueArray[1]}"); // ★ 3.45 → -234.56 Console.WriteLine($"valueArray[2] = {x.valueArray[2]}"); Console.WriteLine($"valueArray[3] = {x.valueArray[3]}"); // ★ 5.67 → -345.67
Console.ReadKey(); } }
|