Create Fraction struct and add property - CSharp Custom Type

CSharp examples for Custom Type:Property

Description

Create Fraction struct and add property

Demo Code

using System;/*from w  w w.j a  v  a  2s .  c o m*/
struct Fraction
{
   private int numerator;
   private int denominator;
   public Fraction(int initNumerator, int initDenominator)
   {
      numerator = initNumerator;
      denominator = initDenominator;
   }
   public int Numerator
   {
      get
      {
         return numerator;
      }
      set
      {
         numerator = value;
      }
   }
   public int Denominator
   {
      get
      {
         return denominator;
      }
      set
      {
         denominator = value;
      }
   }
   public double Value
   {
      get
      {
         return (double)numerator / (double)denominator;
      }
   }
   public override string ToString()
   {
      string returnString;
      returnString = "Fraction value: " + Value;
      return returnString;
   }
}
class Tester
{
   public static void Main()
   {
      Fraction myFraction = new Fraction(1, 3);
      Console.WriteLine(myFraction);
   }
}

Result


Related Tutorials