How to use generic class that accept only type class in C#

1 Answer

0 votes
using System;

class Generic<T> where T: class
{
  public T gVariable {
    get;
    set;
  }
}

class Program
{
  static void Main(string[] args)
  {
    // Generic<int> g = new Generic<int>();
    // int is a value type
    // error CS0452: The type `int' must be a reference type 
 
    Generic<string> g = new Generic<string>();

    g.gVariable = "c#";

    Console.WriteLine(g.gVariable);
  }
}



/*
run:

c#

*/

 



answered Dec 19, 2020 by avibootz
edited Dec 19, 2020 by avibootz

Related questions

2 answers 208 views
1 answer 178 views
1 answer 149 views
149 views asked Dec 19, 2020 by avibootz
1 answer 142 views
142 views asked Jan 9, 2017 by avibootz
...