Re: Struct vs Class



Hi,

You can't prevent the initialization of an instance of a struct with default
values. You'll always be able to code: Fraction fr = new Fraction(),
regardless of the constructors that you define.

If you decide to stick with a struct, don't forget to override Equals and
GetHashCode as well. And make sure that their return values are deterministic
and consistent with each other.

--
Dave Sexton

<JohnGoogle@xxxxxxxxxxxxx> wrote in message
news:1162071782.355396.265210@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

  • Struct vs Class
    ... The simplest declaration for the struct is: ... private Int32 _numerator; ... private Int32 _denominator; ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: help with radical expressions
    ... > in high school and College Algebra, but they went over it so quickly I ... the denominator, ... I see occurs in both terms in the numerator ... So I pull out a fraction that has ...
    (sci.math)
  • Re: Letter to the Editor: The truth of ID is self-evident
    ... The numerator and denominator of a fraction can be any ... And yes, 7 does divide 22. ...
    (talk.origins)