You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#如何跨类访问其他类定义的数组值实现颜色编码转换

问题背景

需要实现的C#程序功能为:提示用户输入3个颜色编码,输出对应颜色名称(约定用户仅输入'A'、'B'、'C'三种合法编码)。

现有可运行代码

Class1定义代码

using System;
using static System.Console;

class Class1
{
    private char code;
    public char Code { get; set; }
    public Class1(char aCode)
    {
        code = aCode;
    }
}

Class2定义代码

class Class2
{
    public static void Main()
    {
        int x;
        char charCode;
        Class1[] valueCodeArray = new Class1[3];
        
        for (x = 0; x < valueCodeArray.Length; ++x)
        {
            WriteLine("Input color code No{0}",x+1);
            charCode = Convert.ToChar(ReadLine());
            valueCodeArray[x] = new Class1(charCode);
        }

        for (x = 0; x <valueCodeArray.Length ; ++x)
        {
            string NameOfCode = changeCodeToName(valueCodeArray[x].Code);
            WriteLine("No{0}: color is {1}", x + 1, NameOfCode);
        }
    }
    public static string changeCodeToName(char charCode)
    {
        string NameOfCode = "";
        switch (charCode)
        {
            case 'A':
                    NameOfCode = "Red";
                return NameOfCode;
            case 'B':
                NameOfCode = "Blue";
                return NameOfCode;
            case 'C':
                NameOfCode = "Green";
                return NameOfCode;
        }
        return "";
    }
}

当前代码可正常输出结果:No1: color is Red. No2: color is Blue. No3: color is Green

调整需求与异常

需要调整代码结构,将颜色名称数组

readonly string[] nameArray = new string[3] { "Red", "Blue", "Green"};

定义在Class1中,在Class2的changeCodeToName方法内调用该数组的对应索引值,替换switch分支中硬编码的颜色字符串。直接替换硬编码值为nameArray对应索引项时程序无法正常运行,需要正确实现方案。

正确实现方案

程序运行异常的核心原因有两点:

  1. 若将nameArray定义为Class1的实例成员,静态方法changeCodeToName没有Class1实例的前提下无法直接访问;同时'A'/'B'/'C'为字符类型,对应ASCII值与数组索引0/1/2不匹配,直接强转作为索引会触发越界错误。
  2. 原Class1中自动属性Code与私有字段code未做关联,构造函数仅对私有字段赋值,通过属性读取时会拿到默认值\0,本身存在逻辑缺陷。

修正步骤

  • 修正Class1逻辑:将颜色数组定义为Class1的公共静态只读成员,静态成员无需实例化即可访问,适配全局映射的使用场景;同时修复Code属性与私有字段的关联问题,解决属性取值错误的bug。
  • 调整changeCodeToName方法逻辑:将输入的字符编码映射为合法数组索引,'A'对应索引0、'B'对应索引1、'C'对应索引2,直接读取Class1中定义的数组对应项返回,移除原switch硬编码分支。

修正后完整代码

修正后的Class1

using System;
using static System.Console;

class Class1
{
    // 公共静态只读颜色名数组,属于类本身,无需实例化即可访问
    public static readonly string[] nameArray = new string[3] { "Red", "Blue", "Green" };
    
    private char code;
    // 关联属性与私有字段,修复原属性取值错误问题
    public char Code 
    { 
        get { return code; }
        set { code = value; }
    }
    public Class1(char aCode)
    {
        code = aCode;
    }
}

修正后的Class2

class Class2
{
    public static void Main()
    {
        int x;
        char charCode;
        Class1[] valueCodeArray = new Class1[3];
        
        for (x = 0; x < valueCodeArray.Length; ++x)
        {
            WriteLine("Input color code No{0}",x+1);
            charCode = Convert.ToChar(ReadLine());
            valueCodeArray[x] = new Class1(charCode);
        }

        for (x = 0; x <valueCodeArray.Length ; ++x)
        {
            string NameOfCode = changeCodeToName(valueCodeArray[x].Code);
            WriteLine("No{0}: color is {1}", x + 1, NameOfCode);
        }
    }
    public static string changeCodeToName(char charCode)
    {
        // 利用字符ASCII值计算合法索引:'A'-'A'=0、'B'-'A'=1、'C'-'A'=2
        int arrayIndex = charCode - 'A';
        // 直接读取Class1静态数组的对应值返回
        return Class1.nameArray[arrayIndex];
    }
}

逻辑说明

  • 数组标记为public static readonly:static表示成员归属于类本身,不需要创建Class1实例就能在静态方法中通过Class1.nameArray访问;readonly保证数组初始化后不会被意外修改,符合固定颜色映射的需求。
  • 索引计算用charCode - 'A':C#中char类型参与运算时会自动转为对应的ASCII数值,计算结果刚好匹配数组的三个下标,完全替代原switch分支的判断逻辑。
  • 修复原属性逻辑缺陷:原代码使用自动属性但构造函数仅赋值给私有字段,之前代码能正常运行是因为switch判断直接使用传入的参数,未调用属性取值,修正后属性的读写逻辑与私有字段完全对齐,不存在取值偏差。

内容的提问来源于stack exchange,提问作者gon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 02:33:44