如何从另一个 class 访问 stringbuilder 类型的变量?
how can i access a variable of type stringbuilder from another class?
我是 c# 的新手,仍在学习,我不确定将其放入结构中是否正确,我想在另一个 class 中访问汽车设备以打印其信息。我该怎么做呢?
我试过将它从结构更改为 class,我还确保在另一个 class
中实例化该结构
``` 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());
}
}```
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; }
}
我是 c# 的新手,仍在学习,我不确定将其放入结构中是否正确,我想在另一个 class 中访问汽车设备以打印其信息。我该怎么做呢?
我试过将它从结构更改为 class,我还确保在另一个 class
中实例化该结构``` 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());
}
}```
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; }
}