Help with generic methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with generic methods

Please help to solve the task from generic methods. I can't understand how to call generic method three times with different parametres. The text of task is: 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. Sample Input Hello 14 7.6 Sample Output Showing Hello Showing 14 Showing 7.6 The given code is: 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(text); Printer.Print(intNum); Printer.Print(doubNum); } } class Printer { //your code goes there } Thank you in advance)

28th Feb 2021, 4:17 AM
Sergey Salyakhov
Sergey Salyakhov - avatar
4 Answers
+ 3
No. like this public static void Print<T>(T data) { Console.WriteLine("Showing " + data); }
28th Feb 2021, 4:48 AM
A͢J
A͢J - avatar
+ 1
You just need to create a Generic method in Class Printer like this public void Print<T>(T data) { Console.WriteLine(data); }
28th Feb 2021, 4:21 AM
A͢J
A͢J - avatar
0
Like this? public void Print<T> (ref T text, ref T intNum, ref T doubNum) { Console.WriteLine ("Showing "+T); }
28th Feb 2021, 4:36 AM
Sergey Salyakhov
Sergey Salyakhov - avatar
0
Thanks a lot🤝
28th Feb 2021, 4:51 AM
Sergey Salyakhov
Sergey Salyakhov - avatar