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 174 views
1 answer 189 views
1 answer 147 views
147 views asked Jan 9, 2017 by avibootz
1 answer 162 views
162 views asked Dec 19, 2020 by avibootz
1 answer 197 views
1 answer 244 views
1 answer 245 views
...