Struct vs Class



Hi,

Newbie question...

After a recent article in VSJ I had a go at implementing a Fraction
class to aid my understanding of operator overloading. After a previous
message someone suggested that I implement it as a struct rather than a
class which I did and all worked OK.

The simplest declaration for the struct is:

public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator;

public Int32 numerator
{
get { return _numerator; }
}

public Int32 denominator
{
get { return _denominator; }
}

public Fraction(Int32 numerator)
{
this._numerator = numerator;
this._denominator = 1;
}

public Fraction(Int32 numerator, Int32 denominator)
{
this._numerator = numerator;
this._denominator = denominator;
if (denominator < 1)
throw new ArgumentException("Fraction denominator value
'" + denominator.ToString() + "' is invalid.");
}

// All other declarations follow (for overloads of
+,-,*,/,++,--,>,<, etc).
}


As you can see, it is essential that the denominator must be 1 or more.
As it was originally a class, the constructors ensured that the
denominator was >= 1. Alos, the numerator and denominator values are
read only as they can only be modified by operators such as +,-,++,*,/
etc (not shown).

However, during my testing I realised that, as a struct, it is possible
to declare a struct which is initialised to 0 so the denominator
becomes illegal.

I've found that you cannot declare a parameterless construction such
as:

public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator;
....
public Fraction()
{
this._numerator = 0;
this._denominator = 1;
}
....
}

This gives a compiler error: 'Structs cannot contain explicit
parameterless constructors'.


Also, you cannot initialise the fields of a struct such as:

public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator = 1;
....
}

This gives a compilation error: '_denominator': cannot have instance
field initializers in structs'.


Is there any way to force a field of a struct to be initialised to a
non zero value?

If not, I'll go back to implementing it as a class.

TIA,

John.

.



Relevant Pages

  • Re: Struct vs Class
    ... niceties using a struct. ... public Int32 Denominator { ... private Int32 _numerator; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Struct vs Class
    ... You could make the _denominator field nullable and check for null in ... public struct Fraction { ... private Int32 _numerator; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Struct vs Class
    ... You can't prevent the initialization of an instance of a struct with default ... You'll always be able to code: Fraction fr = new Fraction, ... private Int32 _numerator; ... private Int32 _denominator; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Conflicting needs for __init__ method
    ... single integer, another Rational instance, and perhaps floats, Decimal ... And when initializing from a pair of integers---a numerator ... sure that the denominator is positive. ... like negation or raising to a positive integer power, ...
    (comp.lang.python)
  • Re: infinite product
    ... This result generalizes to products with an arbitrary number of numerator and denominator terms. ... Note that this does not always give interesting formulae, since both sides may be zero because of poles of Gamma. ... It is for this reason that it is advisable to keep x in formula. ...
    (sci.math)

Loading