+ 4
If you're mainly concerned about the compilation error, it has nothing to do with generics. When I ran your code and fixed the problem, it was that you called the non-static Print method from a static context. I fixed by making Print static.
The following code will run fine. I tested with the input:
Hello
1
3.4
Here's the fixed code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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(text);
Printer.Print(intNum);
Printer.Print(doubNum);
}
class Printer
{
public static void Print <T>( T x)
{
Console.WriteLine(x);
}
}
}
}
Generics are basically code templates. The generic types are symbols to be filled in.
For example, calling this with an int parameter causes the following:
public static void Print <T>( T x)
{
Console.WriteLine(x);
}
to work like:
public static void Print(int x)
{
Console.WriteLine(x);
}
If you called Print with a string, just replace T with string and you see how that'll work.
For more detail, check: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/