简体   繁体   English

C#:如何创建以 24 小时格式显示小时数的属性(只读)?

[英]C#: How can I create property (read-only) that shows hours in 24 - hours format?

I have a struct with constructors:我有一个带有构造函数的结构:

public struct Time
{
    public Time(int minutes)
        : this()
    {
        this.Minutes = minutes;
    }

    public Time(int hours, int minutes)
        : this(minutes)
    {
        this.Hours = hours;
    }

I need help to create a property (read-only) which will allow me to indicate hours in 24 hour format ( 00.XX - 23.XX ).我需要帮助来创建一个属性(只读),它允许我以 24 小时格式( 00.XX - 23.XX )指示小时数。 As I indicated above, I supply property's value by calling a constructor.如上所述,我通过调用构造函数来提供属性的值。 My current incomplete attempt:我目前不完整的尝试:

public readonly int Hours { get; }

Happy to hear any suggestions.很高兴听到任何建议。 Cheers!干杯!

public struct Time
{
    public Time(int minutes)
        : this()
    {
        this.Minutes = minutes;
    }

    public Time(int hours, int minutes)
        : this(minutes)
    {
        this.Hours = hours;
    }

    public int Hours {get;set;}
    public int Minutes {get;set;}

    public string To24Hours{get {
        return $"{this.Hours} : {this.Minutes}";
    }}
}

This requires though that you ensure the hours you put in your constructor is already in 24 hours format.这要求您确保您在构造函数中输入的时间已经是 24 小时格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM