gpt4 book ai didi

c# - 自动更新属性

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:56 25 4
gpt4 key购买 nike

我有一节课:

public class Car
{
public string Color { get; set; }

public string Speed { get; set; }

public string Property3 { get; set; }
}

我想在更新属性 ColorSpeed 时自动设置 Property3 的值

我想将 Property3 的值设置为值 ColorSpeed 的串联,用连字符分隔

执行此操作的最佳方法是什么?

最佳答案

您可以在 Property3 的 getter 中指定它 - 如下所示:

public string Property3 
{
get { return $"{this.Color}-{this.Speed}"; }
}

我假设您希望 Property3 是只读的,所以我在上面的示例中省略了 setter

关于c# - 自动更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40503643/

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