Problem solving C# 74.2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problem solving C# 74.2

Hi, I'm not sure if what I do is right or wrong. Anyway, it doesn't seem to be accepted. Can you hint me what I understood wrong ? Here is the task : "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 then it should output them. Create a generic method Print for a Printer class to execute the given calls correctly. " And here is my code : namespace SoloLearn { class Program { static void Main(string[] args) { string text = Console.ReadLine(); int intNum = Convert.ToInt32(Console.ReadLine()); double doubNum = Convert.ToDouble(Console.ReadLine()); Printer.Print<string>(ref text); Printer.Print<int>(ref intNum); Printer.Print<double>(ref doubNum); } } class Printer { //your code goes here public static void Print<T>(ref T message) { Console.WriteLine(message); } } }

9th Sep 2021, 5:48 PM
Loup-Venant
Loup-Venant - avatar
4 Answers
+ 2
Loup-Venant No need to use ref. You just have to make generic method which will accept all type of inputs. Here it is: public static void Print<T>(T message) { Console.WriteLine("Showing " + message); } Call this method like this: Printer.Print(text); When we call method we don't defined type, we just pass arguments. --> The answer was not valid because you didn't add this text "Showing " before the message.
10th Sep 2021, 2:52 AM
A͢J
A͢J - avatar
+ 1
Did you add ref there or they're already there by default?
9th Sep 2021, 8:08 PM
Tim
Tim - avatar
+ 1
I added it cuz I saw it in the examples before the exercises. With ref keyword, I get the right output but get marked wrong. If I remove it, and also remove the <types> like the <string> from Printer.Print<string>(ref text); then, the output is still good but not valid for SoloLearn for no explicit reason. I really need some hint here :/ I mean, I first thought that maybe I needed to have a method with three arguments as it says "String, int AND double as INPUT" but it makes no sense as the invoke only use one argument for each type plus it kinda defeat the fact that we use a generic method.
9th Sep 2021, 10:36 PM
Loup-Venant
Loup-Venant - avatar
+ 1
The fact that ref is used but never explained totally lost me in that part of the course. As for the "showing", I don't know why I didn't think it was part of the desired output but more of a meta. Thanks for the help !
10th Sep 2021, 6:53 AM
Loup-Venant
Loup-Venant - avatar