How to use generic class in C#

1 Answer

0 votes
using System;

class Generic<T>
{
  private T gVariable;

  public Generic(T gValue) {
    this.gVariable = gValue;
  }

  public void Print() {
    Console.WriteLine(this.gVariable);
  }
}

class Program
{
  static void Main(string[] args)
  {
    Generic<int> g1 = new Generic<int>(12);

    Generic<string> g2 = new Generic<string>("C# Programming");

    g1.Print();
    g2.Print();
  }
}



/*
run:

12
C# Programming

*/

 



answered Dec 19, 2020 by avibootz

Related questions

1 answer 171 views
1 answer 178 views
1 answer 142 views
142 views asked Jan 9, 2017 by avibootz
1 answer 156 views
156 views asked Dec 19, 2020 by avibootz
1 answer 192 views
1 answer 237 views
1 answer 242 views
...