Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:08:53+00:00 2026-05-26T18:08:53+00:00

This is a homework, and we don’t learn the structs in C# (and I

  • 0

This is a homework, and we don’t learn the structs in C# (and I am learn now). So I go to the msdn, but I don’t find what my program’s problem…
Yes, and I can’t translate fully assigned to my native languange, and I don’t understadn what the compiler thinks.

UML of my homework: (we must use it)
Interface: IComparable

  • +Numerator{get;}:long
  • +Denominator{get;}:long
  • +Rational(numerator:long, denominator:long)
  • +GCD(a:long, b:long):long
  • +Equals(r:Rationaol):bool
  • …

And we mustn’t implement another methods or params.

I write this homework in Java with classes and that work correctly.

I have a big problem, I don’t understand what mean the following erros:

Error 2 Backing field for automatically implemented property ‘Rational.Rational.Denominator’ must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.

Error 3 Backing field for automatically implemented property ‘Rational.Rational.Numerator’ must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.

Error 4 The ‘this’ object cannot be used before all of its fields are assigned to

my code:
namespace Rational

{
    //       (Rational is underline)     here the 2 and 3 error
    public struct Rational : IComparable<Rational>
    {
        public long Numerator { get; set; }
        public long Denominator { get; set; }

        public Rational(long num, long den)
        {
             // and here (GCD is underline) the 4. error
            long simple = GCD(num, den);
            this.Numerator = num/simple;
            this.Denominator = den/simple;
        }

        public long GCD(long a, long b)
        {
            long c = Math.Abs(a);
            long d = Math.Abs(b);
            if (d == 0) return c;
            return GCD(d, c % d);
        }

        public override string ToString()
        {
            if (Denominator==1)
            {
                return Numerator + "";
            }
            return Numerator+"/"+Denominator;
        }

        public override bool Equals(object obj)
        {
            bool result = false;
            if (obj is Rational)
            {
                Rational r = (Rational)obj;
                result = this.Equals(r);
            }
            return result;            
        }

        public bool Equals(Rational r)
        {
            if ((object)r ==null)
            {
                return false;
            }
            return this.Denominator == r.Denominator && this.Numerator == r.Numerator;
        }

        public int CompareTo(Rational other)
        {
      ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:08:54+00:00Added an answer on May 26, 2026 at 6:08 pm

    You can either add a this() to your constructor, or replace the auto-properties by properties backed by a field.

     public Rational(long num, long den)
     {
             // and here (GCD is underline) the 4. error
            long simple = GCD(num, den);
            this.Numerator = num;
            this.Denominator = den;
     }
    

    Here you access the instance method GCD before you assigned a value to the auto generated fields backing your properties.

    You should make this method static.

    Next you will get the same error again, this time because you access the auto property Numerator. You can fix that by keeping the auto properties and adding :this() to the constructor:

     public Rational(long num, long den)
       :this()
     {
    

    That results in the fields being initialized to 0 before your own constructor code runs.

    The alternative is switching to fields:

    public struct Rational : IComparable<Rational>
    {
        private long _numerator;
        private long _denominator;
    
        public long Numerator { get{return _numerator;}; set{_numerator=value;} }
        public long Denominator{ get{return denominator;}; set{_denominator=value;} }
    
        public Rational(long num, long den)
        {
             // and here (GCD is underline) the 4. error
            long simple = GCD(num, den);
            this._numerator = num;
            this._denominator = den;
        }
    

    Apart from this your code has few more issues:

    1) You’re using a mutable struct. That’s usually bad design. Remove the setter from your properties or make it private.

    2) You don’t override GetHashCode() to be consistent with Equals (or it’s just not shown in your code excerpt)

    3) I recommend implementing IEquatable<Rational>. You already implemented Equals(Rational), so you don’t need to add any additional methods.

    4) Your code produces int overflows very easily. Consider using BigIntegers instead of longs.

    5) Unless you normalize your rational (denominator >0 and dividing both by the GCD) you get mathematically equivalent rationals that don’t compare as equal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this homework. And I am beginner C# program, and now learn the
Ok this is a homework questions, but I cannot find the answer anywhere, not
Alright, i had this homework recently (don't worry, i've already done it, but in
I've heard that the 8-puzzle problem can be tackled via BFS, but I don't
Considering this is only for my homework I don't expect much help but I
I have this homework, I have only 1 problem, and I don't know the
This is a homework assignment so I don't want to post any code, but
This is a homework task, but it's very simple. The task comes with a
This is not homework, I need this for my program :) I ask this
First of all, this is not homework, so please don't tag it as homewrok

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.