gpt4 book ai didi

c# - 类转换器

转载 作者:太空宇宙 更新时间:2023-11-03 21:54:11 24 4
gpt4 key购买 nike

我有以下类(class) People:

class People
{
public enum Gender
{
Man = 'w',
Woman = 'f'
}

public struct Person
{
public string First, Last;
public Gender Gender;
public int Age;
public float Height, Weight;
}

public struct Group
{
public int Members;
public string Name;
}
}

现在,我们位于 Program 类中:

class Program
{
static void Main( string[] args )
{
People.Person p = new People.Person();

p.First = "Victor";
p.Last = "Barbu";
p.Age = 14;
p.Height = 175;
p.Weight = 62;
p.Gender = People.Gender.Man;

Console.ReadLine();
}
}

我想写这样一行:

Console.Write( x.toString() );

如何自定义x.toString()方法,使控制台的结果如下

Victor Barbu
14 years
Man
175 cm
62 kg

?

提前致谢!

最佳答案

只需覆盖 ToString() 方法

    public override string ToString()
{
// return the value you want here.
}

关于c# - 类转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13053375/

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