0

[SOLVED] (Thank you) Print to Printer C#

I'm trying to complete the Print to Printer, generic methods, code coach challenge detailed below: You are writing a program that can output the value of a variable of any type. it takes a string, an integer, and a double value as input and the should output those values. create a generic method Print for a Printer class to execute the given calls correctly. sample input Hello 14 7.6 sample output Showing Hello Showing 14 Showing 7.6 note that there is no object of Printer class, so the generic method should be static in my attempt at writing this method I've encountered an error code CS7036 There is no argument given that corresponds to the formal parameter... for the three Print functions. if anyone could point me in the right direction towards resolving this issue id appreciate it. thank you. my attempt: (updated to provide a correct solution) https://code.sololearn.com/ca23a147A2a2/#cs

9th Jun 2021, 4:46 AM
Andrew Hall
Andrew Hall - avatar
3 Réponses
+ 10
Andrew Hall If you are creating generic method then no need to create for all type. You can just do this: class Printer { public static void Print<T>(T t) { Console.WriteLine("Showing " + t); } }
9th Jun 2021, 5:27 AM
A͢J
A͢J - avatar
+ 2
The "ref" i used to implement - like in the tutorial - was my mistake. It's only needed, when a value with ref is given over. I tested it. So you can either write: Printer.Print(ref text); // ref added Printer.Print(ref intNum); Printer.Print(ref doubNum); } class Printer { public static void Print <T> (ref T t) // ref like in the tutorial { Console.WriteLine("Showing " + t); } Or as A suggested: Printer.Print(text); // wihtout ref as by default Printer.Print(intNum); Printer.Print(doubNum); } class Printer { public static void Print <T> (T t) // without ref, what is not explained { Console.WriteLine("Showing " + t); } Hopes that helps for e little explaination. For what "ref" is really for, i only know that it's a reference, but I'm still not familiar with it ^^
12th Feb 2023, 3:01 PM
Enrico Kubis
Enrico Kubis - avatar
+ 1
Brilliant thank you very much! the lessons didn't explain that very well. but that worked perfectly! ive updated the code in my link to reflect the changes for anyone else who encounters this issue.
9th Jun 2021, 5:33 AM
Andrew Hall
Andrew Hall - avatar