简体   繁体   中英

Calling a function in a class with the class as a parameter in c#

class Fraction
    {
        public double Num { get; set; }
        public double Denom { get; set; }
        public void show(Fraction f)
        {
            Console.Write($"({f.Num}/{f.Denom})");
        }
     }

this bit of code shows no errors, but when I try to actually call the show(Fraction x) function it gives the "The name 'show' does not exist in the current context" error. I assume I can't set a class as a parameter, then what workaround could you suggest?

This is the solution youre looking for

class Fraction
    {
        public double Num { get; set; }
        public double Denom { get; set; }
        public void show()
        {
            Console.Write($"({Num}/{Denom})");
        }
     }

You would call it this way.

var fraction = new Fraction{Num = 10, Denom = 10};
fraction.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM