Add property to struct - CSharp Custom Type

CSharp examples for Custom Type:struct

Description

Add property to struct

Demo Code

using System;/* w w  w . ja  va  2s.  c om*/
public struct TimeSpan
{
   private uint totalSeconds;
   private const uint SecondsInHour = 3600;
   private const uint SecondsInMinute = 60;
   public TimeSpan(uint initialSeconds)
   {
      totalSeconds = initialSeconds;
   }
   public uint Seconds
   {
      get
      {
         return totalSeconds;
      }
      set
      {
         totalSeconds = value;
      }
   }
   public override string ToString()
   {
      uint hours;
      uint minutes;
      uint seconds;
      hours = totalSeconds / SecondsInHour;
      minutes = (totalSeconds % SecondsInHour) / SecondsInMinute;
      seconds = (totalSeconds % SecondsInHour) % SecondsInMinute;
      return String.Format("{0} Hrs {1} Mins {2} Secs",
      hours, minutes, seconds);
   }
}
class Tester
{
   public static void Main()
   {
      TimeSpan myTime = new TimeSpan(43403);
      TimeSpan yourTime = new TimeSpan();
      Console.WriteLine("My time: " + myTime);
      Console.WriteLine("Your first time: " + yourTime);
      yourTime.Seconds = 310;
      Console.WriteLine("Your second time: " + yourTime);
   }
}

Result


Related Tutorials