gpt4 book ai didi

c# - 是否可以列出我们所有的字符串变量名称和值

转载 作者:太空狗 更新时间:2023-10-29 18:12:36 26 4
gpt4 key购买 nike

可以列出实例中的变量名称及其值。

  public class Car
{
public string Color;
public string Model;
public string Made;
}

protected void Page_Load(object sender, EventArgs e)
{

//Create new instance
Car MyCar = new Car();
MyCar.Color = "Red";
MyCar.Model = "NISSAN";
MyCar.Made = "Japan";

//SOMETHING HERE
foreach (MyCar Variable in MyCar)
{
Response.Write("<br/>Variable Name"+ "XXX"+ "Variable Value");
}

}

最佳答案

尝试这样的事情:

using System;

class Car
{
public string Color;
public string Model;
public string Made;
}

class Example
{
static void Main()
{
var car = new Car
{
Color = "Red",
Model = "NISSAN",
Made = "Japan"
};

foreach (var field in typeof(Car).GetFields())
{
Console.WriteLine("{0}: {1}", field.Name, field.GetValue(car));
}
}
}

关于c# - 是否可以列出我们所有的字符串变量名称和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5922473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com