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 261 views
1 answer 294 views
1 answer 229 views
229 views asked Jan 9, 2017 by avibootz
1 answer 247 views
247 views asked Dec 19, 2020 by avibootz
1 answer 290 views
1 answer 319 views
1 answer 375 views
...