C# 如何从另一个类访问stringbuilder类型的变量?

C# 如何从另一个类访问stringbuilder类型的变量?,c#,struct,stringbuilder,C#,Struct,Stringbuilder,我是c新手,还在学习,我不确定把它放在结构中是否正确,我想访问另一个类中的automotivedevice来打印它的信息。我该怎么做 我试着将它从一个结构更改为一个类,我还确保在另一个类中引用该结构 ``` struct car { public void StringBuilder() { StringBuilder automotivedevice = new StringBuilder(); Cons

我是c新手,还在学习,我不确定把它放在结构中是否正确,我想访问另一个类中的automotivedevice来打印它的信息。我该怎么做

我试着将它从一个结构更改为一个类,我还确保在另一个类中引用该结构

```  struct car
    {
        public void StringBuilder()
        {
            StringBuilder automotivedevice = new StringBuilder();
            Console.WriteLine("enter brand of the car");
            StringBuilder CarBrand = automotivedevice.AppendLine(Console.ReadLine());

            Console.WriteLine("enter mileage of the car");
            StringBuilder CarMileage = automotivedevice.AppendLine(Console.ReadLine());

            Console.WriteLine("enter number of cylinders in the car");
            StringBuilder NumberOfCylinders = automotivedevice.AppendLine(Console.ReadLine());
        }
    }```

将另一个类交给car结构的这个实例。唯一的其他方法是全局变量。但我们真的不应该使用全局变量来共享数据。“这只是个坏主意。”杜尔希赫:我想你看错了他可怜的名字。该函数称为StringBuilder,但应称为getUserInput或类似的函数。老实说,它甚至不属于汽车——这是从用户那里获取数据的唯一控制台方法。它不属于数据类。当然,我们看得越多,它就越像是XY问题链末端的XY问题。
class Program
{
    static void Main(string[] args)
    {

        // collect all user input
        Console.WriteLine("enter brand of car...");
        string brandInput = Console.ReadLine();

        Console.WriteLine("enter mileage of car...");
        string mileageInput = Console.ReadLine();

        Console.WriteLine("enter number of cylinders in the car...");
        string cylinderCountInput = Console.ReadLine();

        // create instance of car and assign user input to car properties
        Car myCar = new Car();
        myCar.Brand = brandInput;
        myCar.Mileage = mileageInput;
        myCar.NumberOfCylinders = cylinderCountInput;

        // output values in the car object to the console window
        Console.WriteLine("Brand: " + myCar.Brand);
        Console.WriteLine("Mileage: " + myCar.Mileage);
        Console.WriteLine("Cylinder Count: " + myCar.NumberOfCylinders);

        Console.WriteLine("press <ENTER> to close the console window");
        Console.ReadLine();
    }


}


public class Car
{
    public string Brand { get; set; }
    public string Mileage { get; set; }
    public string NumberOfCylinders { get; set; }
}